database_statements.rb 12.6 KB
Newer Older
1 2 3
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module DatabaseStatements
4 5
      def initialize
        super
6
        reset_transaction
7 8
      end

9
      # Converts an arel AST to SQL
10
      def to_sql(arel, binds = [])
11
        if arel.respond_to?(:ast)
12
          binds = binds.dup
13 14 15
          visitor.accept(arel.ast) do
            quote(*binds.shift.reverse)
          end
16 17 18 19 20
        else
          arel
        end
      end

21 22
      # Returns an array of record hashes with the column names as keys and
      # column values as values.
23
      def select_all(arel, name = nil, binds = [])
24
        select(to_sql(arel, binds), name, binds)
25
      end
26

27 28
      # Returns a record hash with the column names as keys and column values
      # as values.
29 30
      def select_one(arel, name = nil, binds = [])
        result = select_all(arel, name, binds)
31
        result.first if result
32
      end
33 34

      # Returns a single value from a record
35 36
      def select_value(arel, name = nil, binds = [])
        if result = select_one(arel, name, binds)
37 38
          result.values.first
        end
39 40 41 42
      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]
43
      def select_values(arel, name = nil)
44
        result = select_rows(to_sql(arel, []), name)
45
        result.map { |v| v[0] }
46 47
      end

48
      # Returns an array of arrays containing the field values.
P
Pratik Naik 已提交
49
      # Order is the same as that returned by +columns+.
50 51
      def select_rows(sql, name = nil)
      end
52
      undef_method :select_rows
53

54
      # Executes the SQL statement in the context of this connection.
A
Aaron Patterson 已提交
55
      def execute(sql, name = nil)
56
      end
57
      undef_method :execute
58

A
Aaron Patterson 已提交
59
      # Executes +sql+ statement in the context of this connection using
60
      # +binds+ as the bind substitutes. +name+ is logged along with
A
Aaron Patterson 已提交
61
      # the executed +sql+ statement.
62
      def exec_query(sql, name = 'SQL', binds = [])
A
Aaron Patterson 已提交
63 64
      end

65
      # Executes insert +sql+ statement in the context of this connection using
W
Waseem Ahmad 已提交
66
      # +binds+ as the bind substitutes. +name+ is logged along with
67
      # the executed +sql+ statement.
68
      def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
69 70 71
        exec_query(sql, name, binds)
      end

72
      # Executes delete +sql+ statement in the context of this connection using
W
Waseem Ahmad 已提交
73
      # +binds+ as the bind substitutes. +name+ is logged along with
74 75 76 77 78
      # the executed +sql+ statement.
      def exec_delete(sql, name, binds)
        exec_query(sql, name, binds)
      end

79
      # Executes update +sql+ statement in the context of this connection using
W
Waseem Ahmad 已提交
80
      # +binds+ as the bind substitutes. +name+ is logged along with
81 82 83 84 85
      # the executed +sql+ statement.
      def exec_update(sql, name, binds)
        exec_query(sql, name, binds)
      end

86
      # Returns the last auto-generated ID from the affected table.
87 88 89 90 91 92 93
      #
      # +id_value+ will be returned unless the value is nil, in
      # which case the database will attempt to calculate the last inserted
      # id and return that value.
      #
      # If the next id was calculated in advance (as in Oracle), it should be
      # passed in as +id_value+.
94
      def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
95
        sql, binds = sql_for_insert(to_sql(arel, binds), pk, id_value, sequence_name, binds)
96
        value      = exec_insert(sql, name, binds, pk, sequence_name)
97
        id_value || last_inserted_id(value)
98
      end
99 100

      # Executes the update statement and returns the number of rows affected.
101
      def update(arel, name = nil, binds = [])
102
        exec_update(to_sql(arel, binds), name, binds)
103
      end
104 105

      # Executes the delete statement and returns the number of rows affected.
106
      def delete(arel, name = nil, binds = [])
107
        exec_delete(to_sql(arel, binds), name, binds)
108
      end
109

110 111 112 113 114 115 116 117 118 119 120 121 122
      # 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.
      def outside_transaction?
        nil
      end
123

124 125 126 127 128 129
      # Returns +true+ when the connection adapter supports prepared statement
      # caching, otherwise returns +false+
      def supports_statement_cache?
        false
      end

130 131 132 133 134 135 136 137 138 139 140
      # 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:
V
Vijay Dev 已提交
141
      # http://dev.mysql.com/doc/refman/5.0/en/savepoint.html
142 143 144 145 146 147 148 149 150
      # 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.
151
      # - However, if +:requires_new+ is set, the block will be wrapped in a
152
      #   database savepoint acting as a sub-transaction.
153 154 155 156 157 158 159 160 161 162 163 164 165 166
      #
      # === 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
167
      #     Model.connection.transaction(:requires_new => true) do  # CREATE SAVEPOINT active_record_1
168
      #       Model.connection.create_table(...)
169 170
      #       # active_record_1 now automatically released
      #     end  # RELEASE SAVEPOINT active_record_1  <--- BOOM! database error!
171
      #   end
172 173 174
      def transaction(options = {})
        options.assert_valid_keys :requires_new, :joinable

175 176 177 178
        if !options[:requires_new] && current_transaction.joinable?
          within_existing_transaction { yield }
        else
          within_new_transaction(options) { yield }
179
        end
180 181 182
      rescue Exception => error
        raise unless error.is_a?(ActiveRecord::Rollback)
      end
183

184 185 186 187 188 189
      def within_new_transaction(options = {}) #:nodoc:
        begin_transaction(options)
        yield
      rescue Exception => error
        rollback_transaction unless outside_transaction?
        raise
190
      ensure
191
        if outside_transaction?
192 193
          reset_transaction
        else
194
          begin
195 196
            commit_transaction unless error
          rescue Exception => e
J
DRY  
Jon Leighton 已提交
197
            rollback_transaction
198 199 200
            raise
          end
        end
201
      end
202

203 204 205 206 207 208
      def within_existing_transaction #:nodoc:
        yield
      ensure
        reset_transaction if outside_transaction?
      end

209
      def current_transaction #:nodoc:
210 211 212
        @transaction
      end

213 214 215 216 217
      def transaction_open?
        @transaction.open?
      end

      def begin_transaction(options = {}) #:nodoc:
218
        @transaction = @transaction.begin
219 220
        @transaction.joinable = options.fetch(:joinable, true)
        @transaction
221 222
      end

J
DRY  
Jon Leighton 已提交
223 224 225 226
      def commit_transaction #:nodoc:
        @transaction = @transaction.commit
      end

227 228 229 230
      def rollback_transaction #:nodoc:
        @transaction = @transaction.rollback
      end

231 232 233 234
      def reset_transaction
        @transaction = ClosedTransaction.new(self)
      end

235 236 237
      # Register a record with the current transaction so that its after_commit and after_rollback callbacks
      # can be called.
      def add_transaction_record(record)
238
        @transaction.add_record(record)
239 240
      end

241 242 243 244 245 246
      # Begins the transaction (and turns off auto-committing).
      def begin_db_transaction()    end

      # Commits the transaction (and turns on auto-committing).
      def commit_db_transaction()   end

247 248
      # Rolls back the transaction (and turns on auto-committing). Must be
      # done if the transaction block raises an exception or returns false.
249 250
      def rollback_db_transaction() end

251 252 253 254 255 256
      def default_sequence_name(table, column)
        nil
      end

      # Set the sequence to the max value of the table's column.
      def reset_sequence!(table, column, sequence = nil)
257
        # Do nothing by default. Implement for PostgreSQL, Oracle, ...
258
      end
259

260
      # Inserts the given fixture into the table. Overridden in adapters that require
261 262
      # something beyond a simple insert (eg. Oracle).
      def insert_fixture(fixture, table_name)
263 264 265 266 267 268 269 270 271
        columns = Hash[columns(table_name).map { |c| [c.name, c] }]

        key_list   = []
        value_list = fixture.map do |name, value|
          key_list << quote_column_name(name)
          quote(value, columns[name])
        end

        execute "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES (#{value_list.join(', ')})", 'Fixture Insert'
272 273
      end

274 275
      def empty_insert_statement_value
        "VALUES(DEFAULT)"
276 277
      end

278 279 280 281
      def case_sensitive_equality_operator
        "="
      end

282 283 284 285
      def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
        "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"
      end

286 287
      # Sanitizes the given LIMIT parameter in order to prevent SQL injection.
      #
288
      # The +limit+ may be anything that can evaluate to a string via #to_s. It
289
      # should look like an integer, or a comma-delimited list of integers, or
290
      # an Arel SQL literal.
291
      #
292
      # Returns Integer and Arel::Nodes::SqlLiteral limits as is.
293 294 295
      # Returns the sanitized limit parameter, either as an integer, or as a
      # string which contains a comma-delimited list of integers.
      def sanitize_limit(limit)
296 297 298
        if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral)
          limit
        elsif limit.to_s =~ /,/
299 300 301 302 303 304
          Arel.sql limit.to_s.split(',').map{ |i| Integer(i) }.join(',')
        else
          Integer(limit)
        end
      end

305 306 307 308
      # The default strategy for an UPDATE with joins is to use a subquery. This doesn't work
      # on mysql (even when aliasing the tables), but mysql allows using JOIN directly in
      # an UPDATE statement, so in the mysql adapters we redefine this to do that.
      def join_to_update(update, select) #:nodoc:
309 310
        key = update.key
        subselect = subquery_for(key, select)
311

312 313 314 315 316 317 318
        update.where key.in(subselect)
      end

      def join_to_delete(delete, select, key) #:nodoc:
        subselect = subquery_for(key, select)

        delete.where key.in(subselect)
319 320
      end

321
      protected
322 323 324 325 326 327 328 329

        # Return a subquery for the given key using the join information.
        def subquery_for(key, select)
          subselect = select.clone
          subselect.projections = [key]
          subselect
        end

330 331
        # Returns an array of record hashes with the column names as keys and
        # column values as values.
332
        def select(sql, name = nil, binds = [])
333
        end
334
        undef_method :select
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

        # Returns the last auto-generated ID from the affected table.
        def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
          execute(sql, name)
          id_value
        end

        # Executes the update statement and returns the number of rows affected.
        def update_sql(sql, name = nil)
          execute(sql, name)
        end

        # Executes the delete statement and returns the number of rows affected.
        def delete_sql(sql, name = nil)
          update_sql(sql, name)
        end
351

352 353 354 355 356 357 358 359
      def sql_for_insert(sql, pk, id_value, sequence_name, binds)
        [sql, binds]
      end

      def last_inserted_id(result)
        row = result.rows.first
        row && row.first
      end
360 361
    end
  end
362
end