Skip to Content Skip to Search

class ActiveRecord::ConnectionAdapters::Mysql2Adapter

Active Record MySQL2 Adapter

Inherits From

Constants

ADAPTER_NAME

"Mysql2"

ER_ACCESS_DENIED_ERROR

1045

ER_BAD_DB_ERROR

1049

ER_CONN_HOST_ERROR

2003

ER_DBACCESS_DENIED_ERROR

1044

ER_UNKNOWN_HOST_ERROR

2005

TYPE_MAP

Type::TypeMap.new.tap { |m| initialize_type_map(m) }

Public class methods

new(...)

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 55
def initialize(...)
  super

  @config[:flags] ||= 0

  if @config[:flags].kind_of? Array
    @config[:flags].push "FOUND_ROWS"
  else
    @config[:flags] |= ::Mysql2::Client::FOUND_ROWS
  end

  @connection_parameters ||= @config
end

new_client(config)

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 24
def new_client(config)
  ::Mysql2::Client.new(config)
rescue ::Mysql2::Error => error
  case error.error_number
  when ER_BAD_DB_ERROR
    raise ActiveRecord::NoDatabaseError.db_error(config[:database])
  when ER_DBACCESS_DENIED_ERROR, ER_ACCESS_DENIED_ERROR
    raise ActiveRecord::DatabaseConnectionError.username_error(config[:username])
  when ER_CONN_HOST_ERROR, ER_UNKNOWN_HOST_ERROR
    raise ActiveRecord::DatabaseConnectionError.hostname_error(config[:host])
  else
    raise ActiveRecord::ConnectionNotEstablished, error.message
  end
end

Public instance methods

active?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 115
def active?
  !!@raw_connection&.ping
end

connected?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 111
def connected?
  !(@raw_connection.nil? || @raw_connection.closed?)
end

disconnect!()

Permalink

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

Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 123
def disconnect!
  super
  @raw_connection&.close
  @raw_connection = nil
end

error_number(exception)

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 103
def error_number(exception)
  exception.error_number if exception.respond_to?(:error_number)
end

savepoint_errors_invalidate_transactions?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 85
def savepoint_errors_invalidate_transactions?
  true
end

supports_comments?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 73
def supports_comments?
  true
end

supports_comments_in_create?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 77
def supports_comments_in_create?
  true
end

supports_json?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 69
def supports_json?
  !mariadb? && database_version >= "5.7.8"
end

supports_lazy_transactions?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 89
def supports_lazy_transactions?
  true
end

supports_savepoints?()

Permalink
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 81
def supports_savepoints?
  true
end

Definition files