diff --git a/ext/mysql2/mysql2_ext.c b/ext/mysql2/mysql2_ext.c index 7bba561..b19d20a 100644 --- a/ext/mysql2/mysql2_ext.c +++ b/ext/mysql2/mysql2_ext.c @@ -736,18 +736,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) { return wrapper->rows; } -static VALUE rb_mysql_error_error_number(VALUE obj) { - return rb_iv_get(obj, "error_number"); -} - -static VALUE rb_mysql_error_sql_state(VALUE obj) { - return rb_iv_get(obj, "sql_state"); -} - static VALUE rb_raise_mysql2_error(MYSQL *client) { VALUE e = rb_exc_new2(cMysql2Error, mysql_error(client)); - rb_iv_set(e, "error_number", INT2FIX(mysql_errno(client))); - rb_iv_set(e, "sql_state", rb_tainted_str_new2(mysql_sqlstate(client))); + rb_funcall(e, rb_intern("error_number="), 1, INT2NUM(mysql_errno(client))); + rb_funcall(e, rb_intern("sql_state="), 1, rb_tainted_str_new2(mysql_sqlstate(client))); rb_exc_raise(e); return Qnil; } @@ -773,9 +765,7 @@ void Init_mysql2() { rb_define_method(cMysql2Client, "last_id", rb_mysql_client_last_id, 0); rb_define_method(cMysql2Client, "affected_rows", rb_mysql_client_affected_rows, 0); - cMysql2Error = rb_define_class_under(mMysql2, "Error", rb_eStandardError); - rb_define_method(cMysql2Error, "error_number", rb_mysql_error_error_number, 0); - rb_define_method(cMysql2Error, "sql_state", rb_mysql_error_sql_state, 0); + cMysql2Error = rb_const_get(mMysql2, rb_intern("Error")); cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject); rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1); diff --git a/lib/mysql2.rb b/lib/mysql2.rb index 46cb481..26bb058 100644 --- a/lib/mysql2.rb +++ b/lib/mysql2.rb @@ -2,7 +2,8 @@ require 'date' require 'bigdecimal' -require 'mysql2.so' +require 'mysql2/error' +require 'mysql2/mysql2' # = Mysql2 # diff --git a/lib/mysql2/error.rb b/lib/mysql2/error.rb new file mode 100644 index 0000000..c14ae35 --- /dev/null +++ b/lib/mysql2/error.rb @@ -0,0 +1,11 @@ +module Mysql2 + class Error < StandardError + attr_accessor :error_number, :sql_state + + def initialize msg + super + @error_number = nil + @sql_state = nil + end + end +end diff --git a/mysql2.gemspec b/mysql2.gemspec index 081b373..3385563 100644 --- a/mysql2.gemspec +++ b/mysql2.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Brian Lopez"] - s.date = %q{2010-06-29} + s.date = %q{2010-06-30} s.email = %q{seniorlopez@gmail.com} s.extensions = ["ext/mysql2/extconf.rb"] s.extra_rdoc_files = [ @@ -36,6 +36,7 @@ Gem::Specification.new do |s| "lib/arel/engines/sql/compilers/mysql2_compiler.rb", "lib/mysql2.rb", "lib/mysql2/em.rb", + "lib/mysql2/error.rb", "lib/sequel/adapters/mysql2.rb", "mysql2.gemspec", "spec/active_record/active_record_spec.rb",