Skip to Content Skip to Search

class ActiveRecord::ConnectionAdapters::Mysql2Adapter

Active Record MySQL2 Adapter

Inherits From

Constants

"Mysql2"
1045
1049
2003
1044
2005
Type::TypeMap.new.tap { |m| initialize_type_map(m) }

Public class methods

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

  @affected_rows_before_warnings = nil
  @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
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

Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 108
def active?
  connected? && @lock.synchronize { @raw_connection&.ping } || false
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 104
def connected?
  !(@raw_connection.nil? || @raw_connection.closed?)
end

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 116
def disconnect!
  @lock.synchronize do
    super
    @raw_connection&.close
    @raw_connection = nil
  end
end

HELPER METHODS ===========================================

Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 96
def error_number(exception)
  exception.error_number if exception.respond_to?(:error_number)
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 86
def savepoint_errors_invalidate_transactions?
  true
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 74
def supports_comments?
  true
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 78
def supports_comments_in_create?
  true
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 70
def supports_json?
  !mariadb? && database_version >= "5.7.8"
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 90
def supports_lazy_transactions?
  true
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb, line 82
def supports_savepoints?
  true
end

Definition files