Module: ODBCAdapter::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::ODBCAdapter
Defined in:
lib/odbc_adapter/database_statements.rb

Constant Summary collapse

SQL_NO_NULLS =

ODBC constants missing from Christian Werner’s Ruby ODBC driver

0
SQL_NULLABLE =
1
SQL_NULLABLE_UNKNOWN =
2

Instance Method Summary collapse

Instance Method Details

#begin_db_transactionObject

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



51
52
53
# File 'lib/odbc_adapter/database_statements.rb', line 51

def begin_db_transaction
  @connection.autocommit = false
end

#commit_db_transactionObject

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



56
57
58
59
# File 'lib/odbc_adapter/database_statements.rb', line 56

def commit_db_transaction
  @connection.commit
  @connection.autocommit = true
end

#default_sequence_name(table, _column) ⇒ Object

Returns the default sequence name for a table. Used for databases which don’t support an autoincrementing column type, but do support sequences.



71
72
73
# File 'lib/odbc_adapter/database_statements.rb', line 71

def default_sequence_name(table, _column)
  "#{table}_seq"
end

#exec_delete(sql, name, binds) ⇒ Object Also known as: exec_update

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



45
46
47
# File 'lib/odbc_adapter/database_statements.rb', line 45

def exec_delete(sql, name, binds)
  execute(sql, name, binds)
end

#exec_query(sql, name = 'SQL', binds = [], prepare: false) ⇒ 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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/odbc_adapter/database_statements.rb', line 23

def exec_query(sql, name = 'SQL', binds = [], prepare: false) # rubocop:disable Lint/UnusedMethodArgument
  log(sql, name) do
    stmt =
      if prepared_statements
        @connection.run(sql, *prepared_binds(binds))
      else
        @connection.run(sql)
      end

    columns = stmt.columns
    values  = stmt.to_a
    stmt.drop

    values = dbms_type_cast(columns.values, values)
    column_names = columns.keys.map { |key| format_case(key) }
    ActiveRecord::Result.new(column_names, values)
  end
end

#exec_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.



63
64
65
66
# File 'lib/odbc_adapter/database_statements.rb', line 63

def exec_rollback_db_transaction
  @connection.rollback
  @connection.autocommit = true
end

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

Executes the SQL statement in the context of this connection. Returns the number of rows affected.



10
11
12
13
14
15
16
17
18
# File 'lib/odbc_adapter/database_statements.rb', line 10

def execute(sql, name = nil, binds = [])
  log(sql, name) do
    if prepared_statements
      @connection.do(sql, *prepared_binds(binds))
    else
      @connection.do(sql)
    end
  end
end