class ActiveRecord::ConnectionAdapters::Mysql2Adapter

Constants

ADAPTER_NAME

Public Class Methods

new(connection, logger, connection_options, config) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 33
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 71
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 83
def disconnect!
  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 59
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 237
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) click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 233
def exec_insert(sql, name, binds)
  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 214
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 206
def execute(sql, name = nil)
  # 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

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

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

# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 97
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
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 227
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 243
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 65
def quote_string(string)
  @connection.escape(string)
end
reconnect!() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 76
def reconnect!
  disconnect!
  connect
end
reset!() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 90
def reset!
  disconnect!
  connect
end
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 223
def select(sql, name = nil, binds = [])
  exec_query(sql, name).to_a
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 201
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 39
def supports_explain?
  true
end

Private Instance Methods

configure_connection() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 254
def configure_connection
  @connection.query_options.merge!(:as => :array)

  # By default, MySQL 'where id is null' selects the last inserted id.
  # Turn this off. http://dev.rubyonrails.org/ticket/6778
  variable_assignments = ['SQL_AUTO_IS_NULL=0']
  encoding = @config[:encoding]

  # make sure we set the encoding
  variable_assignments << "NAMES '#{encoding}'" if encoding

  # increase timeout so mysql server doesn't disconnect us
  wait_timeout = @config[:wait_timeout]
  wait_timeout = 2147483 unless wait_timeout.is_a?(Fixnum)
  variable_assignments << "@@wait_timeout = #{wait_timeout}"

  execute("SET #{variable_assignments.join(', ')}", :skip_logging)
end
connect() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 249
def connect
  @connection = Mysql2::Client.new(@config)
  configure_connection
end
version() click to toggle source
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 273
def version
  @version ||= @connection.info[:version].scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
end