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
- #insert_fixtures_set(fixture_set, tables_to_delete = []) ⇒ 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).
47 48 49 50 51 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 47 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'
65 66 67 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 65 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).
70 71 72 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 70 def commit_db_transaction log('commit transaction', nil) { @connection.commit } end |
#default_sequence_name(table_name, _column = nil) ⇒ Object
80 81 82 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 80 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 38 39 40 |
# 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 rescue => e result.close if result && result.is_a?(Fb::Cursor) raise e end end rescue => e raise e 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
42 43 44 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 42 def explain(arel, binds = []) to_sql(arel, binds) end |
#insert_fixtures_set(fixture_set, tables_to_delete = []) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 100 def insert_fixtures_set(fixture_set, tables_to_delete = []) fixture_inserts = fixture_set.map do |table_name, fixtures| next if fixtures.empty? build_fixture_sql(fixtures, table_name).collect {|f| f } end.compact table_deletes = tables_to_delete.map { |table| "DELETE FROM #{quote_table_name table}".dup } sql = fixture_inserts.flatten(1) sql.unshift(*table_deletes) transaction(requires_new: true) do sql.each do |s| execute s, 'Fixture load' end end end |
#last_inserted_id(_result) ⇒ Object
96 97 98 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 96 def last_inserted_id(_result) nil end |
#next_sequence_value(sequence_name) ⇒ Object
Uses the raw connection to get the next sequence value.
92 93 94 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 92 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.
85 86 87 88 89 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 85 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.
76 77 78 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 76 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
55 56 57 58 59 60 61 |
# File 'lib/active_record/connection_adapters/rdb/database_statements.rb', line 55 def transaction_isolation_levels { read_committed: 'READ COMMITTED', repeatable_read: 'REPEATABLE READ', serializable: 'SERIALIZABLE' } end |