Let #c_async_query support a block syntax that yields the result; ensure async in progress is negated by #get_result

This commit is contained in:
Lourens Naude 2008-10-08 05:17:01 +01:00
parent 4e5967bd24
commit f57c2a6576
2 changed files with 23 additions and 32 deletions

View File

@ -861,23 +861,6 @@ static void validate_async_query( VALUE obj )
} }
} }
/*
static VALUE connection_dropped( VALUE obj )
{
MYSQL* m = GetHandler(obj);
if( (mysql_errno(m) == CR_SERVER_LOST ) || ( mysql_errno(m) == CR_SERVER_GONE_ERROR ) || ( mysql_errno(m) == ER_SERVER_SHUTDOWN) ){
return Qtrue;
}else{
return Qfalse;
}
}
*/
static void async_reconnect( VALUE obj )
{
connect(obj);
}
static VALUE simulate_disconnect( VALUE obj ) static VALUE simulate_disconnect( VALUE obj )
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
@ -900,7 +883,6 @@ static VALUE send_query(VALUE obj, VALUE sql)
if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0){ if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0){
mysql_raise(m); mysql_raise(m);
/*( connection_dropped(obj) == Qtrue ) ? async_reconnect(obj) : mysql_raise(m);*/
} }
async_in_progress_set( obj, Qtrue ); async_in_progress_set( obj, Qtrue );
@ -911,14 +893,15 @@ static VALUE send_query(VALUE obj, VALUE sql)
static VALUE get_result(VALUE obj) static VALUE get_result(VALUE obj)
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
async_in_progress_set( obj, Qfalse );
if (GetMysqlStruct(obj)->connection == Qfalse) { if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected"); rb_raise(eMysql, "query: not connected");
} }
if (mysql_read_query_result(m) != 0) if (mysql_read_query_result(m) != 0)
mysql_raise(m); mysql_raise(m);
async_in_progress_set( obj, Qfalse );
if (GetMysqlStruct(obj)->query_with_result == Qfalse) if (GetMysqlStruct(obj)->query_with_result == Qfalse)
return obj; return obj;
if (mysql_field_count(m) == 0) if (mysql_field_count(m) == 0)
@ -946,22 +929,23 @@ static void schedule_query(VALUE obj, VALUE timeout)
/* async_query(sql,timeout=nil) */ /* async_query(sql,timeout=nil) */
static VALUE async_query(int argc, VALUE* argv, VALUE obj) static VALUE async_query(int argc, VALUE* argv, VALUE obj)
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
VALUE sql, timeout; VALUE sql, timeout;
rb_scan_args(argc, argv, "11", &sql, &timeout); rb_scan_args(argc, argv, "11", &sql, &timeout);
async_in_progress_set( obj, Qfalse );
send_query(obj,sql);
if (GetMysqlStruct(obj)->query_with_result == Qfalse){
async_in_progress_set( obj, Qfalse ); async_in_progress_set( obj, Qfalse );
return obj;
} else { send_query(obj,sql);
schedule_query(obj, timeout); schedule_query(obj, timeout);
return get_result(obj);
} if (rb_block_given_p()) {
rb_yield( get_result(obj) );
return obj;
}else{
return get_result(obj);
}
} }
#if MYSQL_VERSION_ID >= 40100 #if MYSQL_VERSION_ID >= 40100

View File

@ -0,0 +1,7 @@
require File.dirname(__FILE__) + '/test_helper'
m = Mysql.real_connect('localhost','root')
m.c_async_query( 'SELECT SLEEP(1)' ) do |result|
puts result.inspect
end