Module: ActiveRecord::ConnectionAdapters::Fb::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::FbAdapter
Defined in:
lib/active_record/connection_adapters/fb/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#begin_db_transactionObject

Begins the transaction (and turns off auto-committing).



36
37
38
39
40
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 36

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’



54
55
56
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 54

def begin_isolated_db_transaction(isolation)
  @connection.transaction transaction_isolation_levels.fetch(isolation, isolation)
end

#commit_db_transactionObject

Commits the transaction (and turns on auto-committing).



59
60
61
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 59

def commit_db_transaction
  log('commit transaction', nil) { @connection.commit }
end

#default_sequence_name(table_name, _column = nil) ⇒ Object



69
70
71
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 69

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 = []) ⇒ Object

Executes sql statement in the context of this connection using binds as the bind substitutes. name is logged along with the executed sql statement.



20
21
22
23
24
25
26
27
28
29
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 20

def exec_query(sql, name = 'SQL', binds = [])
  translate_and_log(sql, binds, name) do |args|
    result, rows = @connection.execute(*args) do |cursor|
      [cursor.fields, cursor.fetchall]
    end
    next result unless result.respond_to?(:map)
    cols = result.map { |col| col.name }
    ActiveRecord::Result.new(cols, rows)
  end
end

#execute(sql, name = nil) ⇒ Object



11
12
13
14
15
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 11

def execute(sql, name = nil)
  translate_and_log(sql, [], name) do |args|
    @connection.execute(*args)
  end
end

#explain(arel, binds = []) ⇒ Object



31
32
33
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 31

def explain(arel, binds = [])
  to_sql(arel, binds)
end

#next_sequence_value(sequence_name) ⇒ Object

Uses the raw connection to get the next sequence value.



81
82
83
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 81

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.



74
75
76
77
78
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 74

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_transactionObject

Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block raises an exception or returns false.



65
66
67
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 65

def rollback_db_transaction
  log('rollback transaction', nil) { @connection.rollback }
end

#select_rows(sql, name = nil, binds = []) ⇒ Object

Returns an array of arrays containing the field values. Order is the same as that returned by columns.



7
8
9
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 7

def select_rows(sql, name = nil, binds = [])
  exec_query(sql, name, binds).to_a.map(&:values)
end

#transaction_isolation_levelsObject

Default isolation levels for transactions. This method exists in 4.0.2+, so it’s here for backward compatibility with AR 3



44
45
46
47
48
49
50
# File 'lib/active_record/connection_adapters/fb/database_statements.rb', line 44

def transaction_isolation_levels
  {
    read_committed:   "READ COMMITTED",
    repeatable_read:  "REPEATABLE READ",
    serializable:     "SERIALIZABLE"
  }
end