Skip to content
Browse files

basic query syntax w/ deferrables working

  • Loading branch information...
1 parent 455ad03 commit 0547957fe3e825d35a571b3ba0371131c1c6373f @igrigorik committed Feb 13, 2010
Showing with 53 additions and 22 deletions.
  1. +14 −20 lib/em-mysql/connection.rb
  2. +10 −1 lib/em-mysql/mysql.rb
  3. +29 −1 spec/mysql_spec.rb
View
34 lib/em-mysql/connection.rb
@@ -9,7 +9,17 @@ def result
module EventMachine
class MySQLConnection < EventMachine::Connection
- def initialize mysql, opts
+
+ attr_reader :processing, :connected, :opts
+ alias :settings :opts
+
+ DisconnectErrors = [
+ 'query: not connected',
+ 'MySQL server has gone away',
+ 'Lost connection to MySQL server during query'
+ ] unless defined? DisconnectErrors
+
+ def initialize(mysql, opts)
@mysql = mysql
@fd = mysql.socket
@opts = opts
@@ -23,14 +33,6 @@ def initialize mysql, opts
self.notify_readable = true
EM.add_timer(0){ next_query }
end
- attr_reader :processing, :connected, :opts
- alias :settings :opts
-
- DisconnectErrors = [
- 'query: not connected',
- 'MySQL server has gone away',
- 'Lost connection to MySQL server during query'
- ] unless defined? DisconnectErrors
def notify_readable
log 'readable'
@@ -93,7 +95,6 @@ def notify_readable
def unbind
log 'mysql disconnect', $!, *($! ? $!.backtrace[0..5] : [])
- # cp = EventedMysql.instance_variable_get('@connection_pool') and cp.delete(self)
@connected = false
# XXX wait for the next tick until the current fd is removed completely from the reactor
@@ -103,6 +104,9 @@ def unbind
#
# XXX do _NOT_ use EM.next_tick here. if a bunch of sockets disconnect at the same time, we want
# XXX reconnects to happen after all the unbinds have been processed
+
+ # TODO: reconnect logic will / is broken with new code structure
+
EM.add_timer(0) do
log 'mysql reconnecting'
@processing = false
@@ -123,16 +127,6 @@ def execute sql, response = nil, cblk = nil, eblk = nil, retries = 0, &blk
begin
unless @processing or !@connected
- # begin
- # log 'mysql ping', @mysql.ping
- # # log 'mysql stat', @mysql.stat
- # # log 'mysql errno', @mysql.errno
- # rescue
- # log 'mysql ping failed'
- # @@queue << [response, sql, blk]
- # return close
- # end
-
@processing = true
log 'mysql sending', sql
View
11 lib/em-mysql/mysql.rb
@@ -22,6 +22,16 @@ def close
@connection.close
end
+ def execute(sql)
+ df = EventMachine::DefaultDeferrable.new
+ cb = Proc.new { |r| df.succeed(r) }
+ eb = Proc.new { |r| df.fail(r) }
+
+ @connection.execute(sql, :select, cb, eb)
+
+ df
+ end
+
private
def connect(opts)
@@ -45,7 +55,6 @@ def connect_socket(opts)
end
conn.options(Mysql::OPT_LOCAL_INFILE, 'client')
-
conn.real_connect(
opts[:host] || 'localhost',
opts[:user] || 'root',
View
30 spec/mysql_spec.rb
@@ -14,8 +14,36 @@
}
end
+ it "should invoke errback on connection failure" do
+ EventMachine.run {
+ lambda {
+ conn = EventMachine::MySQL.new({
+ :host => 'localhost',
+ :port => 20000,
+ :socket => '',
+ :errback => Proc.new {
+ EventMachine.stop
+ }
+ })
+ }.should_not raise_error
+ }
+ end
+
+ it "should execute sql" do
+ EventMachine.run {
+ conn = EventMachine::MySQL.new(:host => 'localhost')
+ query = conn.execute("select 1")
+ query.callback { |res|
+ p res
+ EventMachine.stop
+ }
+
+# EventMachine.stop
+ }
+ end
+
# it "should reconnect when disconnected"
- # it "should execute sql"
+
# it "run select queries and return results"
# it "queue up queries and execute them in order"
# it "have raw mode which yields the mysql object"

0 comments on commit 0547957

Please sign in to comment.
Something went wrong with that request. Please try again.