add Mysql2::Error exception type, use it
This commit is contained in:
parent
432a5fb766
commit
7bac2c520a
|
@ -59,31 +59,31 @@ static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) {
|
||||||
|
|
||||||
if (!mysql_init(client)) {
|
if (!mysql_init(client)) {
|
||||||
// TODO: warning - not enough memory?
|
// TODO: warning - not enough memory?
|
||||||
rb_raise(rb_eStandardError, "%s", mysql_error(client));
|
rb_raise(cMysql2Error, "%s", mysql_error(client));
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set default reconnect behavior
|
// set default reconnect behavior
|
||||||
if (mysql_options(client, MYSQL_OPT_RECONNECT, &reconnect) != 0) {
|
if (mysql_options(client, MYSQL_OPT_RECONNECT, &reconnect) != 0) {
|
||||||
// TODO: warning - unable to set reconnect behavior
|
// 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
|
// set default connection timeout behavior
|
||||||
if (connect_timeout != 0 && mysql_options(client, MYSQL_OPT_CONNECT_TIMEOUT, &connect_timeout) != 0) {
|
if (connect_timeout != 0 && mysql_options(client, MYSQL_OPT_CONNECT_TIMEOUT, &connect_timeout) != 0) {
|
||||||
// TODO: warning - unable to set connection timeout
|
// 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
|
// force the encoding to utf8
|
||||||
if (mysql_options(client, MYSQL_SET_CHARSET_NAME, "utf8") != 0) {
|
if (mysql_options(client, MYSQL_SET_CHARSET_NAME, "utf8") != 0) {
|
||||||
// TODO: warning - unable to set charset
|
// 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) {
|
if (mysql_real_connect(client, host, username, password, database, port, socket, 0) == NULL) {
|
||||||
// unable to connect
|
// unable to connect
|
||||||
rb_raise(rb_eStandardError, "%s", mysql_error(client));
|
rb_raise(cMysql2Error, "%s", mysql_error(client));
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,15 +109,14 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
|
||||||
|
|
||||||
GetMysql2Client(self, client);
|
GetMysql2Client(self, client);
|
||||||
if (mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) {
|
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;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = mysql_store_result(client);
|
result = mysql_store_result(client);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
if (mysql_field_count(client) != 0) {
|
if (mysql_field_count(client) != 0) {
|
||||||
// lookup error code and msg, raise exception
|
rb_raise(cMysql2Error, "%s", mysql_error(client));
|
||||||
fprintf(stdout, "mysql_store_result error: (%d) %s\n", mysql_errno(client), mysql_error(client));
|
|
||||||
}
|
}
|
||||||
return Qnil;
|
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, "server_info", rb_mysql_client_server_info, 0);
|
||||||
rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 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);
|
cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject);
|
||||||
rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1);
|
rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1);
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,9 @@ int utf8Encoding, binaryEncoding;
|
||||||
static VALUE cBigDecimal, cDate, cDateTime;
|
static VALUE cBigDecimal, cDate, cDateTime;
|
||||||
ID intern_new, intern_local;
|
ID intern_new, intern_local;
|
||||||
|
|
||||||
|
/* Mysql2::Error */
|
||||||
|
VALUE cMysql2Error;
|
||||||
|
|
||||||
/* Mysql2::Client */
|
/* Mysql2::Client */
|
||||||
#define GetMysql2Client(obj, sval) (sval = (MYSQL*)DATA_PTR(obj));
|
#define GetMysql2Client(obj, sval) (sval = (MYSQL*)DATA_PTR(obj));
|
||||||
static ID sym_socket, sym_host, sym_port, sym_username, sym_password,
|
static ID sym_socket, sym_host, sym_port, sym_username, sym_password,
|
||||||
|
|
|
@ -57,4 +57,14 @@ describe Mysql2::Client do
|
||||||
@client.socket.class.should eql(Fixnum)
|
@client.socket.class.should eql(Fixnum)
|
||||||
@client.socket.should_not eql(0)
|
@client.socket.should_not eql(0)
|
||||||
end
|
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
|
end
|
|
@ -18,6 +18,16 @@ describe Mysql2::Result do
|
||||||
@result.should respond_to :each
|
@result.should respond_to :each
|
||||||
end
|
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
|
context "#each" do
|
||||||
it "should yield rows as hash's" do
|
it "should yield rows as hash's" do
|
||||||
@result.each do |row|
|
@result.each do |row|
|
||||||
|
|
Loading…
Reference in New Issue