Class: Connection
- Inherits:
-
Object
- Object
- Connection
- Defined in:
- lib/spannerlib/connection.rb
Instance Attribute Summary collapse
-
#conn_id ⇒ Object
readonly
Returns the value of attribute conn_id.
-
#pool_id ⇒ Object
readonly
Returns the value of attribute pool_id.
Instance Method Summary collapse
-
#begin_transaction(transaction_options = nil) ⇒ Object
Begin a read/write transaction on this connection.
-
#close ⇒ Object
Closes this connection.
-
#commit ⇒ Object
Commit the current transaction.
-
#execute(request) ⇒ Object
Execute SQL request (expects a request object with to_proto or raw bytes).
-
#execute_batch(request) ⇒ Object
Execute batch DML/DDL request.
-
#initialize(pool_id, conn_id) ⇒ Connection
constructor
A new instance of Connection.
-
#rollback ⇒ Object
Rollback the current transaction.
-
#write_mutations(mutation_group) ⇒ Object
Accepts either an object that responds to ‘to_proto` or a raw string/bytes containing the serialized mutation proto.
Constructor Details
#initialize(pool_id, conn_id) ⇒ Connection
Returns a new instance of Connection.
23 24 25 26 |
# File 'lib/spannerlib/connection.rb', line 23 def initialize(pool_id, conn_id) @pool_id = pool_id @conn_id = conn_id end |
Instance Attribute Details
#conn_id ⇒ Object (readonly)
Returns the value of attribute conn_id.
21 22 23 |
# File 'lib/spannerlib/connection.rb', line 21 def conn_id @conn_id end |
#pool_id ⇒ Object (readonly)
Returns the value of attribute pool_id.
21 22 23 |
# File 'lib/spannerlib/connection.rb', line 21 def pool_id @pool_id end |
Instance Method Details
#begin_transaction(transaction_options = nil) ⇒ Object
Begin a read/write transaction on this connection. Accepts TransactionOptions proto or bytes. Returns message bytes (or nil) — higher-level parsing not implemented here.
44 45 46 47 48 49 50 51 |
# File 'lib/spannerlib/connection.rb', line 44 def begin_transaction( = nil) bytes = if .respond_to?(:to_proto) .to_proto else .is_a?(String) ? : &.to_s end SpannerLib.begin_transaction(@pool_id, @conn_id, bytes) end |
#close ⇒ Object
Closes this connection. Any active transaction on the connection is rolled back.
87 88 89 90 |
# File 'lib/spannerlib/connection.rb', line 87 def close SpannerLib.close_connection(@pool_id, @conn_id) nil end |
#commit ⇒ Object
Commit the current transaction. Returns CommitResponse bytes or nil.
54 55 56 |
# File 'lib/spannerlib/connection.rb', line 54 def commit SpannerLib.commit(@pool_id, @conn_id, proto_klass: Google::Cloud::Spanner::V1::CommitResponse) end |
#execute(request) ⇒ Object
Execute SQL request (expects a request object with to_proto or raw bytes). Returns message bytes (or nil).
65 66 67 68 69 70 71 72 73 |
# File 'lib/spannerlib/connection.rb', line 65 def execute(request) bytes = if request.respond_to?(:to_proto) request.to_proto else request.is_a?(String) ? request : request.to_s end rows_id = SpannerLib.execute(@pool_id, @conn_id, bytes) SpannerLib::Rows.new(self, rows_id) end |
#execute_batch(request) ⇒ Object
Execute batch DML/DDL request. Returns ExecuteBatchDmlResponse bytes (or nil).
76 77 78 79 80 81 82 83 84 |
# File 'lib/spannerlib/connection.rb', line 76 def execute_batch(request) bytes = if request.respond_to?(:to_proto) request.to_proto else request.is_a?(String) ? request : request.to_s end SpannerLib.execute_batch(@pool_id, @conn_id, bytes, proto_klass: Google::Cloud::Spanner::V1::ExecuteBatchDmlResponse) end |
#rollback ⇒ Object
Rollback the current transaction.
59 60 61 62 |
# File 'lib/spannerlib/connection.rb', line 59 def rollback SpannerLib.rollback(@pool_id, @conn_id) nil end |
#write_mutations(mutation_group) ⇒ Object
Accepts either an object that responds to ‘to_proto` or a raw string/bytes containing the serialized mutation proto. We avoid requiring the protobuf definitions at load time so specs that don’t need them can run.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/spannerlib/connection.rb', line 31 def write_mutations(mutation_group) req_bytes = if mutation_group.respond_to?(:to_proto) mutation_group.to_proto elsif mutation_group.is_a?(String) mutation_group else mutation_group.to_s end SpannerLib.write_mutations(@pool_id, @conn_id, req_bytes, proto_klass: Google::Cloud::Spanner::V1::CommitResponse) end |