From 9b59478cf677144bef257ee65be627aab9de927d Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Sun, 4 Apr 2010 23:31:32 -0700 Subject: [PATCH] allow access to file descriptor in C, EventMachine anyone? ;) --- ext/mysql2_ext.c | 6 ++++++ ext/mysql2_ext.h | 1 + spec/mysql2/client_spec.rb | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/ext/mysql2_ext.c b/ext/mysql2_ext.c index 43c5f86..450ecf0 100644 --- a/ext/mysql2_ext.c +++ b/ext/mysql2_ext.c @@ -166,6 +166,11 @@ static VALUE rb_mysql_client_server_info(VALUE self) { return version; } +static VALUE rb_mysql_client_socket(VALUE self) { + MYSQL * client = GetMysql2Client(self, client);; + return INT2NUM(client->net.fd); +} + /* Mysql2::Result */ static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) { VALUE obj; @@ -361,6 +366,7 @@ void Init_mysql2_ext() { rb_define_method(cMysql2Client, "escape", rb_mysql_client_escape, 1); rb_define_method(cMysql2Client, "info", rb_mysql_client_info, 0); rb_define_method(cMysql2Client, "server_info", rb_mysql_client_server_info, 0); + rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 0); cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject); rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1); diff --git a/ext/mysql2_ext.h b/ext/mysql2_ext.h index 1dee73a..18460bc 100644 --- a/ext/mysql2_ext.h +++ b/ext/mysql2_ext.h @@ -24,6 +24,7 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE query); static VALUE rb_mysql_client_escape(VALUE self, VALUE str); static VALUE rb_mysql_client_info(VALUE self); static VALUE rb_mysql_client_server_info(VALUE self); +static VALUE rb_mysql_client_socket(VALUE self); void rb_mysql_client_free(void * client); /* Mysql2::Result */ diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index 07b8949..4af7b27 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -48,4 +48,13 @@ describe Mysql2::Client do server_info.should have_key(:version) server_info[:version].class.should eql(String) end + + it "should respond to #socket" do + @client.should respond_to :socket + end + + it "#socket should return a Fixnum (file descriptor from C)" do + @client.socket.class.should eql(Fixnum) + @client.socket.should_not eql(0) + end end \ No newline at end of file