diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 9573e8f..f7783cd 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -225,6 +225,9 @@ static VALUE rb_mysql_client_async_result(VALUE self) { return Qnil; } + // we have a result, mark this connection inactive + rb_iv_set(self, "@active", Qfalse); + VALUE resultObj = rb_mysql_result_to_obj(result); // pass-through query options for result construction later rb_iv_set(resultObj, "@query_options", rb_obj_dup(rb_iv_get(self, "@query_options"))); @@ -240,13 +243,22 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) { fd_set fdset; int fd, retval; int async = 0; - VALUE opts, defaults; + VALUE opts, defaults, active; MYSQL *client; Data_Get_Struct(self, MYSQL, client); REQUIRE_OPEN_DB(client); args.mysql = client; + active = rb_iv_get(self, "@active"); + // see if this connection is still waiting on a result from a previous query + if (NIL_P(active) || active == Qfalse) { + // mark this connection active + rb_iv_set(self, "@active", Qtrue); + } else { + rb_raise(cMysql2Error, "This connection is still waiting for a result, try again once you have the result"); + } + defaults = rb_iv_get(self, "@query_options"); if (rb_scan_args(argc, argv, "11", &args.sql, &opts) == 2) { opts = rb_funcall(defaults, intern_merge, 1, opts);