Skip to Content Skip to Search
Methods
A
D
E
N
Q
S
Included Modules

Constants

ADAPTER_NAME = "Mysql2"
 
ER_ACCESS_DENIED_ERROR = 1045
 
ER_BAD_DB_ERROR = 1049
 
ER_CONN_HOST_ERROR = 2003
 
ER_UNKNOWN_HOST_ERROR = 2005
 

Class Public methods

new(...)

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 44
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)

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 29
def new_client(config)
  Mysql2::Client.new(config)
rescue Mysql2::Error => error
  if error.error_number == ConnectionAdapters::Mysql2Adapter::ER_BAD_DB_ERROR
    raise ActiveRecord::NoDatabaseError.db_error(config[:database])
  elsif error.error_number == ConnectionAdapters::Mysql2Adapter::ER_ACCESS_DENIED_ERROR
    raise ActiveRecord::DatabaseConnectionError.username_error(config[:username])
  elsif [ConnectionAdapters::Mysql2Adapter::ER_CONN_HOST_ERROR, ConnectionAdapters::Mysql2Adapter::ER_UNKNOWN_HOST_ERROR].include?(error.error_number)
    raise ActiveRecord::DatabaseConnectionError.hostname_error(config[:host])
  else
    raise ActiveRecord::ConnectionNotEstablished, error.message
  end
end

Instance Public methods

active?()

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 111
def active?
  !!@raw_connection&.ping
end

disconnect!()

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

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

error_number(exception)

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

quote_string(string)

Quotes strings for use in SQL input.

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 101
def quote_string(string)
  with_raw_connection(allow_retry: true, uses_transaction: false) do |connection|
    connection.escape(string)
  end
end

savepoint_errors_invalidate_transactions?()

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 74
def savepoint_errors_invalidate_transactions?
  true
end

supports_comments?()

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 62
def supports_comments?
  true
end

supports_comments_in_create?()

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 66
def supports_comments_in_create?
  true
end

supports_json?()

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

supports_lazy_transactions?()

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 78
def supports_lazy_transactions?
  true
end

supports_savepoints?()

# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 70
def supports_savepoints?
  true
end