|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# frozen_string_literal: true |
| 16 | + |
| 17 | +require_relative "ffi" |
| 18 | + |
| 19 | +class Connection |
| 20 | + attr_reader :pool_id, :conn_id |
| 21 | + |
| 22 | + def initialize(pool_id, conn_id) |
| 23 | + @pool_id = pool_id |
| 24 | + @conn_id = conn_id |
| 25 | + end |
| 26 | + |
| 27 | + # Accepts either an object that responds to `to_proto` or a raw string/bytes |
| 28 | + # containing the serialized mutation proto. We avoid requiring the protobuf |
| 29 | + # definitions at load time so specs that don't need them can run. |
| 30 | + def write_mutations(mutation_group) |
| 31 | + req_bytes = if mutation_group.respond_to?(:to_proto) |
| 32 | + mutation_group.to_proto |
| 33 | + elsif mutation_group.is_a?(String) |
| 34 | + mutation_group |
| 35 | + else |
| 36 | + mutation_group.to_s |
| 37 | + end |
| 38 | + |
| 39 | + SpannerLib.write_mutations(@pool_id, @conn_id, req_bytes) |
| 40 | + end |
| 41 | + |
| 42 | + # Begin a read/write transaction on this connection. Accepts TransactionOptions proto or bytes. |
| 43 | + # Returns message bytes (or nil) — higher-level parsing not implemented here. |
| 44 | + def begin_transaction(transaction_options = nil) |
| 45 | + bytes = if transaction_options.respond_to?(:to_proto) |
| 46 | + transaction_options.to_proto |
| 47 | + else |
| 48 | + transaction_options.is_a?(String) ? transaction_options : transaction_options&.to_s |
| 49 | + end |
| 50 | + SpannerLib.begin_transaction(@pool_id, @conn_id, bytes) |
| 51 | + end |
| 52 | + |
| 53 | + # Commit the current transaction. Returns CommitResponse bytes or nil. |
| 54 | + def commit |
| 55 | + SpannerLib.commit(@pool_id, @conn_id) |
| 56 | + end |
| 57 | + |
| 58 | + # Rollback the current transaction. |
| 59 | + def rollback |
| 60 | + SpannerLib.rollback(@pool_id, @conn_id) |
| 61 | + nil |
| 62 | + end |
| 63 | + |
| 64 | + # Execute SQL request (expects a request object with to_proto or raw bytes). Returns message bytes (or nil). |
| 65 | + def execute(request) |
| 66 | + bytes = if request.respond_to?(:to_proto) |
| 67 | + request.to_proto |
| 68 | + else |
| 69 | + request.is_a?(String) ? request : request.to_s |
| 70 | + end |
| 71 | + SpannerLib.execute(@pool_id, @conn_id, bytes) |
| 72 | + end |
| 73 | + |
| 74 | + # Execute batch DML/DDL request. Returns ExecuteBatchDmlResponse bytes (or nil). |
| 75 | + def execute_batch(request) |
| 76 | + bytes = if request.respond_to?(:to_proto) |
| 77 | + request.to_proto |
| 78 | + else |
| 79 | + request.is_a?(String) ? request : request.to_s |
| 80 | + end |
| 81 | + SpannerLib.execute_batch(@pool_id, @conn_id, bytes) |
| 82 | + end |
| 83 | + |
| 84 | + # Rows helpers — return raw message bytes (caller should parse them). |
| 85 | + def metadata(rows_id) |
| 86 | + SpannerLib.metadata(@pool_id, @conn_id, rows_id) |
| 87 | + end |
| 88 | + |
| 89 | + def next_rows(rows_id, num_rows, encoding = 0) |
| 90 | + SpannerLib.next(@pool_id, @conn_id, rows_id, num_rows, encoding) |
| 91 | + end |
| 92 | + |
| 93 | + def result_set_stats(rows_id) |
| 94 | + SpannerLib.result_set_stats(@pool_id, @conn_id, rows_id) |
| 95 | + end |
| 96 | + |
| 97 | + def close_rows(rows_id) |
| 98 | + SpannerLib.close_rows(@pool_id, @conn_id, rows_id) |
| 99 | + end |
| 100 | + |
| 101 | + # Closes this connection. Any active transaction on the connection is rolled back. |
| 102 | + def close |
| 103 | + SpannerLib.close_connection(@pool_id, @conn_id) |
| 104 | + nil |
| 105 | + end |
| 106 | +end |
0 commit comments