Module: ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements
- Included in:
- ActiveRecord::ConnectionAdapters::SpannerAdapter
- Defined in:
- lib/active_record/connection_adapters/spanner/database_statements.rb
Constant Summary collapse
- COMMENT_REGEX =
ActiveRecord::ConnectionAdapters::AbstractAdapter::COMMENT_REGEX
Instance Method Summary collapse
- #begin_db_transaction ⇒ Object
-
#begin_isolated_db_transaction(isolation) ⇒ Object
Begins a transaction on the database with the specified isolation level.
- #commit_db_transaction ⇒ Object
- #exec_mutation(mutation) ⇒ Object
-
#exec_query(sql, name = "SQL", binds = [], prepare: false) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #exec_update(sql, name = "SQL", binds = []) ⇒ Object (also: #exec_delete)
-
#execute(sql, name = nil, binds = []) ⇒ Object
DDL, DML and DQL Statements.
- #execute_ddl(statements) ⇒ Object
- #query(sql, name = nil) ⇒ Object
- #rollback_db_transaction ⇒ Object
-
#transaction(requires_new: nil, isolation: nil, joinable: true) ⇒ Object
Transaction.
- #transaction_isolation_levels ⇒ Object
- #truncate(table_name, name = nil) ⇒ Object
- #update(arel, name = nil, binds = []) ⇒ Object (also: #delete)
- #write_query?(sql) ⇒ Boolean
Instance Method Details
#begin_db_transaction ⇒ Object
153 154 155 156 157 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 153 def begin_db_transaction log "BEGIN" do @connection.begin_transaction end end |
#begin_isolated_db_transaction(isolation) ⇒ Object
Begins a transaction on the database with the specified isolation level. Cloud Spanner only supports isolation level :serializable, but also defines three additional 'isolation levels' that can be used to start specific types of Spanner transactions:
- :read_only: Starts a read-only snapshot transaction using a strong timestamp bound.
- :buffered_mutations: Starts a read/write transaction that will use mutations instead of DML for single-row inserts/updates/deletes. Mutations are buffered locally until the transaction is committed, and any changes during a transaction cannot be read by the application.
- :pdml: Starts a Partitioned DML transaction. Executing multiple DML statements in one PDML transaction block is NOT supported A PDML transaction is not guaranteed to be atomic. See https://cloud.google.com/spanner/docs/dml-partitioned for more information.
In addition to the above, a Hash containing read-only snapshot options may be used to start a specific read-only snapshot:
- { timestamp: Time } Starts a read-only snapshot at the given timestamp.
- { staleness: Integer } Starts a read-only snapshot with the given staleness in seconds.
- { strong:
} Starts a read-only snapshot with strong timestamp bound (this is the same as :read_only)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 177 def begin_isolated_db_transaction isolation if isolation.is_a? Hash raise "Unsupported isolation level: #{isolation}" unless \ isolation[:timestamp] || isolation[:staleness] || isolation[:strong] raise "Only one option is supported. It must be one of `timestamp`, `staleness` or `strong`." \ if isolation.count != 1 else raise "Unsupported isolation level: #{isolation}" unless \ [:serializable, :read_only, :buffered_mutations, :pdml].include? isolation end log "BEGIN #{isolation}" do @connection.begin_transaction isolation end end |
#commit_db_transaction ⇒ Object
193 194 195 196 197 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 193 def commit_db_transaction log "COMMIT" do @connection.commit_transaction end end |
#exec_mutation(mutation) ⇒ Object
64 65 66 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 64 def exec_mutation mutation @connection.current_transaction.buffer mutation end |
#exec_query(sql, name = "SQL", binds = [], prepare: false) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
57 58 59 60 61 62 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 57 def exec_query sql, name = "SQL", binds = [], prepare: false # rubocop:disable Lint/UnusedMethodArgument result = execute sql, name, binds ActiveRecord::Result.new( result.fields.keys.map(&:to_s), result.rows.map(&:values) ) end |
#exec_update(sql, name = "SQL", binds = []) ⇒ Object Also known as: exec_delete
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 82 def exec_update sql, name = "SQL", binds = [] result = execute sql, name, binds # Make sure that we consume the entire result stream before trying to get the stats. # This is required because the ExecuteStreamingSql RPC is also used for (Partitioned) DML, # and this RPC can return multiple partial result sets for DML as well. Only the last partial # result set will contain the statistics. Although there will never be any rows, this makes # sure that the stream is fully consumed. result.rows.each { |_| } return result.row_count if result.row_count raise ActiveRecord::StatementInvalid.new( "DML statement is invalid.", sql: sql ) end |
#execute(sql, name = nil, binds = []) ⇒ Object
DDL, DML and DQL Statements
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 15 def execute sql, name = nil, binds = [] statement_type = sql_statement_type sql if preventing_writes? && [:dml, :ddl].include?(statement_type) raise ActiveRecord::ReadOnlyError( "Write query attempted while in readonly mode: #{sql}" ) end if statement_type == :ddl execute_ddl sql else transaction_required = statement_type == :dml materialize_transactions # First process and remove any hints in the binds that indicate that # a different read staleness should be used than the default. staleness_hint = binds.find { |b| b.is_a? Arel::Visitors::StalenessHint } if staleness_hint selector = Google::Cloud::Spanner::Session.single_use_transaction staleness_hint.value binds.delete staleness_hint end log sql, name do types, params = to_types_and_params binds ActiveSupport::Dependencies.interlock.permit_concurrent_loads do if transaction_required transaction do @connection.execute_query sql, params: params, types: types end else @connection.execute_query sql, params: params, types: types, single_use_selector: selector end end end end end |
#execute_ddl(statements) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 110 def execute_ddl statements log "MIGRATION", "SCHEMA" do ActiveSupport::Dependencies.interlock.permit_concurrent_loads do @connection.execute_ddl statements end end rescue Google::Cloud::Error => error raise ActiveRecord::StatementInvalid, error end |
#query(sql, name = nil) ⇒ Object
53 54 55 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 53 def query sql, name = nil exec_query sql, name end |
#rollback_db_transaction ⇒ Object
199 200 201 202 203 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 199 def rollback_db_transaction log "ROLLBACK" do @connection.rollback_transaction end end |
#transaction(requires_new: nil, isolation: nil, joinable: true) ⇒ Object
Transaction
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 122 def transaction requires_new: nil, isolation: nil, joinable: true if !requires_new && current_transaction.joinable? return super end backoff = 0.2 begin super rescue ActiveRecord::StatementInvalid => err if err.cause.is_a? Google::Cloud::AbortedError sleep(delay_from_aborted(err) || backoff *= 1.3) retry end raise end end |
#transaction_isolation_levels ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 139 def transaction_isolation_levels { read_uncommitted: "READ UNCOMMITTED", read_committed: "READ COMMITTED", repeatable_read: "REPEATABLE READ", serializable: "SERIALIZABLE", # These are not really isolation levels, but it is the only (best) way to pass in additional # transaction options to the connection. read_only: "READ_ONLY", buffered_mutations: "BUFFERED_MUTATIONS" } end |
#truncate(table_name, name = nil) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 98 def truncate table_name, name = nil Array(table_name).each do |t| log "TRUNCATE #{t}", name do @connection.truncate t end end end |
#update(arel, name = nil, binds = []) ⇒ Object Also known as: delete
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 68 def update arel, name = nil, binds = [] # Add a `WHERE TRUE` if it is an update_all or delete_all call that uses DML. if !should_use_mutation(arel) && arel.respond_to?(:ast) && arel.ast.wheres.empty? arel.ast.wheres << Arel::Nodes::SqlLiteral.new("TRUE") end return super unless should_use_mutation arel raise "Unsupported update for use with mutations: #{arel}" unless arel.is_a? Arel::DeleteManager exec_mutation create_delete_all_mutation arel if arel.is_a? Arel::DeleteManager 0 # Affected rows (unknown) end |
#write_query?(sql) ⇒ Boolean
106 107 108 |
# File 'lib/active_record/connection_adapters/spanner/database_statements.rb', line 106 def write_query? sql sql_statement_type(sql) == :dml end |