From c2fd214cb4c79faa84c1563f3e2d782b2a26b6cd Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 5 Apr 2010 20:51:59 -0700 Subject: [PATCH] convert over to mysql_send_query/mysql_read_query_result non-blocking API --- ext/mysql2_ext.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ext/mysql2_ext.c b/ext/mysql2_ext.c index cebd5a4..1e10430 100644 --- a/ext/mysql2_ext.c +++ b/ext/mysql2_ext.c @@ -105,10 +105,38 @@ void rb_mysql_client_free(void * client) { static VALUE rb_mysql_client_query(VALUE self, VALUE sql) { MYSQL * client; MYSQL_RES * result; + fd_set fdset; + int fd, retval; Check_Type(sql, T_STRING); GetMysql2Client(self, client); - if (mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) { + if (mysql_send_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) { + rb_raise(cMysql2Error, "%s", mysql_error(client)); + return Qnil; + } + + // the below code is largely from do_mysql + fd = client->net.fd; + for(;;) { + FD_ZERO(&fdset); + FD_SET(fd, &fdset); + + retval = rb_thread_select(fd + 1, &fdset, NULL, NULL, NULL); + + if (retval < 0) { + rb_sys_fail(0); + } + + if (retval == 0) { + continue; + } + + if (client->status == MYSQL_STATUS_READY) { + break; + } + } + + if (mysql_read_query_result(client) != 0) { rb_raise(cMysql2Error, "%s", mysql_error(client)); return Qnil; }