Class: EventMachine::MySQL
- Inherits:
-
Object
- Object
- EventMachine::MySQL
- Defined in:
- lib/em-mysqlplus/mysql.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #close ⇒ Object
- #connect(opts) ⇒ Object
-
#connect_socket(opts) ⇒ Object
stolen from sequel.
- #debug(data) ⇒ Object
-
#initialize(opts) ⇒ MySQL
constructor
A new instance of MySQL.
-
#method_missing(method, *args, &blk) ⇒ Object
behave as a normal mysql connection.
- #query(sql, &blk) ⇒ Object (also: #real_query)
Constructor Details
#initialize(opts) ⇒ MySQL
Returns a new instance of MySQL.
12 13 14 15 16 17 18 19 |
# File 'lib/em-mysqlplus/mysql.rb', line 12 def initialize(opts) unless EM.respond_to?(:watch) and Mysql.method_defined?(:socket) raise RuntimeError, 'mysqlplus and EM.watch are required for EventedMysql' end @settings = { :debug => false }.merge!(opts) @connection = connect(@settings) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
behave as a normal mysql connection
37 38 39 |
# File 'lib/em-mysqlplus/mysql.rb', line 37 def method_missing(method, *args, &blk) @connection.send(method, *args) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/em-mysqlplus/mysql.rb', line 10 def connection @connection end |
Instance Method Details
#close ⇒ Object
21 22 23 |
# File 'lib/em-mysqlplus/mysql.rb', line 21 def close @connection.close end |
#connect(opts) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/em-mysqlplus/mysql.rb', line 41 def connect(opts) if conn = connect_socket(opts) debug [:connect, conn.socket, opts] EM.watch(conn.socket, EventMachine::MySQLConnection, conn, opts, self) else # invokes :errback callback in opts before firing again debug [:reconnect] EM.add_timer(5) { connect opts } end end |
#connect_socket(opts) ⇒ Object
stolen from sequel
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/em-mysqlplus/mysql.rb', line 53 def connect_socket(opts) conn = Mysql.init # set encoding _before_ connecting if charset = opts[:charset] || opts[:encoding] conn.(Mysql::SET_CHARSET_NAME, charset) end conn.(Mysql::OPT_LOCAL_INFILE, 'client') conn.real_connect( opts[:host] || 'localhost', opts[:user] || 'root', opts[:password], opts[:database], opts[:port], opts[:socket], 0 + # XXX multi results require multiple callbacks to parse # Mysql::CLIENT_MULTI_RESULTS + # Mysql::CLIENT_MULTI_STATEMENTS + (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS) ) # increase timeout so mysql server doesn't disconnect us # this is especially bad if we're disconnected while EM.attach is # still in progress, because by the time it gets to EM, the FD is # no longer valid, and it throws a c++ 'bad file descriptor' error # (do not use a timeout of -1 for unlimited, it does not work on mysqld > 5.0.60) conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}") # we handle reconnecting (and reattaching the new fd to EM) conn.reconnect = false # By default, MySQL 'where id is null' selects the last inserted id # Turn this off. http://dev.rubyonrails.org/ticket/6778 conn.query("set SQL_AUTO_IS_NULL=0") # get results for queries conn.query_with_result = true conn rescue Mysql::Error => e if cb = opts[:errback] cb.call(e) nil else raise e end end |
#debug(data) ⇒ Object
103 104 105 |
# File 'lib/em-mysqlplus/mysql.rb', line 103 def debug(data) p data if @settings[:debug] end |
#query(sql, &blk) ⇒ Object Also known as: real_query
25 26 27 28 29 30 31 32 33 |
# File 'lib/em-mysqlplus/mysql.rb', line 25 def query(sql, &blk) df = EventMachine::DefaultDeferrable.new cb = blk || Proc.new { |r| df.succeed(r) } eb = Proc.new { |r| df.fail(r) } @connection.execute(sql, cb, eb) df end |