Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created August 8, 2010 14:10
Show Gist options
  • Save methodmissing/514065 to your computer and use it in GitHub Desktop.
Save methodmissing/514065 to your computer and use it in GitHub Desktop.
diff --git a/benchmark/setup_db.rb b/benchmark/setup_db.rb
index 4f1cfe7..a3718c2 100644
--- a/benchmark/setup_db.rb
+++ b/benchmark/setup_db.rb
@@ -24,6 +24,7 @@ create_table_sql = %[
float_test FLOAT(10,3),
double_test DOUBLE(10,3),
decimal_test DECIMAL(10,3),
+ decimal_zero_test DECIMAL(10,3),
date_test DATE,
date_time_test DATETIME,
timestamp_test TIMESTAMP,
@@ -55,7 +56,7 @@ def insert_record(args)
insert_sql = "
INSERT INTO mysql2_test (
null_test, bit_test, tiny_int_test, small_int_test, medium_int_test, int_test, big_int_test,
- float_test, double_test, decimal_test, date_test, date_time_test, timestamp_test, time_test,
+ float_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
long_blob_test, long_text_test, enum_test, set_test
@@ -63,7 +64,7 @@ def insert_record(args)
VALUES (
NULL, #{args[:bit_test]}, #{args[:tiny_int_test]}, #{args[:small_int_test]}, #{args[:medium_int_test]}, #{args[:int_test]}, #{args[:big_int_test]},
- #{args[:float_test]}, #{args[:double_test]}, #{args[:decimal_test]}, '#{args[:date_test]}', '#{args[:date_time_test]}', '#{args[:timestamp_test]}', '#{args[:time_test]}',
+ #{args[:float_test]}, #{args[:double_test]}, #{args[:decimal_test]}, #{args[:decimal_zero_test]}, '#{args[:date_test]}', '#{args[:date_time_test]}', '#{args[:timestamp_test]}', '#{args[:time_test
#{args[:year_test]}, '#{args[:char_test]}', '#{args[:varchar_test]}', '#{args[:binary_test]}', '#{args[:varbinary_test]}', '#{args[:tiny_blob_test]}',
'#{args[:tiny_text_test]}', '#{args[:blob_test]}', '#{args[:text_test]}', '#{args[:medium_blob_test]}', '#{args[:medium_text_test]}',
'#{args[:long_blob_test]}', '#{args[:long_text_test]}', '#{args[:enum_test]}', '#{args[:set_test]}'
@@ -84,6 +85,7 @@ num.times do |n|
:float_test => rand(32767)/1.87,
:double_test => rand(8388607)/1.87,
:decimal_test => rand(8388607)/1.87,
+ :decimal_zero_test => 0.0,
:date_test => '2010-4-4',
:date_time_test => '2010-4-4 11:44:00',
:timestamp_test => '2010-4-4 11:44:00',
diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c
index 70066c4..4e7bcd9 100644
--- a/ext/mysql2/result.c
+++ b/ext/mysql2/result.c
@@ -6,6 +6,7 @@ rb_encoding *binaryEncoding;
VALUE cMysql2Result;
VALUE cBigDecimal, cDate, cDateTime;
+VALUE opt_decimal_zero;
extern VALUE mMysql2, cMysql2Client, cMysql2Error;
static VALUE intern_encoding_from_charset;
static ID intern_new, intern_utc, intern_local, intern_encoding_from_charset_code,
@@ -146,7 +147,11 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
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]));
+ if (row[i] == "0.0" || row[i] == "0"){
+ val = opt_decimal_zero;
+ }else{
+ 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
@@ -420,6 +425,8 @@ void init_mysql2_result() {
sym_database_timezone = ID2SYM(rb_intern("database_timezone"));
sym_application_timezone = ID2SYM(rb_intern("application_timezone"));
+ opt_decimal_zero = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new2("0.0"));
+ FL_SET(opt_decimal_zero, FL_MARK);
#ifdef HAVE_RUBY_ENCODING_H
binaryEncoding = rb_enc_find("binary");
#endif
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 0063bb7..df979d5 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -22,6 +22,7 @@ Spec::Runner.configure do |config|
float_test FLOAT(10,3),
double_test DOUBLE(10,3),
decimal_test DECIMAL(10,3),
+ decimal_zero_test DECIMAL(10,3),
date_test DATE,
date_time_test DATETIME,
timestamp_test TIMESTAMP,
@@ -47,7 +48,7 @@ Spec::Runner.configure do |config|
client.query %[
INSERT INTO mysql2_test (
null_test, bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
- float_test, double_test, decimal_test, date_test, date_time_test, timestamp_test, time_test,
+ float_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
long_blob_test, long_text_test, enum_test, set_test
@@ -55,7 +56,7 @@ Spec::Runner.configure do |config|
VALUES (
NULL, b'101', 1, 1, 10, 10, 10, 10,
- 10.3, 10.3, 10.3, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
+ 10.3, 10.3, 10.3, 0, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
2009, "test", "test", "test", "test", "test",
"test", "test", "test", "test", "test",
"test", "test", 'val1', 'val1,val2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment