result hash keys can be symbols using the :symbolize_keys option with #fetch_row, #fetch_rows or #each. Renamed extension from MySQL to Mysql2. Initial Rakefile and spec dir

This commit is contained in:
Brian Lopez 2010-03-30 09:56:24 -07:00
parent 63b55b6f97
commit 736c0d6cd3
8 changed files with 98 additions and 41 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
Makefile
*.dSYM *.dSYM
*.o *.o
*.bundle *.bundle

35
Rakefile Normal file
View File

@ -0,0 +1,35 @@
# encoding: UTF-8
# begin
# require 'jeweler'
# Jeweler::Tasks.new do |gem|
# gem.name = "yajl-ruby"
# gem.summary = "Ruby C bindings to the excellent Yajl JSON stream-based parser library."
# gem.email = "seniorlopez@gmail.com"
# gem.homepage = "http://github.com/brianmario/yajl-ruby"
# gem.authors = ["Brian Lopez", "Lloyd Hilaiel"]
# gem.require_paths = ["lib", "ext"]
# gem.extra_rdoc_files = `git ls-files *.rdoc`.split("\n")
# gem.files = `git ls-files`.split("\n")
# gem.extensions = ["ext/extconf.rb"]
# gem.files.include %w(lib/jeweler/templates/.document lib/jeweler/templates/.gitignore)
# gem.rubyforge_project = "yajl-ruby"
# end
# rescue LoadError
# puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler -s http://gems.github.com"
# end
require 'rake'
require 'spec/rake/spectask'
desc "Run all examples with RCov"
Spec::Rake::SpecTask.new('spec:rcov') do |t|
t.spec_files = FileList['spec/']
t.rcov = true
t.rcov_opts = lambda do
IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
end
end
Spec::Rake::SpecTask.new('spec') do |t|
t.spec_files = FileList['spec/']
t.spec_opts << '--options' << 'spec/spec.opts'
end

View File

@ -13,7 +13,7 @@ if have_library('mysqlclient')
$CFLAGS << ' -DRUBY_19_COMPATIBILITY' $CFLAGS << ' -DRUBY_19_COMPATIBILITY'
end end
create_makefile('mysql_duce_ext') create_makefile('mysql2_ext')
else else
puts 'libmysql not found, maybe try manually specifying --with-mysql-lib to find it?' puts 'libmysql not found, maybe try manually specifying --with-mysql-lib to find it?'
end end

View File

@ -1,6 +1,6 @@
#include "mysql_duce_ext.h" #include "mysql2_ext.h"
/* MySQL::Client */ /* Mysql2::Client */
static VALUE rb_mysql_client_new(VALUE klass) { static VALUE rb_mysql_client_new(VALUE klass) {
MYSQL * client; MYSQL * client;
VALUE obj; VALUE obj;
@ -47,7 +47,7 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
int query; int query;
Check_Type(sql, T_STRING); Check_Type(sql, T_STRING);
GetMySQLClient(self, client); GetMysql2Client(self, client);
query = mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql)); query = mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql));
if (query != 0) { if (query != 0) {
// lookup error code and msg, raise exception // lookup error code and msg, raise exception
@ -61,10 +61,10 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
} }
/* MySQL::Result */ /* Mysql2::Result */
static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) { static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
VALUE obj; VALUE obj;
obj = Data_Wrap_Struct(cMySQLResult, 0, rb_mysql_result_free, r); obj = Data_Wrap_Struct(cMysql2Result, 0, rb_mysql_result_free, r);
rb_obj_call_init(obj, 0, NULL); rb_obj_call_init(obj, 0, NULL);
return obj; return obj;
} }
@ -76,16 +76,22 @@ void rb_mysql_result_free(void * result) {
} }
} }
static VALUE rb_mysql_result_fetch_row(VALUE self) { static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
VALUE rowHash; VALUE rowHash, opts, block;
MYSQL_RES * result; MYSQL_RES * result;
MYSQL_ROW row; MYSQL_ROW row;
MYSQL_FIELD * fields; MYSQL_FIELD * fields;
unsigned int numFields; unsigned int i = 0, numFields = 0, symbolizeKeys = 0;
unsigned long * fieldLengths; unsigned long * fieldLengths;
unsigned int i;
GetMySQLResult(self, result); 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;
}
}
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
if (row == NULL) { if (row == NULL) {
@ -98,7 +104,15 @@ static VALUE rb_mysql_result_fetch_row(VALUE self) {
rowHash = rb_hash_new(); rowHash = rb_hash_new();
for (i = 0; i < numFields; i++) { for (i = 0; i < numFields; i++) {
VALUE key = rb_str_new(fields[i].name, fields[i].name_length); 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);
}
if (row[i]) { if (row[i]) {
rb_hash_aset(rowHash, key, Qnil); rb_hash_aset(rowHash, key, Qnil);
} else { } else {
@ -109,13 +123,13 @@ static VALUE rb_mysql_result_fetch_row(VALUE self) {
} }
static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) { static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
VALUE dataset, block; VALUE dataset, opts, block;
MYSQL_RES * result; MYSQL_RES * result;
unsigned long numRows, i; unsigned long numRows, i;
GetMySQLResult(self, result); GetMysql2Result(self, result);
rb_scan_args(argc, argv, "0&", &block); rb_scan_args(argc, argv, "01&", &opts, &block);
numRows = mysql_num_rows(result); numRows = mysql_num_rows(result);
if (numRows == 0) { if (numRows == 0) {
@ -126,7 +140,7 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
// like find_in_batches from AR... // like find_in_batches from AR...
if (block != Qnil) { if (block != Qnil) {
for (i = 0; i < numRows; i++) { for (i = 0; i < numRows; i++) {
VALUE row = rb_mysql_result_fetch_row(self); VALUE row = rb_mysql_result_fetch_row(argc, argv, self);
if (row == Qnil) { if (row == Qnil) {
return Qnil; return Qnil;
} }
@ -135,7 +149,7 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
} else { } else {
dataset = rb_ary_new2(numRows); dataset = rb_ary_new2(numRows);
for (i = 0; i < numRows; i++) { for (i = 0; i < numRows; i++) {
VALUE row = rb_mysql_result_fetch_row(self); VALUE row = rb_mysql_result_fetch_row(argc, argv, self);
if (row == Qnil) { if (row == Qnil) {
return Qnil; return Qnil;
} }
@ -147,18 +161,20 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
} }
/* Ruby Extension initializer */ /* Ruby Extension initializer */
void Init_mysql_duce_ext() { void Init_mysql2_ext() {
mMySQL = rb_define_module("MySQL"); mMysql2 = rb_define_module("Mysql2");
cMySQLClient = rb_define_class_under(mMySQL, "Client", rb_cObject); cMysql2Client = rb_define_class_under(mMysql2, "Client", rb_cObject);
rb_define_singleton_method(cMySQLClient, "new", rb_mysql_client_new, 0); rb_define_singleton_method(cMysql2Client, "new", rb_mysql_client_new, 0);
rb_define_method(cMySQLClient, "initialize", rb_mysql_client_init, 0); rb_define_method(cMysql2Client, "initialize", rb_mysql_client_init, 0);
rb_define_method(cMySQLClient, "query", rb_mysql_client_query, 1); rb_define_method(cMysql2Client, "query", rb_mysql_client_query, 1);
cMySQLResult = rb_define_class_under(mMySQL, "Result", rb_cObject); cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject);
rb_define_method(cMySQLResult, "fetch_row", rb_mysql_result_fetch_row, 0); rb_define_method(cMysql2Result, "fetch_row", rb_mysql_result_fetch_row, -1);
rb_define_method(cMySQLResult, "fetch_rows", rb_mysql_result_fetch_rows, -1); rb_define_method(cMysql2Result, "fetch_rows", rb_mysql_result_fetch_rows, -1);
rb_define_method(cMySQLResult, "each", 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"));
#ifdef HAVE_RUBY_ENCODING_H #ifdef HAVE_RUBY_ENCODING_H
utf8Encoding = rb_enc_find_index("UTF-8"); utf8Encoding = rb_enc_find_index("UTF-8");

View File

@ -5,26 +5,22 @@
#include <mysql/errmsg.h> #include <mysql/errmsg.h>
#include <mysql/mysqld_error.h> #include <mysql/mysqld_error.h>
#ifdef HAVE_RUBY_ENCODING_H /* Mysql2 */
#include <ruby/encoding.h> VALUE mMysql2;
int utf8Encoding;
#endif
/* MySQL */ /* Mysql2::Client */
VALUE mMySQL; #define GetMysql2Client(obj, sval) (sval = (MYSQL*)DATA_PTR(obj));
VALUE cMysql2Client;
/* MySQL::Client */
#define GetMySQLClient(obj, sval) (sval = (MYSQL*)DATA_PTR(obj));
VALUE cMySQLClient;
static VALUE rb_mysql_client_new(VALUE klass); static VALUE rb_mysql_client_new(VALUE klass);
static VALUE rb_mysql_client_init(VALUE self); static VALUE rb_mysql_client_init(VALUE self);
static VALUE rb_mysql_client_query(VALUE self, VALUE query); static VALUE rb_mysql_client_query(VALUE self, VALUE query);
void rb_mysql_client_free(void * client); void rb_mysql_client_free(void * client);
/* MySQL::Result */ /* Mysql2::Result */
#define GetMySQLResult(obj, sval) (sval = (MYSQL_RES*)DATA_PTR(obj)); #define GetMysql2Result(obj, sval) (sval = (MYSQL_RES*)DATA_PTR(obj));
VALUE cMySQLResult; VALUE cMysql2Result;
static ID sym_symbolize_keys;
static VALUE rb_mysql_result_to_obj(MYSQL_RES * res); static VALUE rb_mysql_result_to_obj(MYSQL_RES * res);
void rb_mysql_result_free(void * result); void rb_mysql_result_free(void * result);
static VALUE rb_mysql_result_fetch_row(VALUE self); static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self);
static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self); static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self);

3
spec/rcov.opts Normal file
View File

@ -0,0 +1,3 @@
--exclude spec,gem
--text-summary
--sort coverage --sort-reverse

2
spec/spec.opts Normal file
View File

@ -0,0 +1,2 @@
--format specdoc
--colour

4
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,4 @@
# encoding: UTF-8
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'mysql2'