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:
parent
4e5967bd24
commit
f57c2a6576
32
ext/mysql.c
32
ext/mysql.c
@ -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)
|
||||||
@ -955,11 +938,12 @@ static VALUE async_query(int argc, VALUE* argv, VALUE obj)
|
|||||||
|
|
||||||
send_query(obj,sql);
|
send_query(obj,sql);
|
||||||
|
|
||||||
if (GetMysqlStruct(obj)->query_with_result == Qfalse){
|
|
||||||
async_in_progress_set( obj, Qfalse );
|
|
||||||
return obj;
|
|
||||||
} else {
|
|
||||||
schedule_query(obj, timeout);
|
schedule_query(obj, timeout);
|
||||||
|
|
||||||
|
if (rb_block_given_p()) {
|
||||||
|
rb_yield( get_result(obj) );
|
||||||
|
return obj;
|
||||||
|
}else{
|
||||||
return get_result(obj);
|
return get_result(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
test/async_query_with_block_test.rb
Normal file
7
test/async_query_with_block_test.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user