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
-
#begin_db_transaction ⇒ Object
Begins the transaction (and turns off auto-committing).
-
#commit_db_transaction ⇒ Object
Commits the transaction (and turns on auto-committing).
-
#default_sequence_name(table, _column) ⇒ Object
Returns the default sequence name for a table.
-
#exec_delete(sql, name, binds) ⇒ Object
(also: #exec_update)
Executes delete
sql
statement in the context of this connection usingbinds
as the bind substitutes. -
#exec_query(sql, name = 'SQL', binds = [], prepare: false) ⇒ Object
Executes
sql
statement in the context of this connection usingbinds
as the bind substitutes. -
#exec_rollback_db_transaction ⇒ Object
Rolls back the transaction (and turns on auto-committing).
-
#execute(sql, name = nil, binds = []) ⇒ Object
Executes the SQL statement in the context of this connection.
Instance Method Details
#begin_db_transaction ⇒ Object
Begins the transaction (and turns off auto-committing).
58 59 60 |
# File 'lib/odbc_adapter/database_statements.rb', line 58 def begin_db_transaction @connection.autocommit = false end |
#commit_db_transaction ⇒ Object
Commits the transaction (and turns on auto-committing).
63 64 65 66 |
# File 'lib/odbc_adapter/database_statements.rb', line 63 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.
78 79 80 |
# File 'lib/odbc_adapter/database_statements.rb', line 78 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.
52 53 54 |
# File 'lib/odbc_adapter/database_statements.rb', line 52 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 41 42 43 44 45 46 47 |
# File 'lib/odbc_adapter/database_statements.rb', line 23 def exec_query(sql, name = 'SQL', binds = [], prepare: false) 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 casters = TypeCaster.build_from(columns.values) if casters.any? values.each do |row| casters.each { |caster| row[caster.idx] = caster.cast(row[caster.idx]) } end end values = dbms_type_cast(columns.values, values) column_names = columns.keys.map { |key| format_case(key) } result = ActiveRecord::Result.new(column_names, values) end end |
#exec_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.
70 71 72 73 |
# File 'lib/odbc_adapter/database_statements.rb', line 70 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 |