diff --git a/ext/mysql2_ext.c b/ext/mysql2_ext.c index 450ecf0..cebd5a4 100644 --- a/ext/mysql2_ext.c +++ b/ext/mysql2_ext.c @@ -59,31 +59,31 @@ static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) { if (!mysql_init(client)) { // TODO: warning - not enough memory? - rb_raise(rb_eStandardError, "%s", mysql_error(client)); + rb_raise(cMysql2Error, "%s", mysql_error(client)); return Qnil; } // set default reconnect behavior if (mysql_options(client, MYSQL_OPT_RECONNECT, &reconnect) != 0) { // TODO: warning - unable to set reconnect behavior - rb_warn("%s", mysql_error(client)); + rb_warn("%s\n", mysql_error(client)); } // set default connection timeout behavior if (connect_timeout != 0 && mysql_options(client, MYSQL_OPT_CONNECT_TIMEOUT, &connect_timeout) != 0) { // TODO: warning - unable to set connection timeout - rb_warn("%s", mysql_error(client)); + rb_warn("%s\n", mysql_error(client)); } // force the encoding to utf8 if (mysql_options(client, MYSQL_SET_CHARSET_NAME, "utf8") != 0) { // TODO: warning - unable to set charset - rb_warn("%s", mysql_error(client)); + rb_warn("%s\n", mysql_error(client)); } if (mysql_real_connect(client, host, username, password, database, port, socket, 0) == NULL) { // unable to connect - rb_raise(rb_eStandardError, "%s", mysql_error(client)); + rb_raise(cMysql2Error, "%s", mysql_error(client)); return Qnil; } @@ -109,15 +109,14 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE sql) { GetMysql2Client(self, client); if (mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) { - fprintf(stdout, "mysql_real_query error: %s\n", mysql_error(client)); + rb_raise(cMysql2Error, "%s", mysql_error(client)); return Qnil; } result = mysql_store_result(client); if (result == NULL) { if (mysql_field_count(client) != 0) { - // lookup error code and msg, raise exception - fprintf(stdout, "mysql_store_result error: (%d) %s\n", mysql_errno(client), mysql_error(client)); + rb_raise(cMysql2Error, "%s", mysql_error(client)); } return Qnil; } @@ -368,6 +367,8 @@ void Init_mysql2_ext() { rb_define_method(cMysql2Client, "server_info", rb_mysql_client_server_info, 0); rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 0); + cMysql2Error = rb_define_class_under(mMysql2, "Error", rb_eStandardError); + cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject); rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1); diff --git a/ext/mysql2_ext.h b/ext/mysql2_ext.h index 18460bc..5600ef4 100644 --- a/ext/mysql2_ext.h +++ b/ext/mysql2_ext.h @@ -14,6 +14,9 @@ int utf8Encoding, binaryEncoding; static VALUE cBigDecimal, cDate, cDateTime; ID intern_new, intern_local; +/* Mysql2::Error */ +VALUE cMysql2Error; + /* Mysql2::Client */ #define GetMysql2Client(obj, sval) (sval = (MYSQL*)DATA_PTR(obj)); static ID sym_socket, sym_host, sym_port, sym_username, sym_password, diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index 4af7b27..dd4e94d 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -57,4 +57,14 @@ describe Mysql2::Client do @client.socket.class.should eql(Fixnum) @client.socket.should_not eql(0) end + + it "should raise a Mysql2::Error exception upon connection failure" do + lambda { + bad_client = Mysql2::Client.new :host => "dfjhdi9wrhw", :username => 'asdfasdf8d2h' + }.should raise_error(Mysql2::Error) + + lambda { + good_client = Mysql2::Client.new + }.should_not raise_error(Mysql2::Error) + end end \ No newline at end of file diff --git a/spec/mysql2/result_spec.rb b/spec/mysql2/result_spec.rb index 21fac68..df0d8a4 100644 --- a/spec/mysql2/result_spec.rb +++ b/spec/mysql2/result_spec.rb @@ -18,6 +18,16 @@ describe Mysql2::Result do @result.should respond_to :each end + it "should raise a Mysql2::Error exception upon a bad query" do + lambda { + @client.query "bad sql" + }.should raise_error(Mysql2::Error) + + lambda { + @client.query "SELECT 1" + }.should_not raise_error(Mysql2::Error) + end + context "#each" do it "should yield rows as hash's" do @result.each do |row|