From a1d10c140b2010aa657ec7a895780a10ec8d5aea Mon Sep 17 00:00:00 2001 From: Roger Pack Date: Wed, 1 Oct 2008 16:30:45 -0600 Subject: [PATCH 1/5] take out some compiler warnings --- ext/mysql.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/mysql.c b/ext/mysql.c index b804723..d388791 100644 --- a/ext/mysql.c +++ b/ext/mysql.c @@ -1128,7 +1128,7 @@ static VALUE process_all_hashes(VALUE obj, VALUE with_table, int build_array, in { MYSQL_RES* res = GetMysqlRes(obj); unsigned int n = mysql_num_fields(res); - VALUE ary; + VALUE ary = Qnil; if(build_array) ary = rb_ary_new(); MYSQL_ROW row = mysql_fetch_row(res); // grab one off the top, to determine the rows @@ -1195,6 +1195,8 @@ static VALUE process_all_hashes(VALUE obj, VALUE with_table, int build_array, in if(yield) return obj; + + return Qnil; /* we should never get here -- this takes out a compiler warning */ } /* fetch_hash2 (internal) */ From 1af96063beebf45c3ba3a276f9e7078d2f9c98f0 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Tue, 18 Nov 2008 18:56:40 +0100 Subject: [PATCH 2/5] Fix compilation on Ruby 1.9. --- ext/mysql.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/mysql.c b/ext/mysql.c index d388791..02c2f62 100644 --- a/ext/mysql.c +++ b/ext/mysql.c @@ -1580,12 +1580,12 @@ static VALUE stmt_execute(int argc, VALUE *argv, VALUE obj) s->param.bind[i].buffer = &(s->param.buffer[i]); t.second_part = 0; t.neg = 0; - t.second = FIX2INT(RARRAY(a)->ptr[0]); - t.minute = FIX2INT(RARRAY(a)->ptr[1]); - t.hour = FIX2INT(RARRAY(a)->ptr[2]); - t.day = FIX2INT(RARRAY(a)->ptr[3]); - t.month = FIX2INT(RARRAY(a)->ptr[4]); - t.year = FIX2INT(RARRAY(a)->ptr[5]); + t.second = FIX2INT(rb_ary_entry(a, 0)); + t.minute = FIX2INT(rb_ary_entry(a, 1)); + t.hour = FIX2INT(rb_ary_entry(a, 2)); + t.day = FIX2INT(rb_ary_entry(a, 3)); + t.month = FIX2INT(rb_ary_entry(a, 4)); + t.year = FIX2INT(rb_ary_entry(a, 5)); *(MYSQL_TIME*)&(s->param.buffer[i]) = t; } else if (CLASS_OF(argv[i]) == cMysqlTime) { MYSQL_TIME t; From 8a758f77e3ad4f55f4b3f6d9d0b17d3b5d294254 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Tue, 18 Nov 2008 19:18:51 +0100 Subject: [PATCH 3/5] When in Ruby 1.9, release the global interpreter lock while performing a query, so that other threads can still run. --- ext/mysql.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/ext/mysql.c b/ext/mysql.c index 02c2f62..aa8631d 100644 --- a/ext/mysql.c +++ b/ext/mysql.c @@ -719,18 +719,43 @@ static VALUE use_result(VALUE obj) } static VALUE res_free(VALUE); + +typedef struct { + MYSQL *m; + const char *data; + unsigned long len; +} QueryArgs; + +static VALUE blocking_query(void *data) +{ + QueryArgs *args = (QueryArgs *) data; + return (VALUE) mysql_real_query(args->m, args->data, args->len); +} + /* query(sql) */ static VALUE query(VALUE obj, VALUE sql) { int loop = 0; MYSQL* m = GetHandler(obj); + QueryArgs args; + int result; + Check_Type(sql, T_STRING); if (GetMysqlStruct(obj)->connection == Qfalse) { rb_raise(eMysql, "query: not connected"); } if (rb_block_given_p()) { - if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) + #ifdef RUBY_VM + args.m = m; + args.data = RSTRING_PTR(sql); + args.len = RSTRING_LEN(sql); + result = (int) rb_thread_blocking_region(blocking_query, &args, RUBY_UBF_PROCESS, 0); + #else + result = mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)); + #endif + if (result != 0) mysql_raise(m); + do { MYSQL_RES* res = mysql_store_result(m); if (res == NULL) { @@ -749,7 +774,16 @@ static VALUE query(VALUE obj, VALUE sql) #endif return obj; } - if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) + + #ifdef RUBY_VM + args.m = m; + args.data = RSTRING_PTR(sql); + args.len = RSTRING_LEN(sql); + result = (int) rb_thread_blocking_region(blocking_query, &args, RUBY_UBF_PROCESS, 0); + #else + result = mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)); + #endif + if (result != 0) mysql_raise(m); if (GetMysqlStruct(obj)->query_with_result == Qfalse) return obj; From dcb1e10c164302cb05eebe8a8c652bd6428d74b3 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Tue, 18 Nov 2008 19:23:31 +0100 Subject: [PATCH 4/5] Fix various compiler warnings. --- ext/mysql.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ext/mysql.c b/ext/mysql.c index aa8631d..6292991 100644 --- a/ext/mysql.c +++ b/ext/mysql.c @@ -803,7 +803,7 @@ static VALUE socket_type(VALUE obj) { MYSQL* m = GetHandler(obj); VALUE description = vio_description( m->net.vio ); - return NILorSTRING( description ); + return (VALUE) NILorSTRING( description ); } /* blocking */ @@ -857,7 +857,7 @@ static VALUE get_result(VALUE obj) return store_result(obj); } -static VALUE schedule(VALUE obj, VALUE timeout) +static void schedule(VALUE obj, VALUE timeout) { MYSQL* m = GetHandler(obj); fd_set read; @@ -872,13 +872,11 @@ static VALUE schedule(VALUE obj, VALUE timeout) if (rb_thread_select(m->net.fd + 1, &read, NULL, NULL, &tv) < 0) { rb_raise(eMysql, "query: timeout"); } - } /* async_query(sql,timeout=nil) */ static VALUE async_query(int argc, VALUE* argv, VALUE obj) { - MYSQL* m = GetHandler(obj); VALUE sql, timeout; rb_scan_args(argc, argv, "11", &sql, &timeout); From 9d884870226278027842db7e7479ea78bc897e75 Mon Sep 17 00:00:00 2001 From: Roger Pack Date: Sat, 20 Dec 2008 22:59:35 +0000 Subject: [PATCH 5/5] add test for when a db isn't there --- test/test_all_hashes.rb | 1 + test/test_failure.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/test_failure.rb diff --git a/test/test_all_hashes.rb b/test/test_all_hashes.rb index b9b5119..3581c04 100644 --- a/test/test_all_hashes.rb +++ b/test/test_all_hashes.rb @@ -2,6 +2,7 @@ # run it by substiting in a 'long' [many row] query for the query variable and toggling use_all_hashes here at the top # note that we load all the rows first, then run .all_hashes on the result [to see more easily the effect of all hashes] # on my machine and a 200_000 row table, it took 3.38s versus 3.65s +require 'rubygems' require 'mysqlplus' use_the_all_hashes_method = true diff --git a/test/test_failure.rb b/test/test_failure.rb new file mode 100644 index 0000000..824534f --- /dev/null +++ b/test/test_failure.rb @@ -0,0 +1,21 @@ +# shows the effect of using .all_hashes instead of looping on each hash +# run it by substiting in a 'long' [many row] query for the query variable and toggling use_all_hashes here at the top +# note that we load all the rows first, then run .all_hashes on the result [to see more easily the effect of all hashes] +# on my machine and a 200_000 row table, it took 3.38s versus 3.65s +require 'rubygems' +require 'mysqlplus' +begin + Mysql.real_connect('fakehost','root', '', 'local_leadgen_dev') +rescue Mysql::Error +end +begin + Mysql.real_connect('localhost','root', '', 'faketable') +rescue Mysql::Error +end +begin + Mysql.real_connect('localhost', 'root', 'pass', 'db', 3307)# bad port +rescue Mysql::Error +end +print "pass" + +