Module

ActiveRecord::ConnectionAdapters::DatabaseStatements

Inheritance

Methods

Instance

Visibility Signature
public add_limit! (sql, options)
public add_limit_offset! (sql, options)
public add_lock! (sql, options)
public begin_db_transaction ()
public case_sensitive_equality_operator ()
public commit_db_transaction ()
public default_sequence_name (table, column)
public delete (sql, name = nil)
public empty_insert_statement (table_name)
public execute (sql, name = nil, skip_logging = false)
public insert (sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
public insert_fixture (fixture, table_name)
public limited_update_conditions (where_sql, quoted_table_name, quoted_primary_key)
public outside_transaction? ()
public reset_sequence! (table, column, sequence = nil)
public rollback_db_transaction ()
public select_all (sql, name = nil)
public select_one (sql, name = nil)
public select_rows (sql, name = nil)
public select_value (sql, name = nil)
public select_values (sql, name = nil)
public transaction (options = {}) {|| ...}
public update (sql, name = nil)
protected delete_sql (sql, name = nil)
protected insert_sql (sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
protected sanitize_limit (limit)
protected select (sql, name = nil)
protected update_sql (sql, name = nil)

Instance Method Detail

add_limit!(sql, options)

Alias for add_limit_offset!.

add_limit_offset!(sql, options)

Appends LIMIT and OFFSET options to an SQL statement, or some SQL fragment that has the same semantics as LIMIT and OFFSET.

options must be a Hash which contains a +:limit+ option (required) and an +:offset+ option (optional).

This method modifies the sql parameter.

Examples
 add_limit_offset!('SELECT * FROM suppliers', {:limit => 10, :offset => 50})

generates

 SELECT * FROM suppliers LIMIT 10 OFFSET 50

add_lock!(sql, options)

Appends a locking clause to an SQL statement. This method modifies the sql parameter.

  # SELECT * FROM suppliers FOR UPDATE
  add_lock! 'SELECT * FROM suppliers', :lock => true
  add_lock! 'SELECT * FROM suppliers', :lock => ' FOR UPDATE'

begin_db_transaction()

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

case_sensitive_equality_operator()

commit_db_transaction()

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

default_sequence_name(table, column)

delete(sql, name = nil)

Executes the delete statement and returns the number of rows affected.

empty_insert_statement(table_name)

execute(sql, name = nil, skip_logging = false)

Executes the SQL statement in the context of this connection.

insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

Returns the last auto-generated ID from the affected table.

insert_fixture(fixture, table_name)

Inserts the given fixture into the table. Overridden in adapters that require something beyond a simple insert (eg. Oracle).

limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)

outside_transaction?()

Checks whether there is currently no transaction active. This is done by querying the database driver, and does not use the transaction house-keeping information recorded by increment_open_transactions and friends.

Returns true if there is no transaction active, false if there is a transaction active, and nil if this information is unknown.

Not all adapters supports transaction state introspection. Currently, only the PostgreSQL adapter supports this.

reset_sequence!(table, column, sequence = nil)

Set the sequence to the max value of the table‘s column.

rollback_db_transaction()

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

select_all(sql, name = nil)

Returns an array of record hashes with the column names as keys and column values as values.

select_one(sql, name = nil)

Returns a record hash with the column names as keys and column values as values.

select_rows(sql, name = nil)

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

select_value(sql, name = nil)

Returns a single value from a record

select_values(sql, name = nil)

Returns an array of the values of the first column in a select:

  select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]

transaction(options = {}) {|| ...}

Runs the given block in a database transaction, and returns the result of the block.

Nested transactions support

Most databases don‘t support true nested transactions. At the time of writing, the only database that supports true nested transactions that we‘re aware of, is MS-SQL.

In order to get around this problem, transaction will emulate the effect of nested transactions, by using savepoints: dev.mysql.com/doc/refman/5.0/en/savepoints.html Savepoints are supported by MySQL and PostgreSQL, but not SQLite3.

It is safe to call this method if a database transaction is already open, i.e. if transaction is called within another transaction block. In case of a nested call, transaction will behave as follows:

  • The block will be run without doing anything. All database statements that happen within the block are effectively appended to the already open database transaction.
  • However, if +:requires_new+ is set, the block will be wrapped in a database savepoint acting as a sub-transaction.

Caveats

MySQL doesn‘t support DDL transactions. If you perform a DDL operation, then any created savepoints will be automatically released. For example, if you‘ve created a savepoint, then you execute a CREATE TABLE statement, then the savepoint that was created will be automatically released.

This means that, on MySQL, you shouldn‘t execute DDL operations inside a transaction call that you know might create a savepoint. Otherwise, transaction will raise exceptions when it tries to release the already-automatically-released savepoints:

  Model.connection.transaction do  # BEGIN
    Model.connection.transaction(:requires_new => true) do  # CREATE SAVEPOINT active_record_1
      Model.connection.create_table(...)
      # active_record_1 now automatically released
    end  # RELEASE SAVEPOINT active_record_1  <--- BOOM! database error!
  end

update(sql, name = nil)

Executes the update statement and returns the number of rows affected.

delete_sql(sql, name = nil)

Executes the delete statement and returns the number of rows affected.

insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

Returns the last auto-generated ID from the affected table.

sanitize_limit(limit)

Sanitizes the given LIMIT parameter in order to prevent SQL injection.

limit may be anything that can evaluate to a string via to_s. It should look like an integer, or a comma-delimited list of integers.

Returns the sanitized limit parameter, either as an integer, or as a string which contains a comma-delimited list of integers.

select(sql, name = nil)

Returns an array of record hashes with the column names as keys and column values as values.

update_sql(sql, name = nil)

Executes the update statement and returns the number of rows affected.