Methods
A
B
C
D
E
I
L
O
R
S
T
U
Instance Public methods
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 and an :offset option.

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
     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 217
217:       def add_limit_offset!(sql, options)
218:         if limit = options[:limit]
219:           sql << " LIMIT #{sanitize_limit(limit)}"
220:         end
221:         if offset = options[:offset]
222:           sql << " OFFSET #{offset.to_i}"
223:         end
224:         sql
225:       end
add_transaction_record(record)

Register a record with the current transaction so that its after_commit and after_rollback callbacks can be called.

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 189
189:       def add_transaction_record(record)
190:         last_batch = @_current_transaction_records.last
191:         last_batch << record if last_batch
192:       end
begin_db_transaction()

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 195
195:       def begin_db_transaction()    end
case_sensitive_equality_operator()
     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 246
246:       def case_sensitive_equality_operator
247:         "="
248:       end
commit_db_transaction()

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 198
198:       def commit_db_transaction()   end
default_sequence_name(table, column)
     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 227
227:       def default_sequence_name(table, column)
228:         nil
229:       end
delete(sql, name = nil)

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

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 53
53:       def delete(sql, name = nil)
54:         delete_sql(sql, name)
55:       end
empty_insert_statement_value()
     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 242
242:       def empty_insert_statement_value
243:         "VALUES(DEFAULT)"
244:       end
execute(sql, name = nil, skip_logging = false)

Executes the SQL statement in the context of this connection.

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 38
38:       def execute(sql, name = nil, skip_logging = false)
39:       end
insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

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

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 43
43:       def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
44:         insert_sql(sql, name, pk, id_value, sequence_name)
45:       end
insert_fixture(fixture, table_name)

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 238
238:       def insert_fixture(fixture, table_name)
239:         execute "INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
240:       end
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 250
250:       def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
251:         "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"
252:       end
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.

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 67
67:       def outside_transaction?
68:         nil
69:       end
reset_sequence!(table, column, sequence = nil)

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 232
232:       def reset_sequence!(table, column, sequence = nil)
233:         # Do nothing by default.  Implement for PostgreSQL, Oracle, ...
234:       end
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.

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 202
202:       def rollback_db_transaction() end
sanitize_limit(limit)

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

The 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, or an Arel SQL literal.

Returns Integer and Arel::Nodes::SqlLiteral limits as is. Returns the sanitized limit parameter, either as an integer, or as a string which contains a comma-delimited list of integers.

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 263
263:       def sanitize_limit(limit)
264:         if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral)
265:           limit
266:         elsif limit.to_s =~ /,/
267:           Arel.sql limit.to_s.split(',').map{ |i| Integer(i) }.join(',')
268:         else
269:           Integer(limit)
270:         end
271:       end
select_all(sql, name = nil)

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

   # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 6
6:       def select_all(sql, name = nil)
7:         select(sql, name)
8:       end
select_one(sql, name = nil)

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

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 12
12:       def select_one(sql, name = nil)
13:         result = select_all(sql, name)
14:         result.first if result
15:       end
select_rows(sql, name = nil)

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

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 33
33:       def select_rows(sql, name = nil)
34:       end
select_value(sql, name = nil)

Returns a single value from a record

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 18
18:       def select_value(sql, name = nil)
19:         if result = select_one(sql, name)
20:           result.values.first
21:         end
22:       end
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]
    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 26
26:       def select_values(sql, name = nil)
27:         result = select_rows(sql, name)
28:         result.map { |v| v[0] }
29:       end
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
     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 113
113:       def transaction(options = {})
114:         options.assert_valid_keys :requires_new, :joinable
115: 
116:         last_transaction_joinable = defined?(@transaction_joinable) ? @transaction_joinable : nil
117:         if options.has_key?(:joinable)
118:           @transaction_joinable = options[:joinable]
119:         else
120:           @transaction_joinable = true
121:         end
122:         requires_new = options[:requires_new] || !last_transaction_joinable
123: 
124:         transaction_open = false
125:         @_current_transaction_records ||= []
126: 
127:         begin
128:           if block_given?
129:             if requires_new || open_transactions == 0
130:               if open_transactions == 0
131:                 begin_db_transaction
132:               elsif requires_new
133:                 create_savepoint
134:               end
135:               increment_open_transactions
136:               transaction_open = true
137:               @_current_transaction_records.push([])
138:             end
139:             yield
140:           end
141:         rescue Exception => database_transaction_rollback
142:           if transaction_open && !outside_transaction?
143:             transaction_open = false
144:             decrement_open_transactions
145:             if open_transactions == 0
146:               rollback_db_transaction
147:               rollback_transaction_records(true)
148:             else
149:               rollback_to_savepoint
150:               rollback_transaction_records(false)
151:             end
152:           end
153:           raise unless database_transaction_rollback.is_a?(ActiveRecord::Rollback)
154:         end
155:       ensure
156:         @transaction_joinable = last_transaction_joinable
157: 
158:         if outside_transaction?
159:           @open_transactions = 0
160:         elsif transaction_open
161:           decrement_open_transactions
162:           begin
163:             if open_transactions == 0
164:               commit_db_transaction
165:               commit_transaction_records
166:             else
167:               release_savepoint
168:               save_point_records = @_current_transaction_records.pop
169:               unless save_point_records.blank?
170:                 @_current_transaction_records.push([]) if @_current_transaction_records.empty?
171:                 @_current_transaction_records.last.concat(save_point_records)
172:               end
173:             end
174:           rescue Exception => database_transaction_rollback
175:             if open_transactions == 0
176:               rollback_db_transaction
177:               rollback_transaction_records(true)
178:             else
179:               rollback_to_savepoint
180:               rollback_transaction_records(false)
181:             end
182:             raise
183:           end
184:         end
185:       end
update(sql, name = nil)

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

    # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 48
48:       def update(sql, name = nil)
49:         update_sql(sql, name)
50:       end
Instance Protected methods
commit_transaction_records()

Send a commit message to all records after they have been committed.

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 318
318:         def commit_transaction_records #:nodoc
319:           records = @_current_transaction_records.flatten
320:           @_current_transaction_records.clear
321:           unless records.blank?
322:             records.uniq.each do |record|
323:               begin
324:                 record.committed!
325:               rescue Exception => e
326:                 record.logger.error(e) if record.respond_to?(:logger) && record.logger
327:               end
328:             end
329:           end
330:         end
delete_sql(sql, name = nil)

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 292
292:         def delete_sql(sql, name = nil)
293:           update_sql(sql, name)
294:         end
insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 281
281:         def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
282:           execute(sql, name)
283:           id_value
284:         end
rollback_transaction_records(rollback)

Send a rollback message to all records after they have been rolled back. If rollback is false, only rollback records since the last save point.

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 298
298:         def rollback_transaction_records(rollback) #:nodoc
299:           if rollback
300:             records = @_current_transaction_records.flatten
301:             @_current_transaction_records.clear
302:           else
303:             records = @_current_transaction_records.pop
304:           end
305: 
306:           unless records.blank?
307:             records.uniq.each do |record|
308:               begin
309:                 record.rolledback!(rollback)
310:               rescue Exception => e
311:                 record.logger.error(e) if record.respond_to?(:logger) && record.logger
312:               end
313:             end
314:           end
315:         end
select(sql, name = nil)

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 276
276:         def select(sql, name = nil)
277:         end
update_sql(sql, name = nil)

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

     # File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 287
287:         def update_sql(sql, name = nil)
288:           execute(sql, name)
289:         end