mostly flesh out mysql type to ruby type conversion mapping

This commit is contained in:
Brian Lopez 2010-03-30 23:30:06 -07:00
parent c844c66046
commit 1848b0da4a
4 changed files with 52 additions and 24 deletions

View File

@ -7,15 +7,15 @@ require 'mysql2_ext'
number_of = 1
database = 'nbb_1_production'
sql = "SELECT * FROM account_transactions"
Benchmark.bmbm do |x|
GC.start
mysql = Mysql.new("localhost", "root")
mysql.query "USE #{database}"
x.report do
puts "Mysql"
number_of.times do
mysql_result = mysql.query "SELECT * FROM account_transactions"
mysql_result = mysql.query sql
number = 0
mysql_result.each_hash do |res|
number += 1
@ -25,13 +25,12 @@ Benchmark.bmbm do |x|
end
end
GC.start
mysql2 = Mysql2::Client.new
mysql2.query "USE #{database}"
x.report do
puts "Mysql2"
number_of.times do
mysql2_result = mysql2.query "SELECT * FROM account_transactions"
mysql2_result = mysql2.query sql
number = 0
mysql2_result.each(:symbolize_keys => true) do |res|
number += 1

View File

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

View File

@ -50,15 +50,14 @@ static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
GetMysql2Client(self, client);
query = mysql_real_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql));
if (query != 0) {
// lookup error code and msg, raise exception
fprintf(stdout, "mysql_real_query error: %s\n", mysql_error(client));
// fprintf(stdout, "mysql_real_query error: %s\n", mysql_error(client));
return Qnil;
}
result = mysql_store_result(client);
if (result == NULL) {
// lookup error code and msg, raise exception
fprintf(stdout, "mysql_store_result error: %s\n", mysql_error(client));
// fprintf(stdout, "mysql_store_result error: %s\n", mysql_error(client));
return Qnil;
}
return rb_mysql_result_to_obj(result);
@ -120,39 +119,61 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
if (row[i]) {
VALUE val;
switch(fields[i].type) {
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_STRING: // CHAR or BINARY field
val = rb_str_new(row[i], fieldLengths[i]);
break;
case MYSQL_TYPE_NULL: // NULL-type field
val = Qnil;
break;
case MYSQL_TYPE_TINY: // TINYINT field
if (row[i] == 0 || 0 == memcmp("0", row[i], fieldLengths[i])) {
val = Qfalse;
} else {
val = Qtrue;
}
break; //infer_ruby_type
case MYSQL_TYPE_BIT: // BIT field (MySQL 5.0.3 and up)
case MYSQL_TYPE_SHORT: // SMALLINT field
case MYSQL_TYPE_LONG: // INTEGER field
case MYSQL_TYPE_INT24: // MEDIUMINT field
case MYSQL_TYPE_LONGLONG: // BIGINT field
val = rb_cstr2inum(row[i], 10);
case MYSQL_TYPE_DECIMAL: // DECIMAL or NUMERIC field
case MYSQL_TYPE_NEWDECIMAL: // Precision math DECIMAL or NUMERIC field (MySQL 5.0.3 and up)
val = Qnil;
case MYSQL_TYPE_FLOAT: // FLOAT field
val = Qnil;
case MYSQL_TYPE_DOUBLE: // DOUBLE or REAL field
val = Qnil;
case MYSQL_TYPE_BIT: // BIT field (MySQL 5.0.3 and up)
case MYSQL_TYPE_TIMESTAMP: // TIMESTAMP field
case MYSQL_TYPE_DATE: // DATE field
case MYSQL_TYPE_TIME: // TIME field
case MYSQL_TYPE_DATETIME: // DATETIME field
val = Qnil;
case MYSQL_TYPE_YEAR: // YEAR field
val = rb_cstr2inum(row[i], 10);
case MYSQL_TYPE_VAR_STRING: // VARCHAR or VARBINARY field
case MYSQL_TYPE_BLOB: // BLOB or TEXT field (use max_length to determine the maximum length)
break;
case MYSQL_TYPE_DECIMAL: // DECIMAL or NUMERIC field
case MYSQL_TYPE_NEWDECIMAL: // Precision math DECIMAL or NUMERIC field (MySQL 5.0.3 and up)
// val = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new(row[i], fieldLengths[i]));
// break;
case MYSQL_TYPE_FLOAT: // FLOAT field
case MYSQL_TYPE_DOUBLE: // DOUBLE or REAL field
val = rb_float_new(strtod(row[i], NULL));
break;
case MYSQL_TYPE_TIMESTAMP: // TIMESTAMP field
case MYSQL_TYPE_TIME: // TIME field
case MYSQL_TYPE_DATETIME: // DATETIME field
// if (memcmp("0000-00-00 00:00:00", row[i], 19) == 0) {
val = rb_str_new(row[i], fieldLengths[i]);
// } else {
// val = rb_funcall(rb_cTime, intern_parse, 1, rb_str_new(row[i], fieldLengths[i]));
// }
break;
case MYSQL_TYPE_DATE: // DATE field
case MYSQL_TYPE_NEWDATE: // Newer const used > 5.0
// val = rb_funcall(cDate, intern_parse, 1, rb_str_new(row[i], fieldLengths[i]));
val = rb_str_new(row[i], fieldLengths[i]);
break;
case MYSQL_TYPE_SET: // SET field
case MYSQL_TYPE_ENUM: // ENUM field
case MYSQL_TYPE_GEOMETRY: // Spatial fielda
default:
val = rb_str_new(row[i], fieldLengths[i]);
break;
}
// TODO: determine field type then inflate a new ruby object around it
rb_hash_aset(rowHash, key, val);
@ -203,6 +224,9 @@ static VALUE rb_mysql_result_fetch_rows(int argc, VALUE * argv, VALUE self) {
/* Ruby Extension initializer */
void Init_mysql2_ext() {
// rb_require("bigdecimal");
// cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
mMysql2 = rb_define_module("Mysql2");
cMysql2Client = rb_define_class_under(mMysql2, "Client", rb_cObject);
@ -215,6 +239,8 @@ void Init_mysql2_ext() {
rb_define_method(cMysql2Result, "fetch_rows", rb_mysql_result_fetch_rows, -1);
rb_define_method(cMysql2Result, "each", rb_mysql_result_fetch_rows, -1);
// intern_new = rb_intern("new");
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
#ifdef HAVE_RUBY_ENCODING_H

View File

@ -5,6 +5,9 @@
#include <mysql/errmsg.h>
#include <mysql/mysqld_error.h>
// VALUE cBigDecimal;
// ID intern_new;
/* Mysql2 */
VALUE mMysql2;