Class: Sequel::Mysql2::Synchrony::Database

Inherits:
Database
  • Object
show all
Defined in:
lib/my-sequel-synchrony/adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Database

Returns a new instance of Database.



17
18
19
20
21
22
23
24
# File 'lib/my-sequel-synchrony/adapter.rb', line 17

def initialize(opts={})
  pool_class = if opts.include?(:servers)
    ::Sequel::Mysql2::Synchrony::ShardedConnectionPool
  else
    ::Sequel::Mysql2::Synchrony::ConnectionPool
  end
  super(opts.merge(:pool_class => pool_class, :connection_handling => :queue))
end

Instance Method Details

#connect(server) ⇒ Object

Connect to the database. Similar to the Sequel::Mysql2#connect method, but uses the eventmachine client.

NOTE: this code is mostly a copy of Sequel::Mysql2#connect() method, with the only difference being the connection class (Mysql::EM::Client instead of Mysql2::Client). This code could be removed if the parent class accepted a :use_eventmachine option.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/my-sequel-synchrony/adapter.rb', line 32

def connect(server)
  opts = server_opts(server)
  opts[:host] ||= 'localhost'
  opts[:username] ||= opts[:user]
  opts[:flags] = ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS)
  conn = ::Mysql2::EM::Client.new(opts)
  conn.query_options.merge!(:symbolize_keys=>true, :cache_rows=>false)

  sqls = mysql_connection_setting_sqls

  # Set encoding a slightly different way after connecting,
  # in case the READ_DEFAULT_GROUP overrode the provided encoding.
  # Doesn't work across implicit reconnects, but Sequel doesn't turn on
  # that feature.
  if encoding = opts[:encoding] || opts[:charset]
    sqls.unshift("SET NAMES #{conn.escape(encoding.to_s)}")
  end

  sqls.each{|sql| log_yield(sql){conn.query(sql)}}

  add_prepared_statements_cache(conn)
  conn
end