2010-03-30 16:56:24 +00:00
|
|
|
#include "mysql2_ext.h"
|
2010-03-26 08:03:08 +00:00
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
/* Mysql2::Client */
|
2010-03-26 08:03:08 +00:00
|
|
|
static VALUE rb_mysql_client_new(VALUE klass) {
|
|
|
|
MYSQL * client;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
obj = Data_Make_Struct(klass, MYSQL, NULL, rb_mysql_client_free, client);
|
|
|
|
|
|
|
|
if (!mysql_init(client)) {
|
|
|
|
// TODO: warning - not enough memory?
|
|
|
|
rb_raise(rb_eStandardError, "%s", mysql_error(client));
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_options(client, MYSQL_SET_CHARSET_NAME, "utf8") != 0) {
|
|
|
|
// TODO: warning - unable to set charset
|
|
|
|
rb_warn("%s", mysql_error(client));
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK
|
|
|
|
if (!mysql_real_connect(client, "localhost", "root", NULL, NULL, 0, NULL, 0)) {
|
|
|
|
// unable to connect
|
|
|
|
rb_raise(rb_eStandardError, "%s", mysql_error(client));
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
// HACK
|
|
|
|
|
|
|
|
rb_obj_call_init(obj, 0, NULL);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_mysql_client_init(VALUE self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rb_mysql_client_free(void * client) {
|
|
|
|
MYSQL * c = client;
|
|
|
|
if (c) {
|
|
|
|
mysql_close(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
|
|
|
|
MYSQL * client;
|
|
|
|
MYSQL_RES * result = NULL;
|
|
|
|
int query;
|
|
|
|
Check_Type(sql, T_STRING);
|
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
GetMysql2Client(self, client);
|
2010-03-26 08:03:08 +00:00
|
|
|
query = mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql));
|
|
|
|
if (query != 0) {
|
|
|
|
// lookup error code and msg, raise exception
|
2010-03-30 23:41:02 +00:00
|
|
|
fprintf(stdout, "Error: %s\n", mysql_error(client));
|
|
|
|
return Qnil;
|
2010-03-26 08:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = mysql_store_result(client);
|
|
|
|
if (result == NULL) {
|
|
|
|
// lookup error code and msg, raise exception
|
2010-03-30 23:41:02 +00:00
|
|
|
fprintf(stdout, "Error: %s\n", mysql_error(client));
|
|
|
|
return Qnil;
|
2010-03-26 08:03:08 +00:00
|
|
|
}
|
|
|
|
return rb_mysql_result_to_obj(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
/* Mysql2::Result */
|
2010-03-26 08:03:08 +00:00
|
|
|
static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
|
|
|
|
VALUE obj;
|
2010-03-30 16:56:24 +00:00
|
|
|
obj = Data_Wrap_Struct(cMysql2Result, 0, rb_mysql_result_free, r);
|
2010-03-26 08:03:08 +00:00
|
|
|
rb_obj_call_init(obj, 0, NULL);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rb_mysql_result_free(void * result) {
|
|
|
|
MYSQL_RES * r = result;
|
|
|
|
if (r) {
|
|
|
|
mysql_free_result(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
|
|
|
|
VALUE rowHash, opts, block;
|
2010-03-26 08:03:08 +00:00
|
|
|
MYSQL_RES * result;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_FIELD * fields;
|
2010-03-30 16:56:24 +00:00
|
|
|
unsigned int i = 0, numFields = 0, symbolizeKeys = 0;
|
2010-03-26 08:03:08 +00:00
|
|
|
unsigned long * fieldLengths;
|
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
GetMysql2Result(self, result);
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
|
|
|
|
Check_Type(opts, T_HASH);
|
|
|
|
if (rb_hash_aref(opts, sym_symbolize_keys) == Qtrue) {
|
|
|
|
symbolizeKeys = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-03-26 08:03:08 +00:00
|
|
|
|
|
|
|
row = mysql_fetch_row(result);
|
|
|
|
if (row == NULL) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
numFields = mysql_num_fields(result);
|
|
|
|
fieldLengths = mysql_fetch_lengths(result);
|
|
|
|
fields = mysql_fetch_fields(result);
|
|
|
|
|
|
|
|
rowHash = rb_hash_new();
|
|
|
|
for (i = 0; i < numFields; i++) {
|
2010-03-30 16:56:24 +00:00
|
|
|
VALUE key;
|
|
|
|
if (symbolizeKeys) {
|
|
|
|
char buf[fields[i].name_length+1];
|
|
|
|
memcpy(buf, fields[i].name, fields[i].name_length);
|
|
|
|
buf[fields[i].name_length] = 0;
|
|
|
|
key = ID2SYM(rb_intern(buf));
|
|
|
|
} else {
|
|
|
|
key = rb_str_new(fields[i].name, fields[i].name_length);
|
|
|
|
}
|
2010-03-26 08:03:08 +00:00
|
|
|
if (row[i]) {
|
|
|
|
rb_hash_aset(rowHash, key, Qnil);
|
|
|
|
} else {
|
|
|
|
rb_hash_aset(rowHash, key, Qnil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rowHash;
|
|
|
|
}
|
|
|
|
|
2010-03-30 16:14:27 +00:00
|
|
|
static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
|
2010-03-30 16:56:24 +00:00
|
|
|
VALUE dataset, opts, block;
|
2010-03-26 08:03:08 +00:00
|
|
|
MYSQL_RES * result;
|
|
|
|
unsigned long numRows, i;
|
2010-03-30 16:14:27 +00:00
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
GetMysql2Result(self, result);
|
2010-03-30 16:14:27 +00:00
|
|
|
|
2010-03-30 16:56:24 +00:00
|
|
|
rb_scan_args(argc, argv, "01&", &opts, &block);
|
2010-03-30 16:14:27 +00:00
|
|
|
|
2010-03-26 08:03:08 +00:00
|
|
|
numRows = mysql_num_rows(result);
|
|
|
|
if (numRows == 0) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-03-30 16:14:27 +00:00
|
|
|
|
|
|
|
// TODO: allow yielding datasets of configurable size
|
|
|
|
// like find_in_batches from AR...
|
|
|
|
if (block != Qnil) {
|
|
|
|
for (i = 0; i < numRows; i++) {
|
2010-03-30 16:56:24 +00:00
|
|
|
VALUE row = rb_mysql_result_fetch_row(argc, argv, self);
|
2010-03-30 16:14:27 +00:00
|
|
|
if (row == Qnil) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
rb_yield(row);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dataset = rb_ary_new2(numRows);
|
|
|
|
for (i = 0; i < numRows; i++) {
|
2010-03-30 16:56:24 +00:00
|
|
|
VALUE row = rb_mysql_result_fetch_row(argc, argv, self);
|
2010-03-30 16:14:27 +00:00
|
|
|
if (row == Qnil) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
rb_ary_store(dataset, i, row);
|
2010-03-26 08:03:08 +00:00
|
|
|
}
|
2010-03-30 16:14:27 +00:00
|
|
|
return dataset;
|
2010-03-26 08:03:08 +00:00
|
|
|
}
|
2010-03-30 16:14:27 +00:00
|
|
|
return Qnil;
|
2010-03-26 08:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ruby Extension initializer */
|
2010-03-30 16:56:24 +00:00
|
|
|
void Init_mysql2_ext() {
|
|
|
|
mMysql2 = rb_define_module("Mysql2");
|
|
|
|
|
|
|
|
cMysql2Client = rb_define_class_under(mMysql2, "Client", rb_cObject);
|
|
|
|
rb_define_singleton_method(cMysql2Client, "new", rb_mysql_client_new, 0);
|
|
|
|
rb_define_method(cMysql2Client, "initialize", rb_mysql_client_init, 0);
|
|
|
|
rb_define_method(cMysql2Client, "query", rb_mysql_client_query, 1);
|
|
|
|
|
|
|
|
cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject);
|
|
|
|
rb_define_method(cMysql2Result, "fetch_row", rb_mysql_result_fetch_row, -1);
|
|
|
|
rb_define_method(cMysql2Result, "fetch_rows", rb_mysql_result_fetch_rows, -1);
|
|
|
|
rb_define_method(cMysql2Result, "each", rb_mysql_result_fetch_rows, -1);
|
|
|
|
|
|
|
|
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
|
2010-03-26 08:03:08 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_RUBY_ENCODING_H
|
|
|
|
utf8Encoding = rb_enc_find_index("UTF-8");
|
|
|
|
#endif
|
|
|
|
}
|