moving the error class to ruby

This commit is contained in:
Aaron Patterson 2010-06-30 10:10:59 -07:00
parent 925b1b13fd
commit 6ef738200d
4 changed files with 18 additions and 15 deletions

View File

@ -736,18 +736,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
return wrapper->rows; 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) { static VALUE rb_raise_mysql2_error(MYSQL *client) {
VALUE e = rb_exc_new2(cMysql2Error, mysql_error(client)); VALUE e = rb_exc_new2(cMysql2Error, mysql_error(client));
rb_iv_set(e, "error_number", INT2FIX(mysql_errno(client))); rb_funcall(e, rb_intern("error_number="), 1, INT2NUM(mysql_errno(client)));
rb_iv_set(e, "sql_state", rb_tainted_str_new2(mysql_sqlstate(client))); rb_funcall(e, rb_intern("sql_state="), 1, rb_tainted_str_new2(mysql_sqlstate(client)));
rb_exc_raise(e); rb_exc_raise(e);
return Qnil; 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, "last_id", rb_mysql_client_last_id, 0);
rb_define_method(cMysql2Client, "affected_rows", rb_mysql_client_affected_rows, 0); rb_define_method(cMysql2Client, "affected_rows", rb_mysql_client_affected_rows, 0);
cMysql2Error = rb_define_class_under(mMysql2, "Error", rb_eStandardError); cMysql2Error = rb_const_get(mMysql2, rb_intern("Error"));
rb_define_method(cMysql2Error, "error_number", rb_mysql_error_error_number, 0);
rb_define_method(cMysql2Error, "sql_state", rb_mysql_error_sql_state, 0);
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);

View File

@ -2,7 +2,8 @@
require 'date' require 'date'
require 'bigdecimal' require 'bigdecimal'
require 'mysql2.so' require 'mysql2/error'
require 'mysql2/mysql2'
# = Mysql2 # = Mysql2
# #

11
lib/mysql2/error.rb Normal file
View File

@ -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

View File

@ -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.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Brian Lopez"] s.authors = ["Brian Lopez"]
s.date = %q{2010-06-29} s.date = %q{2010-06-30}
s.email = %q{seniorlopez@gmail.com} s.email = %q{seniorlopez@gmail.com}
s.extensions = ["ext/mysql2/extconf.rb"] s.extensions = ["ext/mysql2/extconf.rb"]
s.extra_rdoc_files = [ s.extra_rdoc_files = [
@ -36,6 +36,7 @@ Gem::Specification.new do |s|
"lib/arel/engines/sql/compilers/mysql2_compiler.rb", "lib/arel/engines/sql/compilers/mysql2_compiler.rb",
"lib/mysql2.rb", "lib/mysql2.rb",
"lib/mysql2/em.rb", "lib/mysql2/em.rb",
"lib/mysql2/error.rb",
"lib/sequel/adapters/mysql2.rb", "lib/sequel/adapters/mysql2.rb",
"mysql2.gemspec", "mysql2.gemspec",
"spec/active_record/active_record_spec.rb", "spec/active_record/active_record_spec.rb",