Permalink
Browse files

add support for synchronous commands (list_dbs, list_tables, quote, etc)

  • Loading branch information...
1 parent b32512a commit 1035f9670cc572ce84e8ca4092fb43b449cbd8fb @igrigorik committed Mar 12, 2010
Showing with 34 additions and 20 deletions.
  1. +14 −8 lib/em-mysql/connection.rb
  2. +4 −6 lib/em-mysql/mysql.rb
  3. +3 −5 spec/helper.rb
  4. +13 −1 spec/mysql_spec.rb
View
22 lib/em-mysql/connection.rb 100755 → 100644
@@ -115,20 +115,26 @@ def execute(sql, cblk = nil, eblk = nil, retries = 0)
@current = [sql, cblk, eblk, retries]
end
-
+
+ # mysql gem has syncronous methods such as list_dbs
+ # and others which require that we execute without callbacks
+ def method_missing(method, *args, &blk)
+ @mysql.send(method, *args, &blk) if @mysql.respond_to? method
+ end
+
def close
@connected = false
detach
end
private
- def next_query
- if @connected and !@processing and pending = @queue.shift
- sql, cblk, eblk = pending
- execute(sql, cblk, eblk)
+ def next_query
+ if @connected and !@processing and pending = @queue.shift
+ sql, cblk, eblk = pending
+ execute(sql, cblk, eblk)
+ end
end
- end
-
+
end
-end
+end
View
@@ -33,11 +33,9 @@ def query(sql, &blk)
end
alias :real_query :query
- # behave as a normal mysql connection
- def method_missing(method, *args, &block)
- if @connection.respond_to? method
- @connection.send(method, args)
- end
+ # behave as a normal mysql connection
+ def method_missing(method, *args, &blk)
+ @connection.send(method, *args)
end
def connect(opts)
@@ -69,7 +67,7 @@ def connect_socket(opts)
opts[:port],
opts[:socket],
0 +
- # XXX multi results require multiple callbacks to parse
+ # XXX multi results require multiple callbacks to parse
# Mysql::CLIENT_MULTI_RESULTS +
# Mysql::CLIENT_MULTI_STATEMENTS +
(opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
View
@@ -1,7 +1,5 @@
-require "spec"
+require "rubygems"
require "eventmachine"
-require "lib/em-mysql"
-
-# helpers
-# http://github.com/hedgehog/em-spec/blob/master/lib/em-spec/rspec.rb
+require "spec"
+require "lib/em-mysql"
View
@@ -76,7 +76,7 @@
}
}
end
-
+
it "should continue processing queries after hitting an error" do
EventMachine.run {
conn = EventMachine::MySQL.new(:host => 'localhost')
@@ -89,6 +89,18 @@
}
end
+ it "should work with synchronous commands" do
+ EventMachine.run {
+ conn = EventMachine::MySQL.new(:host => 'localhost', :database => 'test')
+
+ conn.list_dbs.class.should == Array
+ conn.list_tables.class.should == Array
+ conn.quote("select '1'").should == "select \\'1\\'"
+
+ EventMachine.stop
+ }
+ end
+
# it "should reconnect when disconnected" do
# # to test, run:
# # mysqladmin5 -u root kill `mysqladmin -u root processlist | grep "select sleep(5)" | cut -d'|' -f2`

0 comments on commit 1035f96

Please sign in to comment.