Files

Class Index [+]

Quicksearch

ActiveRecord::ConnectionAdapters::DatabaseStatements

Public Instance Methods

add_limit_offset!(sql, options) click to toggle source

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 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) click to toggle source

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

     # File 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() click to toggle source

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

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

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

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 198
198:       def commit_db_transaction()   end
default_sequence_name(table, column) click to toggle source
     # File 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) click to toggle source

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

    # File 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() click to toggle source
     # File 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) click to toggle source

Executes the SQL statement in the context of this connection.

    # File 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) click to toggle source

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

    # File 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) click to toggle source

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

     # File 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) click to toggle source
     # File 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?() click to toggle source

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 # 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 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) click to toggle source

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

     # File 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() click to toggle source

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

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 202
202:       def rollback_db_transaction() end
select_all(sql, name = nil) click to toggle source

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

   # File 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) click to toggle source

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

    # File 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) click to toggle source

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

    # File 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) click to toggle source

Returns a single value from a record

    # File 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) click to toggle source

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 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 = {}) click to toggle source

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, # 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 # is called within another # block. In case of a nested call, # 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 # call that you know might create a savepoint. Otherwise, # 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 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) click to toggle source

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

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

Protected Instance Methods

commit_transaction_records() click to toggle source

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

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

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

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 273
273:         def delete_sql(sql, name = nil)
274:           update_sql(sql, name)
275:         end
insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) click to toggle source

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

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 262
262:         def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
263:           execute(sql, name)
264:           id_value
265:         end
rollback_transaction_records(rollback) click to toggle source

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 lib/active_record/connection_adapters/abstract/database_statements.rb, line 294
294:         def rollback_transaction_records(rollback) #:nodoc
295:           if rollback
296:             records = @_current_transaction_records.flatten
297:             @_current_transaction_records.clear
298:           else
299:             records = @_current_transaction_records.pop
300:           end
301: 
302:           unless records.blank?
303:             records.uniq.each do |record|
304:               begin
305:                 record.rolledback!(rollback)
306:               rescue Exception => e
307:                 record.logger.error(e) if record.respond_to?(:logger) && record.logger
308:               end
309:             end
310:           end
311:         end
sanitize_limit(limit) click to toggle source

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

limit may be anything that can evaluate to a string via #. 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.

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 284
284:         def sanitize_limit(limit)
285:           if limit.to_s =~ /,/
286:             limit.to_s.split(',').map{ |i| i.to_i }.join(',')
287:           else
288:             limit.to_i
289:           end
290:         end
select(sql, name = nil) click to toggle source

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

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 257
257:         def select(sql, name = nil)
258:         end
update_sql(sql, name = nil) click to toggle source

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

     # File lib/active_record/connection_adapters/abstract/database_statements.rb, line 268
268:         def update_sql(sql, name = nil)
269:           execute(sql, name)
270:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.