### Home Classes Methods

### In Files

- lib/active_record/connection_adapters/mysql_adapter.rb

### Parent

ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter

### Namespace

- CLASS ActiveRecord::ConnectionAdapters::MysqlAdapter::StatementPool

### Methods

- ::new
- #active?
- #clear_cache!
- #client_encoding
- #disconnect!
- #exec_delete
- #exec_query
- #exec_update
- #execute_and_free
- #last_inserted_id
- #reconnect!
- #reset!
- #select_rows
- #supports_statement_cache?
- #type_cast

### 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::MysqlAdapter

The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with the faster C-based MySQL/Ruby adapter (available both as a gem and from www.tmtm.org/en/mysql/ruby/).

Options:

- `:host` - Defaults to "localhost".

- `:port` - Defaults to 3306.

- `:socket` - Defaults to "/tmp/mysql.sock".

- `:username` - Defaults to "root"

- `:password` - Defaults to nothing.

- `:database` - The name of the database. No default, must be provided.

- `:encoding` - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.

- `:reconnect` - Defaults to false (See MySQL documentation: dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).

- `:sslca` - Necessary to use MySQL with an SSL connection.

- `:sslkey` - Necessary to use MySQL with an SSL connection.

- `:sslcert` - Necessary to use MySQL with an SSL connection.

- `:sslcapath` - Necessary to use MySQL with an SSL connection.

- `:sslcipher` - Necessary to use MySQL with an SSL connection.

### Constants

ADAPTER_NAME
ENCODINGS

  Taken from here:

      https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb

  Author: TOMITA Masahiro <tommy@tmtm.org>

### Public Class Methods

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 126
    def initialize(connection, logger, connection_options, config)
      super
      @statements = StatementPool.new(@connection,
                                      config.fetch(:statement_limit) { 1000 })
      @client_encoding = nil
      connect
    end

### Public Instance Methods

active?() click to toggle source

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 175
    def active?
      if @connection.respond_to?(:stat)
        @connection.stat
      else
        @connection.query 'select 1'
      end

      # mysql-ruby doesn't raise an exception when stat fails.
      if @connection.respond_to?(:errno)
        @connection.errno.zero?
      else
        true
      end
    rescue Mysql::Error
      false
    end

clear_cache!() click to toggle source

Clears the prepared statements cache.

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 223
    def clear_cache!
      @statements.clear
    end

client_encoding() click to toggle source

Get the client encoding for this database

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 275
    def client_encoding
      return @client_encoding if @client_encoding

      result = exec_query(
        "SHOW VARIABLES WHERE Variable_name = 'character_set_client'",
        'SCHEMA')
      @client_encoding = ENCODINGS[result.rows.last.last]
    end

disconnect!() click to toggle source

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 200
    def disconnect!
      @connection.close rescue nil
    end

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 336
    def exec_delete(sql, name, binds)
      affected_rows = 0

      exec_query(sql, name, binds) do |n|
        affected_rows = n
      end

      affected_rows
    end

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 284
    def exec_query(sql, name = 'SQL', binds = [])
      # If the configuration sets prepared_statements:false, binds will
      # always be empty, since the bind variables will have been already
      # substituted and removed from binds by BindVisitor, so this will
      # effectively disable prepared statement usage completely.
      if binds.empty?
        result_set, affected_rows = exec_without_stmt(sql, name)
      else
        result_set, affected_rows = exec_stmt(sql, name, binds)
      end

      yield affected_rows if block_given?

      result_set
    end

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 323
    def execute_and_free(sql, name = nil)
      result = execute(sql, name)
      ret = yield result
      result.free
      ret
    end

last_inserted_id(result) click to toggle source

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 300
    def last_inserted_id(result)
      @connection.insert_id
    end

reconnect!() click to toggle source

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 192
    def reconnect!
      disconnect!
      clear_cache!
      connect
    end

reset!() click to toggle source

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 204
    def reset!
      if @connection.respond_to?(:change_user)
        # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
        # reset the connection is to change the user to the same user.
        @connection.change_user(@config[:username], @config[:password], @config[:database])
        configure_connection
      end
    end

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

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 215
    def select_rows(sql, name = nil)
      @connection.query_with_result = true
      rows = exec_query(sql, name).rows
      @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
      rows
    end

supports_statement_cache?() click to toggle source

Returns true, since this connection adapter supports prepared statement caching.

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 136
    def supports_statement_cache?
      true
    end

type_cast(value, column) click to toggle source

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

    # File lib/active_record/connection_adapters/mysql_adapter.rb, line 163
    def type_cast(value, column)
      return super unless value == true || value == false

      value ? 1 : 0
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.