Module: ActiveRecord::ConnectionAdapters::Firebird::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::FirebirdAdapter
Defined in:
lib/active_record/connection_adapters/firebird/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#begin_db_transactionObject



66
67
68
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 66

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

#commit_db_transactionObject



70
71
72
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 70

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

#create_sequence(sequence_name) ⇒ Object



96
97
98
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 96

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

#create_table(table_name, **options) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 78

def create_table(table_name, **options)
  super

  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



108
109
110
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 108

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

#drop_sequence(sequence_name) ⇒ Object



100
101
102
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 100

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

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



87
88
89
90
91
92
93
94
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 87

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 29

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)
        elsif name.end_with?(' Destroy')
          result
        else
          ActiveRecord::Result.new([], [])
        end
      rescue RangeError => e
        return ActiveRecord::Result.new([], []) if sql.downcase.start_with? 'select'
        raise e
      rescue Exception => e
        result.close if result.is_a?(Fb::Cursor)
        raise e.message.encode('UTF-8', @connection.encoding)
      end
    end
  end
end

#exec_rollback_db_transactionObject



74
75
76
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 74

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

#execute(sql, name = nil) ⇒ Object



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

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

#native_database_typesObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 3

def native_database_types
  {
    primary_key: 'integer not null primary key',
    string:      { name: 'varchar', limit: 255 },
    text:        { name: 'blob sub_type text' },
    integer:     { name: 'integer' },
    float:       { name: 'float' },
    decimal:     { name: 'decimal' },
    datetime:    { name: 'timestamp' },
    timestamp:   { name: 'timestamp' },
    date:        { name: 'date' },
    binary:      { name: 'blob' },
    boolean:     { name: 'smallint' }
  }
end

#next_sequence_value(sequence_name) ⇒ Object



112
113
114
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 112

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

#sequence_exists?(sequence_name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/active_record/connection_adapters/firebird/database_statements.rb', line 104

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