- A
- B
- C
- D
- E
- I
- L
- O
- R
- S
- T
- U
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
Register a record with the current transaction so that its after_commit and after_rollback callbacks can be called.
Begins the transaction (and turns off auto-committing).
Commits the transaction (and turns on auto-committing).
Executes the delete statement and returns the number of rows affected.
Executes the SQL statement in the context of this connection.
Returns the last auto-generated ID from the affected table.
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
# 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
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.
Set the sequence to the max value of the table’s column.
Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block raises an exception or returns false.
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
Returns an array of record hashes with the column names as keys and column values as values.
Returns a record hash with the column names as keys and column values as values.
Returns an array of arrays containing the field values. Order is the same as that returned by columns.
Returns a single value from a record
Returns an array of the values of the first column in a select:
select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
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
Executes the update statement and returns the number of rows affected.
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
Executes the delete statement and returns the number of rows affected.
Returns the last auto-generated ID from the affected table.
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
Returns an array of record hashes with the column names as keys and column values as values.