class ActiveRecord::ConnectionAdapters::Mysql2Adapter

Constants

ADAPTER_NAME
MAX_INDEX_LENGTH_FOR_UTF8MB4

Public Class Methods

new(connection, logger, connection_options, config) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 35
def initialize(connection, logger, connection_options, config)
  super
  @visitor = BindSubstitution.new self
  configure_connection
end

Public Instance Methods

active?() click to toggle source

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

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 82
def active?
  return false unless @connection
  @connection.ping
end
create(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
Alias for: insert_sql
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 96
def disconnect!
  super
  unless @connection.nil?
    @connection.close
    @connection = nil
  end
end
error_number(exception) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 70
def error_number(exception)
  exception.error_number if exception.respond_to?(:error_number)
end
exec_delete(sql, name, binds) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 248
def exec_delete(sql, name, binds)
  execute to_sql(sql, binds), name
  @connection.affected_rows
end
Also aliased as: exec_update
exec_insert(sql, name, binds, pk = nil, sequence_name = nil) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 244
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
  execute to_sql(sql, binds), name
end
exec_query(sql, name = 'SQL', binds = []) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 225
def exec_query(sql, name = 'SQL', binds = [])
  result = execute(sql, name)
  ActiveRecord::Result.new(result.fields, result.to_a)
end
Also aliased as: exec_without_stmt
exec_update(sql, name, binds)
Alias for: exec_delete
exec_without_stmt(sql, name = 'SQL', binds = [])
Alias for: exec_query
execute(sql, name = nil) click to toggle source

Executes the SQL statement in the context of this connection.

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 215
def execute(sql, name = nil)
  if @connection
    # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
    # made since we established the connection
    @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
  end

  super
end
explain(arel, binds = []) click to toggle source

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

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 106
def explain(arel, binds = [])
  sql     = "EXPLAIN #{to_sql(arel, binds.dup)}"
  start   = Time.now
  result  = exec_query(sql, 'EXPLAIN', binds)
  elapsed = Time.now - start

  ExplainPrettyPrinter.new.pp(result, elapsed)
end
initialize_schema_migrations_table() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 42
def initialize_schema_migrations_table
  if @config[:encoding] == 'utf8mb4'
    ActiveRecord::SchemaMigration.create_table(MAX_INDEX_LENGTH_FOR_UTF8MB4)
  else
    ActiveRecord::SchemaMigration.create_table
  end
end
insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 238
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
  super
  id_value || @connection.last_id
end
Also aliased as: create
last_inserted_id(result) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 254
def last_inserted_id(result)
  @connection.last_id
end
quote_string(string) click to toggle source

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

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 76
def quote_string(string)
  @connection.escape(string)
end
reconnect!() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 87
def reconnect!
  super
  disconnect!
  connect
end
Also aliased as: reset!
reset!()
Alias for: reconnect!
select(sql, name = nil, binds = []) click to toggle source

Returns an array of record hashes with the column names as keys and column values as values.

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 234
def select(sql, name = nil, binds = [])
  exec_query(sql, name)
end
select_rows(sql, name = nil) click to toggle source

Returns an array of arrays containing the field values. Order is the same as that returned by columns.

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 210
def select_rows(sql, name = nil)
  execute(sql, name).to_a
end
supports_explain?() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 50
def supports_explain?
  true
end

Private Instance Methods

configure_connection() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 265
def configure_connection
  @connection.query_options.merge!(:as => :array)
  super
end
connect() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 260
def connect
  @connection = Mysql2::Client.new(@config)
  configure_connection
end
set_field_encoding(field_name) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 274
def set_field_encoding field_name
  field_name
end
version() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 270
def version
  @version ||= @connection.info[:version].scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
end