make sure we raise exceptions early and often if the connection has been closed

This commit is contained in:
Brian Lopez 2010-05-12 23:05:33 -07:00
parent a577f09fb0
commit 69b9c587ad
2 changed files with 29 additions and 6 deletions

View File

@ -268,6 +268,10 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
Check_Type(args.sql, T_STRING); Check_Type(args.sql, T_STRING);
GetMysql2Client(self, args.mysql); GetMysql2Client(self, args.mysql);
if (!args.mysql) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
if (rb_thread_blocking_region(nogvl_send_query, &args, RUBY_UBF_IO, 0) == Qfalse) { if (rb_thread_blocking_region(nogvl_send_query, &args, RUBY_UBF_IO, 0) == Qfalse) {
rb_raise(cMysql2Error, "%s", mysql_error(args.mysql)); rb_raise(cMysql2Error, "%s", mysql_error(args.mysql));
return Qnil; return Qnil;
@ -308,7 +312,10 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
char escaped[(oldLen*2)+1]; char escaped[(oldLen*2)+1];
GetMysql2Client(self, client); GetMysql2Client(self, client);
if (!client) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
newLen = mysql_real_escape_string(client, escaped, RSTRING_PTR(str), RSTRING_LEN(str)); newLen = mysql_real_escape_string(client, escaped, RSTRING_PTR(str), RSTRING_LEN(str));
if (newLen == oldLen) { if (newLen == oldLen) {
// no need to return a new ruby string if nothing changed // no need to return a new ruby string if nothing changed
@ -334,6 +341,10 @@ static VALUE rb_mysql_client_server_info(VALUE self) {
VALUE version; VALUE version;
GetMysql2Client(self, client); GetMysql2Client(self, client);
if (!client) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
version = rb_hash_new(); version = rb_hash_new();
rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_server_version(client))); rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_server_version(client)));
rb_hash_aset(version, sym_version, rb_str_new2(mysql_get_server_info(client))); rb_hash_aset(version, sym_version, rb_str_new2(mysql_get_server_info(client)));
@ -341,7 +352,11 @@ static VALUE rb_mysql_client_server_info(VALUE self) {
} }
static VALUE rb_mysql_client_socket(VALUE self) { static VALUE rb_mysql_client_socket(VALUE self) {
MYSQL * client = GetMysql2Client(self, client);; MYSQL * client = GetMysql2Client(self, client);
if (!client) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
return INT2NUM(client->net.fd); return INT2NUM(client->net.fd);
} }
@ -369,7 +384,10 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
MYSQL * client; MYSQL * client;
MYSQL_RES * result; MYSQL_RES * result;
GetMysql2Client(self, client); GetMysql2Client(self, client);
if (!client) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
if (rb_thread_blocking_region(nogvl_read_query_result, client, RUBY_UBF_IO, 0) == Qfalse) { if (rb_thread_blocking_region(nogvl_read_query_result, client, RUBY_UBF_IO, 0) == Qfalse) {
rb_raise(cMysql2Error, "%s", mysql_error(client)); rb_raise(cMysql2Error, "%s", mysql_error(client));
return Qnil; return Qnil;
@ -389,14 +407,20 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
static VALUE rb_mysql_client_last_id(VALUE self) { static VALUE rb_mysql_client_last_id(VALUE self) {
MYSQL * client; MYSQL * client;
GetMysql2Client(self, client); GetMysql2Client(self, client);
if (!client) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
return ULL2NUM(mysql_insert_id(client)); return ULL2NUM(mysql_insert_id(client));
} }
static VALUE rb_mysql_client_affected_rows(VALUE self) { static VALUE rb_mysql_client_affected_rows(VALUE self) {
MYSQL * client; MYSQL * client;
GetMysql2Client(self, client); GetMysql2Client(self, client);
if (!client) {
rb_raise(cMysql2Error, "closed MySQL connection");
return Qnil;
}
return ULL2NUM(mysql_affected_rows(client)); return ULL2NUM(mysql_affected_rows(client));
} }

View File

@ -244,7 +244,6 @@ module ActiveRecord
def disconnect! def disconnect!
@connection.close @connection.close
@connection = nil
end end
def reset! def reset!