class ActiveRecord::ConnectionAdapters::Mysql2Adapter

Active Record MySQL2 Adapter

Constants

ADAPTER_NAME
ER_ACCESS_DENIED_ERROR
ER_BAD_DB_ERROR
ER_CONN_HOST_ERROR
ER_DBACCESS_DENIED_ERROR
ER_UNKNOWN_HOST_ERROR
TYPE_MAP

Public Class Methods

new(...) click to toggle source
# File 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
new_client(config) click to toggle source
# File 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

Private Class Methods

initialize_type_map(m) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 40
def initialize_type_map(m)
  super

  m.register_type(%r(char)i) do |sql_type|
    limit = extract_limit(sql_type)
    Type.lookup(:string, adapter: :mysql2, limit: limit)
  end

  m.register_type %r(^enum)i, Type.lookup(:string, adapter: :mysql2)
  m.register_type %r(^set)i,  Type.lookup(:string, adapter: :mysql2)
end

Public Instance Methods

active?() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 108
def active?
  connected? && @lock.synchronize { @raw_connection&.ping } || false
end
connected?() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 104
def connected?
  !(@raw_connection.nil? || @raw_connection.closed?)
end
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 116
def disconnect!
  @lock.synchronize do
    super
    @raw_connection&.close
    @raw_connection = nil
  end
end
error_number(exception) click to toggle source

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

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

Private Instance Methods

configure_connection() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 151
def configure_connection
  @raw_connection.query_options[:as] = :array
  @raw_connection.query_options[:database_timezone] = default_timezone
  super
end
connect() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 137
def connect
  @raw_connection = self.class.new_client(@connection_parameters)
rescue ConnectionNotEstablished => ex
  raise ex.set_pool(@pool)
end
default_prepared_statements() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 179
def default_prepared_statements
  false
end
full_version() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 157
def full_version
  database_version.full_version_string
end
get_full_version() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 161
def get_full_version
  any_raw_connection.server_info[:version]
end
reconnect() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 143
def reconnect
  @lock.synchronize do
    @raw_connection&.close
    @raw_connection = nil
    connect
  end
end
text_type?(type) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 133
def text_type?(type)
  TYPE_MAP.lookup(type).is_a?(Type::String) || TYPE_MAP.lookup(type).is_a?(Type::Text)
end
translate_exception(exception, message:, sql:, binds:) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 165
def translate_exception(exception, message:, sql:, binds:)
  if exception.is_a?(::Mysql2::Error::TimeoutError) && !exception.error_number
    ActiveRecord::AdapterTimeout.new(message, sql: sql, binds: binds, connection_pool: @pool)
  elsif exception.is_a?(::Mysql2::Error::ConnectionError)
    if exception.message.match?(/MySQL client is not connected/i)
      ActiveRecord::ConnectionNotEstablished.new(exception, connection_pool: @pool)
    else
      ActiveRecord::ConnectionFailed.new(message, sql: sql, binds: binds, connection_pool: @pool)
    end
  else
    super
  end
end