Module ActiveRecord::ConnectionAdapters::DatabaseStatements
In: vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb

Methods

Public Instance methods

Alias for add_limit_offset!.

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 97
97:       def add_limit!(sql, options)
98:         add_limit_offset!(sql, options) if options
99:       end

Appends LIMIT and OFFSET options to an SQL statement. 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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 107
107:       def add_limit_offset!(sql, options)
108:         if limit = options[:limit]
109:           sql << " LIMIT #{sanitize_limit(limit)}"
110:           if offset = options[:offset]
111:             sql << " OFFSET #{offset.to_i}"
112:           end
113:         end
114:         sql
115:       end

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'

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 126
126:       def add_lock!(sql, options)
127:         case lock = options[:lock]
128:           when true;   sql << ' FOR UPDATE'
129:           when String; sql << " #{lock}"
130:         end
131:       end

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 87
87:       def begin_db_transaction()    end

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 90
90:       def commit_db_transaction()   end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 133
133:       def default_sequence_name(table, column)
134:         nil
135:       end

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

[Source]

    # File vendor/rails/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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 148
148:       def empty_insert_statement(table_name)
149:         "INSERT INTO #{quote_table_name(table_name)} VALUES(DEFAULT)"
150:       end

Executes the SQL statement in the context of this connection.

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 38
38:       def execute(sql, name = nil)
39:         raise NotImplementedError, "execute is an abstract method"
40:       end

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

[Source]

    # File vendor/rails/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

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 144
144:       def insert_fixture(fixture, table_name)
145:         execute "INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
146:       end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 138
138:       def reset_sequence!(table, column, sequence = nil)
139:         # Do nothing by default.  Implement for PostgreSQL, Oracle, ...
140:       end

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 94
94:       def rollback_db_transaction() end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 117
117:       def sanitize_limit(limit)
118:         limit.to_s[/,/] ? limit.split(',').map{ |i| i.to_i }.join(',') : limit.to_i
119:       end

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

[Source]

   # File vendor/rails/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

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

[Source]

    # File vendor/rails/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

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 33
33:       def select_rows(sql, name = nil)
34:         raise NotImplementedError, "select_rows is an abstract method"
35:       end

Returns a single value from a record

[Source]

    # File vendor/rails/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

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

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

[Source]

    # File vendor/rails/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

Wrap a block in a transaction. Returns result of block.

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 58
58:       def transaction(start_db_transaction = true)
59:         transaction_open = false
60:         begin
61:           if block_given?
62:             if start_db_transaction
63:               begin_db_transaction
64:               transaction_open = true
65:             end
66:             yield
67:           end
68:         rescue Exception => database_transaction_rollback
69:           if transaction_open
70:             transaction_open = false
71:             rollback_db_transaction
72:           end
73:           raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
74:         end
75:       ensure
76:         if transaction_open
77:           begin
78:             commit_db_transaction
79:           rescue Exception => database_transaction_rollback
80:             rollback_db_transaction
81:             raise
82:           end
83:         end
84:       end

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

[Source]

    # File vendor/rails/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

Protected Instance methods

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 171
171:         def delete_sql(sql, name = nil)
172:           update_sql(sql, name)
173:         end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 160
160:         def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
161:           execute(sql, name)
162:           id_value
163:         end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 155
155:         def select(sql, name = nil)
156:           raise NotImplementedError, "select is an abstract method"
157:         end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 166
166:         def update_sql(sql, name = nil)
167:           execute(sql, name)
168:         end

[Validate]