Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/em_mysql2_adapter.rb

Class Method Summary collapse

Class Method Details

.em_mysql2_connection(config) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_record/connection_adapters/em_mysql2_adapter.rb', line 14

def self.em_mysql2_connection(config)
  client = EM::Synchrony::ActiveRecord::ConnectionPool.new(size: config[:pool]) do
    conn = ActiveRecord::ConnectionAdapters::EMMysql2Adapter::Client.new(config.symbolize_keys)
    # From Mysql2Adapter#configure_connection
    conn.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]
    variable_assignments << "NAMES '#{encoding}'" if encoding

    wait_timeout = config[:wait_timeout]
    wait_timeout = 2592000 unless wait_timeout.is_a?(Fixnum)
    variable_assignments << "@@wait_timeout = #{wait_timeout}"

    conn.query("SET #{variable_assignments.join(', ')}")
    conn
  end 
  options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
  ActiveRecord::ConnectionAdapters::EMMysql2Adapter.new(client, logger, options, config)
end