Module: ActiveRecord::ConnectionAdapters::ArFirebird::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::ArFirebirdAdapter
Defined in:
lib/active_record/connection_adapters/ar_firebird/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#begin_db_transactionObject



46
47
48
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 46

def begin_db_transaction
  log("begin transaction", nil) { @connection.transaction('READ COMMITTED') }
end

#commit_db_transactionObject



50
51
52
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 50

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

#create_sequence(sequence_name) ⇒ Object



83
84
85
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 83

def create_sequence(sequence_name)
  execute("CREATE SEQUENCE #{sequence_name}") rescue nil
end

#create_table(table_name, **options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 58

def create_table(table_name, **options)
  super(table_name, options) do |td|
    yield td if block_given?()
    # We have to map the columns to check if we have to change the type
    td.columns.each do |col|
      if col.options[:limit] && col.type == :integer
        col.type = :bigint
      end
    end
  end
  if options[:sequence] != false && options[:id] != false
    sequence_name = options[:sequence] || default_sequence_name(table_name)
    create_sequence(sequence_name)
  end
end

#default_sequence_name(table_name, _column = nil) ⇒ Object



95
96
97
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 95

def default_sequence_name(table_name, _column = nil)
  "#{table_name}_g01"
end

#drop_sequence(sequence_name) ⇒ Object



87
88
89
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 87

def drop_sequence(sequence_name)
  execute("DROP SEQUENCE #{sequence_name}") rescue nil
end

#drop_table(table_name, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 74

def drop_table(table_name, options = {})
  if options[:sequence] != false
    sequence_name = options[:sequence] || default_sequence_name(table_name)
    drop_sequence(sequence_name) if sequence_exists?(sequence_name)
  end

  super
end

#exec_query(sql, name = 'SQL', binds = [], prepare: false) ⇒ Object



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
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 15

def exec_query(sql, name = 'SQL', binds = [], prepare: false)
  sql = sql.encode(encoding, 'UTF-8')

  type_casted_binds = type_casted_binds(binds).map do |value|
    value.encode(encoding) rescue value
  end

  log(sql, name, binds, type_casted_binds) do
    ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
      begin
        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 col
            end
          end

          result.close
          ActiveRecord::Result.new(fields, rows)
        else
          result
        end
      rescue Exception => e
        raise e.message.encode('UTF-8', @connection.encoding)
      end
    end
  end
end

#exec_rollback_db_transactionObject



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

def exec_rollback_db_transaction
  log("rollback transaction", nil) { @connection.rollback }
end

#execute(sql, name = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 5

def execute(sql, name = nil)
  sql = sql.encode(encoding, 'UTF-8')

  log(sql, name) do
    ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
      @connection.query(sql)
    end
  end
end

#next_sequence_value(sequence_name) ⇒ Object



99
100
101
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 99

def next_sequence_value(sequence_name)
  @connection.query("SELECT NEXT VALUE FOR #{sequence_name} FROM RDB$DATABASE")[0][0]
end

#remove_column(table_name, column_name, type = nil, options = {}) ⇒ Object



103
104
105
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 103

def remove_column(table_name, column_name, type = nil, options = {})
  execute "ALTER TABLE #{table_name} DROP #{column_name}"
end

#sequence_exists?(sequence_name) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/active_record/connection_adapters/ar_firebird/database_statements.rb', line 91

def sequence_exists?(sequence_name)
  @connection.generator_names.include?(sequence_name)
end