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:
parent
63b55b6f97
commit
736c0d6cd3
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
Makefile
|
||||
*.dSYM
|
||||
*.o
|
||||
*.bundle
|
||||
|
35
Rakefile
Normal file
35
Rakefile
Normal 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
|
@ -13,7 +13,7 @@ if have_library('mysqlclient')
|
||||
$CFLAGS << ' -DRUBY_19_COMPATIBILITY'
|
||||
end
|
||||
|
||||
create_makefile('mysql_duce_ext')
|
||||
create_makefile('mysql2_ext')
|
||||
else
|
||||
puts 'libmysql not found, maybe try manually specifying --with-mysql-lib to find it?'
|
||||
end
|
@ -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) {
|
||||
MYSQL * client;
|
||||
VALUE obj;
|
||||
@ -47,7 +47,7 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
|
||||
int query;
|
||||
Check_Type(sql, T_STRING);
|
||||
|
||||
GetMySQLClient(self, client);
|
||||
GetMysql2Client(self, client);
|
||||
query = mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql));
|
||||
if (query != 0) {
|
||||
// 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) {
|
||||
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);
|
||||
return obj;
|
||||
}
|
||||
@ -76,16 +76,22 @@ void rb_mysql_result_free(void * result) {
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE rb_mysql_result_fetch_row(VALUE self) {
|
||||
VALUE rowHash;
|
||||
static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
|
||||
VALUE rowHash, opts, block;
|
||||
MYSQL_RES * result;
|
||||
MYSQL_ROW row;
|
||||
MYSQL_FIELD * fields;
|
||||
unsigned int numFields;
|
||||
unsigned int i = 0, numFields = 0, symbolizeKeys = 0;
|
||||
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);
|
||||
if (row == NULL) {
|
||||
@ -98,7 +104,15 @@ static VALUE rb_mysql_result_fetch_row(VALUE self) {
|
||||
|
||||
rowHash = rb_hash_new();
|
||||
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]) {
|
||||
rb_hash_aset(rowHash, key, Qnil);
|
||||
} 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) {
|
||||
VALUE dataset, block;
|
||||
VALUE dataset, opts, block;
|
||||
MYSQL_RES * result;
|
||||
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);
|
||||
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...
|
||||
if (block != Qnil) {
|
||||
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) {
|
||||
return Qnil;
|
||||
}
|
||||
@ -135,7 +149,7 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
|
||||
} else {
|
||||
dataset = rb_ary_new2(numRows);
|
||||
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) {
|
||||
return Qnil;
|
||||
}
|
||||
@ -147,18 +161,20 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
|
||||
}
|
||||
|
||||
/* Ruby Extension initializer */
|
||||
void Init_mysql_duce_ext() {
|
||||
mMySQL = rb_define_module("MySQL");
|
||||
void Init_mysql2_ext() {
|
||||
mMysql2 = rb_define_module("Mysql2");
|
||||
|
||||
cMySQLClient = rb_define_class_under(mMySQL, "Client", rb_cObject);
|
||||
rb_define_singleton_method(cMySQLClient, "new", rb_mysql_client_new, 0);
|
||||
rb_define_method(cMySQLClient, "initialize", rb_mysql_client_init, 0);
|
||||
rb_define_method(cMySQLClient, "query", rb_mysql_client_query, 1);
|
||||
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);
|
||||
|
||||
cMySQLResult = rb_define_class_under(mMySQL, "Result", rb_cObject);
|
||||
rb_define_method(cMySQLResult, "fetch_row", rb_mysql_result_fetch_row, 0);
|
||||
rb_define_method(cMySQLResult, "fetch_rows", rb_mysql_result_fetch_rows, -1);
|
||||
rb_define_method(cMySQLResult, "each", rb_mysql_result_fetch_rows, -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"));
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
utf8Encoding = rb_enc_find_index("UTF-8");
|
||||
|
@ -5,26 +5,22 @@
|
||||
#include <mysql/errmsg.h>
|
||||
#include <mysql/mysqld_error.h>
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
#include <ruby/encoding.h>
|
||||
int utf8Encoding;
|
||||
#endif
|
||||
/* Mysql2 */
|
||||
VALUE mMysql2;
|
||||
|
||||
/* MySQL */
|
||||
VALUE mMySQL;
|
||||
|
||||
/* MySQL::Client */
|
||||
#define GetMySQLClient(obj, sval) (sval = (MYSQL*)DATA_PTR(obj));
|
||||
VALUE cMySQLClient;
|
||||
/* Mysql2::Client */
|
||||
#define GetMysql2Client(obj, sval) (sval = (MYSQL*)DATA_PTR(obj));
|
||||
VALUE cMysql2Client;
|
||||
static VALUE rb_mysql_client_new(VALUE klass);
|
||||
static VALUE rb_mysql_client_init(VALUE self);
|
||||
static VALUE rb_mysql_client_query(VALUE self, VALUE query);
|
||||
void rb_mysql_client_free(void * client);
|
||||
|
||||
/* MySQL::Result */
|
||||
#define GetMySQLResult(obj, sval) (sval = (MYSQL_RES*)DATA_PTR(obj));
|
||||
VALUE cMySQLResult;
|
||||
/* Mysql2::Result */
|
||||
#define GetMysql2Result(obj, sval) (sval = (MYSQL_RES*)DATA_PTR(obj));
|
||||
VALUE cMysql2Result;
|
||||
static ID sym_symbolize_keys;
|
||||
static VALUE rb_mysql_result_to_obj(MYSQL_RES * res);
|
||||
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);
|
3
spec/rcov.opts
Normal file
3
spec/rcov.opts
Normal file
@ -0,0 +1,3 @@
|
||||
--exclude spec,gem
|
||||
--text-summary
|
||||
--sort coverage --sort-reverse
|
2
spec/spec.opts
Normal file
2
spec/spec.opts
Normal file
@ -0,0 +1,2 @@
|
||||
--format specdoc
|
||||
--colour
|
4
spec/spec_helper.rb
Normal file
4
spec/spec_helper.rb
Normal file
@ -0,0 +1,4 @@
|
||||
# encoding: UTF-8
|
||||
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
||||
|
||||
require 'mysql2'
|
Loading…
Reference in New Issue
Block a user