Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

### Home Classes Methods

### In Files

- lib/active_record/connection_adapters/mysql2_adapter.rb

### Parent

ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter

### Methods

- ::new
- #active?
- #create
- #disconnect!
- #error_number
- #exec_delete
- #exec_insert
- #exec_query
- #exec_update
- #exec_without_stmt
- #execute
- #explain
- #insert_sql
- #last_inserted_id
- #quote_string
- #reconnect!
- #reset!
- #select
- #select_rows
- #supports_explain?

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- ActiveRecord
- ActiveRecord::ActiveRecordError
- ActiveRecord::AdapterNotFound
- ActiveRecord::AdapterNotSpecified
- ActiveRecord::Aggregations
- ActiveRecord::Aggregations::ClassMethods
- ActiveRecord::AssociationTypeMismatch
- ActiveRecord::Associations
- ActiveRecord::Associations::ClassMethods
- ActiveRecord::AttributeAssignment
- ActiveRecord::AttributeAssignment::ClassMethods
- ActiveRecord::AttributeAssignmentError
- ActiveRecord::AttributeMethods
- ActiveRecord::AttributeMethods::BeforeTypeCast
- ActiveRecord::AttributeMethods::ClassMethods
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead::ClassMethods
- ActiveRecord::AttributeMethods::Dirty
- ActiveRecord::AttributeMethods::PrimaryKey
- ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods
- ActiveRecord::AttributeMethods::Query
- ActiveRecord::AttributeMethods::Read
- ActiveRecord::AttributeMethods::Read::ClassMethods
- ActiveRecord::AttributeMethods::Serialization
- ActiveRecord::AttributeMethods::Serialization::Attribute
- ActiveRecord::AttributeMethods::Serialization::ClassMethods
- ActiveRecord::AttributeMethods::TimeZoneConversion
- ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods
- ActiveRecord::AttributeMethods::Write
- ActiveRecord::AttributeMethods::Write::ClassMethods
- ActiveRecord::AutosaveAssociation
- ActiveRecord::AutosaveAssociation::ClassMethods
- ActiveRecord::Base
- ActiveRecord::Batches
- ActiveRecord::Calculations
- ActiveRecord::Callbacks
- ActiveRecord::Coders
- ActiveRecord::ConfigurationError
- ActiveRecord::ConnectionAdapters
- ActiveRecord::ConnectionAdapters::AbstractAdapter
- ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- ActiveRecord::ConnectionAdapters::ConnectionManagement
- ActiveRecord::ConnectionAdapters::ConnectionPool
- ActiveRecord::ConnectionAdapters::DatabaseLimits
- ActiveRecord::ConnectionAdapters::DatabaseStatements
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils
- ActiveRecord::ConnectionAdapters::PostgreSQLColumn
- ActiveRecord::ConnectionAdapters::QueryCache
- ActiveRecord::ConnectionAdapters::Quoting
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::ExplainPrettyPrinter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::Version
- ActiveRecord::ConnectionAdapters::SchemaCache
- ActiveRecord::ConnectionAdapters::SchemaStatements
- ActiveRecord::ConnectionAdapters::StatementPool
- ActiveRecord::ConnectionAdapters::Table
- ActiveRecord::ConnectionAdapters::TableDefinition
- ActiveRecord::ConnectionNotEstablished
- ActiveRecord::ConnectionTimeoutError
- ActiveRecord::CounterCache
- ActiveRecord::DangerousAttributeError
- ActiveRecord::Delegation
- ActiveRecord::DynamicFinderMatch
- ActiveRecord::DynamicMatchers
- ActiveRecord::DynamicScopeMatch
- ActiveRecord::Explain
- ActiveRecord::FinderMethods
- ActiveRecord::Fixtures
- ActiveRecord::Fixtures::File
- ActiveRecord::Generators
- ActiveRecord::Generators::Migration
- ActiveRecord::Generators::MigrationGenerator
- ActiveRecord::Generators::ModelGenerator
- ActiveRecord::Generators::ObserverGenerator
- ActiveRecord::Generators::SessionMigrationGenerator
- ActiveRecord::IdentityMap
- ActiveRecord::IdentityMap::Middleware
- ActiveRecord::Inheritance
- ActiveRecord::Inheritance::ClassMethods
- ActiveRecord::Integration
- ActiveRecord::InvalidForeignKey
- ActiveRecord::IrreversibleMigration
- ActiveRecord::Locking
- ActiveRecord::Locking::Optimistic
- ActiveRecord::Locking::Optimistic::ClassMethods
- ActiveRecord::Locking::Pessimistic
- ActiveRecord::LogSubscriber
- ActiveRecord::Migration
- ActiveRecord::Migration::CommandRecorder
- ActiveRecord::MigrationProxy
- ActiveRecord::ModelSchema
- ActiveRecord::ModelSchema::ClassMethods
- ActiveRecord::MultiparameterAssignmentErrors
- ActiveRecord::NestedAttributes
- ActiveRecord::NestedAttributes::ClassMethods
- ActiveRecord::NestedAttributes::TooManyRecords
- ActiveRecord::Observer
- ActiveRecord::Persistence
- ActiveRecord::Persistence::ClassMethods
- ActiveRecord::PreparedStatementInvalid
- ActiveRecord::QueryCache
- ActiveRecord::QueryCache::ClassMethods
- ActiveRecord::QueryMethods
- ActiveRecord::Querying
- ActiveRecord::Railtie
- ActiveRecord::Railties
- ActiveRecord::Railties::ControllerRuntime
- ActiveRecord::Railties::ControllerRuntime::ClassMethods
- ActiveRecord::ReadOnlyRecord
- ActiveRecord::ReadonlyAttributes
- ActiveRecord::ReadonlyAttributes::ClassMethods
- ActiveRecord::RecordInvalid
- ActiveRecord::RecordNotFound
- ActiveRecord::RecordNotSaved
- ActiveRecord::RecordNotUnique
- ActiveRecord::Reflection
- ActiveRecord::Reflection::ClassMethods
- ActiveRecord::Reflection::MacroReflection
- ActiveRecord::Result
- ActiveRecord::Rollback
- ActiveRecord::Sanitization
- ActiveRecord::Sanitization::ClassMethods
- ActiveRecord::Schema
- ActiveRecord::Scoping
- ActiveRecord::Scoping::ClassMethods
- ActiveRecord::Scoping::Default
- ActiveRecord::Scoping::Default::ClassMethods
- ActiveRecord::Scoping::Named
- ActiveRecord::Scoping::Named::ClassMethods
- ActiveRecord::Serialization
- ActiveRecord::SerializationTypeMismatch
- ActiveRecord::SessionStore
- ActiveRecord::SessionStore::Session
- ActiveRecord::SessionStore::SqlBypass
- ActiveRecord::SpawnMethods
- ActiveRecord::StaleObjectError
- ActiveRecord::StatementInvalid
- ActiveRecord::Store
- ActiveRecord::Store::ClassMethods
- ActiveRecord::TestFixtures
- ActiveRecord::TestFixtures::ClassMethods
- ActiveRecord::ThrowResult
- ActiveRecord::Timestamp
- ActiveRecord::Transactions
- ActiveRecord::Transactions::ClassMethods
- ActiveRecord::Translation
- ActiveRecord::UnknownAttributeError
- ActiveRecord::UnknownPrimaryKey
- ActiveRecord::Validations
- ActiveRecord::Validations::AssociatedValidator
- ActiveRecord::Validations::ClassMethods
- ActiveRecord::Validations::UniquenessValidator
- ActiveRecord::WrappedDatabaseException
- Mysql
- Mysql::Result
- Mysql::Stmt
- Mysql::Time
- ArJdbcMySQL
- ArJdbcMySQL::Error

No matching classes.

# ActiveRecord::ConnectionAdapters::Mysql2Adapter

### Constants

ADAPTER_NAME

### Public Class Methods

new(connection, logger, connection_options, config) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 33
    def initialize(connection, logger, connection_options, config)
      super
      @visitor = BindSubstitution.new self
      configure_connection
    end

### Public Instance Methods

active?() click to toggle source

CONNECTION MANAGEMENT ====================================

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 71
    def active?
      return false unless @connection
      @connection.ping
    end

create(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) click to toggle source
Alias for: insert_sql
disconnect!() click to toggle source

Disconnects from the database if already connected. Otherwise, this method does nothing.

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 83
    def disconnect!
      unless @connection.nil?
        @connection.close
        @connection = nil
      end
    end

error_number(exception) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 59
    def error_number(exception)
      exception.error_number if exception.respond_to?(:error_number)
    end

exec_delete(sql, name, binds) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 239
    def exec_delete(sql, name, binds)
      execute to_sql(sql, binds), name
      @connection.affected_rows
    end

Also aliased as: exec_update
exec_insert(sql, name, binds) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 235
    def exec_insert(sql, name, binds)
      execute to_sql(sql, binds), name
    end

exec_query(sql, name = 'SQL', binds = []) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 216
    def exec_query(sql, name = 'SQL', binds = [])
      result = execute(sql, name)
      ActiveRecord::Result.new(result.fields, result.to_a)
    end

Also aliased as: exec_without_stmt
exec_update(sql, name, binds) click to toggle source
Alias for: exec_delete
exec_without_stmt(sql, name = 'SQL', binds = []) click to toggle source
Alias for: exec_query
execute(sql, name = nil) click to toggle source

Executes the SQL statement in the context of this connection.

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 206
    def execute(sql, name = nil)
      if @connection
        # 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
      end

      super
    end

explain(arel, binds = []) click to toggle source

DATABASE STATEMENTS ======================================

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 97
    def explain(arel, binds = [])
      sql     = "EXPLAIN #{to_sql(arel, binds.dup)}"
      start   = Time.now
      result  = exec_query(sql, 'EXPLAIN', binds)
      elapsed = Time.now - start

      ExplainPrettyPrinter.new.pp(result, elapsed)
    end

insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 229
    def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
      super
      id_value || @connection.last_id
    end

Also aliased as: create
last_inserted_id(result) click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 245
    def last_inserted_id(result)
      @connection.last_id
    end

quote_string(string) click to toggle source

QUOTING ==================================================

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 65
    def quote_string(string)
      @connection.escape(string)
    end

reconnect!() click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 76
    def reconnect!
      disconnect!
      connect
    end

reset!() click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 90
    def reset!
      disconnect!
      connect
    end

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

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

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 225
    def select(sql, name = nil, binds = [])
      exec_query(sql, name).to_a
    end

select_rows(sql, name = nil) click to toggle source

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

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 201
    def select_rows(sql, name = nil)
      execute(sql, name).to_a
    end

supports_explain?() click to toggle source

    # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 39
    def supports_explain?
      true
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.