Module: ActiveRecord::ConnectionAdapters::Rdb::DatabaseStatements
- Included in:
- ActiveRecord::ConnectionAdapters::RdbAdapter
- Defined in:
- lib/active_record/connection_adapters/rdb/database_statements.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#begin_db_transaction ⇒ Object
Begins the transaction (and turns off auto-committing).
-
#begin_isolated_db_transaction(isolation) ⇒ Object
Allows providing the :transaction option to ActiveRecord::Base.transaction in 4.0.2+.
-
#commit_db_transaction ⇒ Object
Commits the transaction (and turns on auto-committing).
- #default_sequence_name(table_name, _column = nil) ⇒ Object
- #exec_query(sql, name = 'SQL', binds = [], prepare: false) ⇒ Object
- #execute(sql, name = nil) ⇒ Object
- #explain(arel, binds = []) ⇒ Object
- #last_inserted_id(_result) ⇒ Object
-
#next_sequence_value(sequence_name) ⇒ Object
Uses the raw connection to get the next sequence value.
-
#reset_sequence!(table, column, sequence = nil) ⇒ Object
Set the sequence to the max value of the table’s column.
-
#rollback_db_transaction ⇒ Object
Rolls back the transaction (and turns on auto-committing).
-
#transaction_isolation_levels ⇒ Object
Default isolation levels for transactions.
Instance Method Details
#begin_db_transaction ⇒ Object
Begins the transaction (and turns off auto-committing).
44 45 46 47 48 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 44 def begin_db_transaction log('begin transaction', nil) do begin_isolated_db_transaction(default_transaction_isolation) end end |
#begin_isolated_db_transaction(isolation) ⇒ Object
Allows providing the :transaction option to ActiveRecord::Base.transaction in 4.0.2+. Can accept verbatim isolation options like ‘WAIT READ COMMITTED’
62 63 64 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 62 def begin_isolated_db_transaction(isolation) @connection.transaction transaction_isolation_levels.fetch(isolation, isolation) end |
#commit_db_transaction ⇒ Object
Commits the transaction (and turns on auto-committing).
67 68 69 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 67 def commit_db_transaction log('commit transaction', nil) { @connection.commit } end |
#default_sequence_name(table_name, _column = nil) ⇒ Object
77 78 79 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 77 def default_sequence_name(table_name, _column = nil) "#{table_name.to_s.tr('-', '_')[0, table_name_length - 4]}_seq" end |
#exec_query(sql, name = 'SQL', binds = [], prepare: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 13 def exec_query(sql, name = 'SQL', binds = [], prepare: false) type_casted_binds = type_casted_binds(binds) log(sql, name, binds, type_casted_binds) do ActiveSupport::Dependencies.interlock.permit_concurrent_loads do result = @connection.execute(sql, *type_casted_binds) if result.is_a?(Fb::Cursor) fields = result.fields.map(&:name) rows = result.fetchall.map do |row| row.map do |col| col.encode('UTF-8', @connection.encoding) rescue StandardError col end end result.close ActiveRecord::Result.new(fields, rows) else result end end end rescue StandardError => e raise e..encode('UTF-8', @connection.encoding) end |
#execute(sql, name = nil) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 5 def execute(sql, name = nil) log(sql, name) do ActiveSupport::Dependencies.interlock.permit_concurrent_loads do @connection.query(sql) end end end |
#explain(arel, binds = []) ⇒ Object
39 40 41 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 39 def explain(arel, binds = []) to_sql(arel, binds) end |
#last_inserted_id(_result) ⇒ Object
93 94 95 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 93 def last_inserted_id(_result) nil end |
#next_sequence_value(sequence_name) ⇒ Object
Uses the raw connection to get the next sequence value.
89 90 91 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 89 def next_sequence_value(sequence_name) @connection.query("SELECT NEXT VALUE FOR #{sequence_name} FROM RDB$DATABASE")[0][0] end |
#reset_sequence!(table, column, sequence = nil) ⇒ Object
Set the sequence to the max value of the table’s column.
82 83 84 85 86 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 82 def reset_sequence!(table, column, sequence = nil) sequence ||= default_sequence_name(table, column) max_id = select_value("select max(#{column}) from #{table}") execute("alter sequence #{sequence} restart with #{max_id}") end |
#rollback_db_transaction ⇒ Object
Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block raises an exception or returns false.
73 74 75 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 73 def rollback_db_transaction log('rollback transaction', nil) { @connection.rollback } end |
#transaction_isolation_levels ⇒ Object
Default isolation levels for transactions. This method exists in 4.0.2+, so it’s here for backward compatibility with AR 3
52 53 54 55 56 57 58 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 52 def transaction_isolation_levels { read_committed: 'READ COMMITTED', repeatable_read: 'REPEATABLE READ', serializable: 'SERIALIZABLE' } end |