# module ActiveRecord::ConnectionAdapters::MySQL::DatabaseStatements

### Public Instance Methods

exec_delete(sql, name = nil, binds = []) click to toggle source

    # File lib/active_record/connection_adapters/mysql/database_statements.rb, line 41
    def exec_delete(sql, name = nil, binds = [])
      if without_prepared_statement?(binds)
        execute_and_free(sql, name) { @connection.affected_rows }
      else
        exec_stmt_and_free(sql, name, binds) { |stmt| stmt.affected_rows }
      end
    end

Also aliased as: exec_update
exec_query(sql, name = "SQL", binds = [], prepare: false) click to toggle source

    # File lib/active_record/connection_adapters/mysql/database_statements.rb, line 29
    def exec_query(sql, name = "SQL", binds = [], prepare: false)
      if without_prepared_statement?(binds)
        execute_and_free(sql, name) do |result|
          ActiveRecord::Result.new(result.fields, result.to_a) if result
        end
      else
        exec_stmt_and_free(sql, name, binds, cache_stmt: prepare) do |_, result|
          ActiveRecord::Result.new(result.fields, result.to_a) if result
        end
      end
    end

exec_update(sql, name = nil, binds = [])
Alias for: exec_delete
execute(sql, name = nil) click to toggle source

Executes the SQL statement in the context of this connection.

Calls superclass method

    # File lib/active_record/connection_adapters/mysql/database_statements.rb, line 21
    def execute(sql, name = nil)
      # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
      # made since we established the connection
      @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone

      super
    end

### Private Instance Methods

exec_stmt_and_free(sql, name, binds, cache_stmt: false) { |stmt, result| ... } click to toggle source

    # File lib/active_record/connection_adapters/mysql/database_statements.rb, line 56
    def exec_stmt_and_free(sql, name, binds, cache_stmt: false)
      # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
      # made since we established the connection
      @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone

      type_casted_binds = type_casted_binds(binds)

      log(sql, name, binds, type_casted_binds) do
        if cache_stmt
          cache = @statements[sql] ||= {
            stmt: @connection.prepare(sql)
          }
          stmt = cache[:stmt]
        else
          stmt = @connection.prepare(sql)
        end

        begin
          result = ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
            stmt.execute(*type_casted_binds)
          end
        rescue Mysql2::Error => e
          if cache_stmt
            @statements.delete(sql)
          else
            stmt.close
          end
          raise e
        end

        ret = yield stmt, result
        result.free if result
        stmt.close unless cache_stmt
        ret
      end
    end

last_inserted_id(result) click to toggle source

    # File lib/active_record/connection_adapters/mysql/database_statements.rb, line 52
    def last_inserted_id(result)
      @connection.last_id
    end