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, 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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 113
113:       def add_limit_offset!(sql, options)
114:         if limit = options[:limit]
115:           sql << " LIMIT #{sanitize_limit(limit)}"
116:           if offset = options[:offset]
117:             sql << " OFFSET #{offset.to_i}"
118:           end
119:         end
120:         sql
121:       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 128
128:       def add_lock!(sql, options)
129:         case lock = options[:lock]
130:           when true;   sql << ' FOR UPDATE'
131:           when String; sql << " #{lock}"
132:         end
133:       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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 154
154:       def case_sensitive_equality_operator
155:         "="
156:       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 135
135:       def default_sequence_name(table, column)
136:         nil
137:       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 150
150:       def empty_insert_statement(table_name)
151:         "INSERT INTO #{quote_table_name(table_name)} VALUES(DEFAULT)"
152:       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 146
146:       def insert_fixture(fixture, table_name)
147:         execute "INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
148:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 158
158:       def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
159:         "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"
160:       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 140
140:       def reset_sequence!(table, column, sequence = nil)
141:         # Do nothing by default.  Implement for PostgreSQL, Oracle, ...
142:       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

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 181
181:         def delete_sql(sql, name = nil)
182:           update_sql(sql, name)
183:         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 170
170:         def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
171:           execute(sql, name)
172:           id_value
173:         end

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.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 192
192:         def sanitize_limit(limit)
193:           if limit.to_s =~ /,/
194:             limit.to_s.split(',').map{ |i| i.to_i }.join(',')
195:           else
196:             limit.to_i
197:           end
198:         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 165
165:         def select(sql, name = nil)
166:           raise NotImplementedError, "select is an abstract method"
167:         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 176
176:         def update_sql(sql, name = nil)
177:           execute(sql, name)
178:         end

[Validate]