diff --git a/README.rdoc b/README.rdoc index d571a9d..0c3ca12 100644 --- a/README.rdoc +++ b/README.rdoc @@ -31,6 +31,14 @@ MySQL connector for Ruby. stmt = my.prepare('insert into tblname (col1,col2) values (?,?)') stmt.execute 123, 'abc' +== Use with {Celluloid::IO}[https://github.com/celluloid/celluloid-io] + +You just need to override Mysql default socket classes with +Celluloid's ones. Then use it as you normally do. + + Mysql.unixsocket_class = Celluloid::IO::UNIXSocket + Mysql.tcpsocket_class = Celluloid::IO::TCPSocket + == Incompatible with MySQL/Ruby 2.8.x * Ruby 1.8.x ではシフトJISのような安全でないマルチバイト文字セットに対して Mysql#escape_string を使用すると例外が発生します。 diff --git a/lib/mysql.rb b/lib/mysql.rb index 4aa788c..ad268d5 100644 --- a/lib/mysql.rb +++ b/lib/mysql.rb @@ -21,6 +21,20 @@ class Mysql rescue LoadError end + ## + # Make socket classes configurable, so we can be compatible + # with amazing Celluloid::IO (https://github.com/celluloid/celluloid-io) + # + @@tcpsocket_class = TCPSocket + def self.tcpsocket_class; @@tcpsocket_class end + def self.tcpsocket_class=(value); @@tcpsocket_class = value end + + @@unixsocket_class = UNIXSocket + def self.unixsocket_class; @@unixsocket_class end + def self.unixsocket_class=(value); @@unixsocket_class = value end + # + ## + VERSION = 20911 # Version number of this library MYSQL_UNIX_PORT = "/tmp/mysql.sock" # UNIX domain socket filename MYSQL_TCP_PORT = 3306 # TCP socket port number diff --git a/lib/mysql/protocol.rb b/lib/mysql/protocol.rb index 2db1011..ffce035 100644 --- a/lib/mysql/protocol.rb +++ b/lib/mysql/protocol.rb @@ -168,10 +168,10 @@ def initialize(host, port, socket, conn_timeout, read_timeout, write_timeout) Timeout.timeout conn_timeout do if host.nil? or host.empty? or host == "localhost" socket ||= ENV["MYSQL_UNIX_PORT"] || MYSQL_UNIX_PORT - @sock = UNIXSocket.new socket + @sock = Mysql.unixsocket_class.new socket else port ||= ENV["MYSQL_TCP_PORT"] || (Socket.getservbyname("mysql","tcp") rescue MYSQL_TCP_PORT) - @sock = TCPSocket.new host, port + @sock = Mysql.tcpsocket_class.new host, port end end rescue Timeout::Error