refactoring C to use the allocate function

This commit is contained in:
Aaron Patterson 2010-06-30 13:12:47 -07:00
parent 40f3a2c491
commit 8c96aa1fcd
2 changed files with 20 additions and 13 deletions

View File

@ -43,8 +43,21 @@ static VALUE nogvl_connect(void *ptr) {
return client ? Qtrue : Qfalse; return client ? Qtrue : Qfalse;
} }
static VALUE allocate(VALUE klass)
{
mysql2_client_wrapper * client;
return Data_Make_Struct(
klass,
mysql2_client_wrapper,
NULL,
rb_mysql_client_free,
client
);
}
/* Mysql2::Client */ /* Mysql2::Client */
static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) { static VALUE rb_mysql_client_init(int argc, VALUE * argv, VALUE self) {
mysql2_client_wrapper * client; mysql2_client_wrapper * client;
struct nogvl_connect_args args = { struct nogvl_connect_args args = {
.host = "localhost", .host = "localhost",
@ -55,7 +68,7 @@ static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) {
.unix_socket = NULL, .unix_socket = NULL,
.client_flag = 0 .client_flag = 0
}; };
VALUE obj, opts; VALUE opts;
VALUE rb_host, rb_socket, rb_port, rb_database, VALUE rb_host, rb_socket, rb_port, rb_database,
rb_username, rb_password, rb_reconnect, rb_username, rb_password, rb_reconnect,
rb_connect_timeout; rb_connect_timeout;
@ -66,7 +79,8 @@ static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) {
unsigned int connect_timeout = 0; unsigned int connect_timeout = 0;
my_bool reconnect = 1; my_bool reconnect = 1;
obj = Data_Make_Struct(klass, mysql2_client_wrapper, NULL, rb_mysql_client_free, client); /* FIXME: refactor this to not use mysql2_client_wrapper */
Data_Get_Struct(self, mysql2_client_wrapper, client);
if (rb_scan_args(argc, argv, "01", &opts) == 1) { if (rb_scan_args(argc, argv, "01", &opts) == 1) {
Check_Type(opts, T_HASH); Check_Type(opts, T_HASH);
@ -171,11 +185,6 @@ static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) {
client->client = args.mysql; client->client = args.mysql;
rb_obj_call_init(obj, argc, argv);
return obj;
}
static VALUE rb_mysql_client_init(RB_MYSQL_UNUSED int argc, RB_MYSQL_UNUSED VALUE * argv, VALUE self) {
return self; return self;
} }
@ -751,9 +760,10 @@ void Init_mysql2() {
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime")); cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));
VALUE mMysql2 = rb_define_module("Mysql2"); VALUE mMysql2 = rb_define_module("Mysql2");
VALUE cMysql2Client = rb_define_class_under(mMysql2, "Client", rb_cObject); VALUE cMysql2Client = rb_define_class_under(mMysql2, "Client", rb_cObject);
rb_define_singleton_method(cMysql2Client, "new", rb_mysql_client_new, -1);
rb_define_alloc_func(cMysql2Client, allocate);
rb_define_method(cMysql2Client, "initialize", rb_mysql_client_init, -1); rb_define_method(cMysql2Client, "initialize", rb_mysql_client_init, -1);
rb_define_method(cMysql2Client, "close", rb_mysql_client_close, 0); rb_define_method(cMysql2Client, "close", rb_mysql_client_close, 0);
rb_define_method(cMysql2Client, "query", rb_mysql_client_query, -1); rb_define_method(cMysql2Client, "query", rb_mysql_client_query, -1);

View File

@ -39,7 +39,6 @@ static ID sym_socket, sym_host, sym_port, sym_username, sym_password,
sym_database, sym_reconnect, sym_connect_timeout, sym_id, sym_version, sym_database, sym_reconnect, sym_connect_timeout, sym_id, sym_version,
sym_sslkey, sym_sslcert, sym_sslca, sym_sslcapath, sym_sslcipher, sym_sslkey, sym_sslcert, sym_sslca, sym_sslcapath, sym_sslcipher,
sym_symbolize_keys, sym_async; sym_symbolize_keys, sym_async;
static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass);
static VALUE rb_mysql_client_init(int argc, VALUE * argv, VALUE self); static VALUE rb_mysql_client_init(int argc, VALUE * argv, VALUE self);
static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self); static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self);
static VALUE rb_mysql_client_escape(VALUE self, VALUE str); static VALUE rb_mysql_client_escape(VALUE self, VALUE str);
@ -72,8 +71,6 @@ static void rb_mysql_result_free_result(mysql2_result_wrapper * wrapper);
/* Mysql2::Error */ /* Mysql2::Error */
static VALUE rb_raise_mysql2_error(MYSQL *client); static VALUE rb_raise_mysql2_error(MYSQL *client);
static VALUE rb_mysql_error_error_number(VALUE obj);
static VALUE rb_mysql_error_sql_state(VALUE obj);
/* /*
* used to pass all arguments to mysql_real_connect while inside * used to pass all arguments to mysql_real_connect while inside