database_statements.rb 12.5 KB
Newer Older
1 2 3
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module DatabaseStatements
4 5
      def initialize
        super
6
        @transaction_joinable = nil
J
Jon Leighton 已提交
7
        @transaction          = ClosedTransaction.new(self)
8 9
      end

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

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

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

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

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

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

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

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

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

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

87
      # Returns the last auto-generated ID from the affected table.
88 89 90 91 92 93 94
      #
      # +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+.
95
      def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
96
        sql, binds = sql_for_insert(to_sql(arel, binds), pk, id_value, sequence_name, binds)
97
        value      = exec_insert(sql, name, binds, pk, sequence_name)
98
        id_value || last_inserted_id(value)
99
      end
100 101

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

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

111 112 113 114 115 116 117 118 119 120 121 122 123
      # 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
124

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

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

176
        last_transaction_joinable = @transaction_joinable
177 178 179
        @transaction_joinable     = options.fetch(:joinable, true)
        requires_new              = options[:requires_new] || !last_transaction_joinable
        transaction_open          = false
180

181
        begin
182
          if @transaction.closed? || requires_new
J
DRY  
Jon Leighton 已提交
183
            begin_transaction
184
            transaction_open = true
185
          end
186

187
          yield
188 189
        rescue Exception => error
          if !outside_transaction? && transaction_open
J
DRY  
Jon Leighton 已提交
190
            rollback_transaction
191 192
            transaction_open = false
          end
193 194

          raise unless error.is_a?(ActiveRecord::Rollback)
195
        end
196

197
      ensure
198 199
        @transaction_joinable = last_transaction_joinable

200
        if outside_transaction?
J
Jon Leighton 已提交
201
          @transaction = ClosedTransaction.new(self)
202
        elsif @transaction.open? && transaction_open
203
          begin
J
DRY  
Jon Leighton 已提交
204
            commit_transaction
A
Aaron Patterson 已提交
205
          rescue Exception
J
DRY  
Jon Leighton 已提交
206
            rollback_transaction
207 208 209
            raise
          end
        end
210
      end
211

212 213 214 215 216 217 218 219
      def transaction_state #:nodoc:
        @transaction
      end

      def begin_transaction #:nodoc:
        @transaction = @transaction.begin
      end

J
DRY  
Jon Leighton 已提交
220 221 222 223
      def commit_transaction #:nodoc:
        @transaction = @transaction.commit
      end

224 225 226 227
      def rollback_transaction #:nodoc:
        @transaction = @transaction.rollback
      end

228 229 230
      # Register a record with the current transaction so that its after_commit and after_rollback callbacks
      # can be called.
      def add_transaction_record(record)
231
        @transaction.add_record(record)
232 233
      end

234 235 236 237 238 239
      # 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

240 241
      # Rolls back the transaction (and turns on auto-committing). Must be
      # done if the transaction block raises an exception or returns false.
242 243
      def rollback_db_transaction() end

244 245 246 247 248 249
      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)
250
        # Do nothing by default. Implement for PostgreSQL, Oracle, ...
251
      end
252

253
      # Inserts the given fixture into the table. Overridden in adapters that require
254 255
      # something beyond a simple insert (eg. Oracle).
      def insert_fixture(fixture, table_name)
256 257 258 259 260 261 262 263 264
        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'
265 266
      end

267 268
      def empty_insert_statement_value
        "VALUES(DEFAULT)"
269 270
      end

271 272 273 274
      def case_sensitive_equality_operator
        "="
      end

275 276 277 278
      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

279 280
      # Sanitizes the given LIMIT parameter in order to prevent SQL injection.
      #
281
      # The +limit+ may be anything that can evaluate to a string via #to_s. It
282
      # should look like an integer, or a comma-delimited list of integers, or
283
      # an Arel SQL literal.
284
      #
285
      # Returns Integer and Arel::Nodes::SqlLiteral limits as is.
286 287 288
      # 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)
289 290 291
        if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral)
          limit
        elsif limit.to_s =~ /,/
292 293 294 295 296 297
          Arel.sql limit.to_s.split(',').map{ |i| Integer(i) }.join(',')
        else
          Integer(limit)
        end
      end

298 299 300 301
      # 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:
302 303
        key = update.key
        subselect = subquery_for(key, select)
304

305 306 307 308 309 310 311
        update.where key.in(subselect)
      end

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

        delete.where key.in(subselect)
312 313
      end

314
      protected
315 316 317 318 319 320 321 322

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

323 324
        # Returns an array of record hashes with the column names as keys and
        # column values as values.
325
        def select(sql, name = nil, binds = [])
326
        end
327
        undef_method :select
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

        # 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
344

345 346 347 348 349 350 351 352
      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
353 354
    end
  end
355
end