Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created September 14, 2008 14:29
Show Gist options
  • Save methodmissing/10731 to your computer and use it in GitHub Desktop.
Save methodmissing/10731 to your computer and use it in GitHub Desktop.
diff --git a/ext/mysql.c b/ext/mysql.c
index b804723..472f0eb 100644
--- a/ext/mysql.c
+++ b/ext/mysql.c
@@ -61,6 +61,7 @@ struct mysql {
char connection;
char query_with_result;
char blocking;
+ char query_sent;
};
struct mysql_res {
@@ -231,6 +232,17 @@ static VALUE init(VALUE klass)
return obj;
}
+static void optimize_for_async( struct mysql* myp )
+{
+ my_bool was_blocking;
+
+ vio_blocking(myp->handler.net.vio, 0, &was_blocking);
+ myp->blocking = vio_is_blocking( myp->handler.net.vio );
+
+ vio_fastsend( myp->handler.net.vio );
+ myp->query_sent = Qfalse;
+}
+
/* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
static VALUE real_connect(int argc, VALUE* argv, VALUE klass)
{
@@ -270,12 +282,7 @@ static VALUE real_connect(int argc, VALUE* argv, VALUE klass)
myp->handler.reconnect = 0;
myp->connection = Qtrue;
- my_bool was_blocking;
-
- vio_blocking(myp->handler.net.vio, 0, &was_blocking);
- myp->blocking = vio_is_blocking( myp->handler.net.vio );
-
- vio_fastsend( myp->handler.net.vio );
+ optimize_for_async(myp);
myp->query_with_result = Qtrue;
rb_obj_call_init(obj, argc, argv);
@@ -341,7 +348,7 @@ static VALUE real_connect2(int argc, VALUE* argv, VALUE obj)
mysql_raise(m);
m->reconnect = 0;
GetMysqlStruct(obj)->connection = Qtrue;
-
+ optimize_for_async(m);
return obj;
}
@@ -798,13 +805,18 @@ static VALUE send_query(VALUE obj, VALUE sql)
{
MYSQL* m = GetHandler(obj);
+ if( GetMysqlStruct(obj)->query_sent == Qtrue && m->last_used_con == m ){
+ rb_raise(eMysql, "Query out of sequence: Each call to Mysql#send_query requires a successive Mysql#get_result.");
+ }
+
Check_Type(sql, T_STRING);
if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected");
}
- if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
- mysql_raise(m);
- return Qnil;
+ if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
+ mysql_raise(m);
+ GetMysqlStruct(obj)->query_sent = Qtrue;
+ return Qnil;
}
/* get_result */
@@ -814,12 +826,15 @@ static VALUE get_result(VALUE obj)
if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected");
}
- if (mysql_read_query_result(m) != 0)
- mysql_raise(m);
+ if (mysql_read_query_result(m) != 0)
+ mysql_raise(m);
if (GetMysqlStruct(obj)->query_with_result == Qfalse)
return obj;
if (mysql_field_count(m) == 0)
return Qnil;
+ if ( m == m->last_used_con ){
+ GetMysqlStruct(obj)->query_sent = Qfalse;
+ }
return store_result(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment