Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call BigDecimal(num) instead of BigDecimal.new(num) #928

Merged
merged 2 commits into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/query_with_mysql_casting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def mysql_cast(type, value)
Mysql::Field::TYPE_INT24, Mysql::Field::TYPE_LONGLONG, Mysql::Field::TYPE_YEAR
value.to_i
when Mysql::Field::TYPE_DECIMAL, Mysql::Field::TYPE_NEWDECIMAL
BigDecimal.new(value)
BigDecimal(value)
when Mysql::Field::TYPE_DOUBLE, Mysql::Field::TYPE_FLOAT
value.to_f
when Mysql::Field::TYPE_DATE
Expand Down
22 changes: 11 additions & 11 deletions ext/mysql2/result.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ typedef struct {
VALUE block_given;
} result_each_args;

VALUE cBigDecimal, cDateTime, cDate;
static VALUE cMysql2Result;
static VALUE opt_decimal_zero, opt_float_zero, opt_time_year, opt_time_month, opt_utc_offset;
extern VALUE mMysql2, cMysql2Client, cMysql2Error;
static ID intern_new, intern_utc, intern_local, intern_localtime, intern_local_offset, intern_civil, intern_new_offset;
static VALUE sym_symbolize_keys, sym_as, sym_array, sym_database_timezone, sym_application_timezone,
sym_local, sym_utc, sym_cast_booleans, sym_cache_rows, sym_cast, sym_stream, sym_name;
static ID intern_merge;
static VALUE cMysql2Result, cDateTime, cDate;
static VALUE opt_decimal_zero, opt_float_zero, opt_time_year, opt_time_month, opt_utc_offset;
static ID intern_new, intern_utc, intern_local, intern_localtime, intern_local_offset,
intern_civil, intern_new_offset, intern_merge, intern_BigDecimal;
static VALUE sym_symbolize_keys, sym_as, sym_array, sym_database_timezone,
sym_application_timezone, sym_local, sym_utc, sym_cast_booleans,
sym_cache_rows, sym_cast, sym_stream, sym_name;

/* Mark any VALUEs that are only referenced in C, so the GC won't get them. */
static void rb_mysql_result_mark(void * wrapper) {
Expand Down Expand Up @@ -444,7 +444,7 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
}
case MYSQL_TYPE_DECIMAL: // char[]
case MYSQL_TYPE_NEWDECIMAL: // char[]
val = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new(result_buffer->buffer, *(result_buffer->length)));
val = rb_funcall(rb_mKernel, intern_BigDecimal, 1, rb_str_new(result_buffer->buffer, *(result_buffer->length)));
break;
case MYSQL_TYPE_STRING: // char[]
case MYSQL_TYPE_VAR_STRING: // char[]
Expand Down Expand Up @@ -546,9 +546,9 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, MYSQL_FIELD * fields, const r
if (fields[i].decimals == 0) {
val = rb_cstr2inum(row[i], 10);
} else if (strtod(row[i], NULL) == 0.000000){
val = rb_funcall(cBigDecimal, intern_new, 1, opt_decimal_zero);
val = rb_funcall(rb_mKernel, intern_BigDecimal, 1, opt_decimal_zero);
}else{
val = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new(row[i], fieldLengths[i]));
val = rb_funcall(rb_mKernel, intern_BigDecimal, 1, rb_str_new(row[i], fieldLengths[i]));
}
break;
case MYSQL_TYPE_FLOAT: /* FLOAT field */
Expand Down Expand Up @@ -959,7 +959,6 @@ VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_
}

void init_mysql2_result() {
cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));

Expand All @@ -978,6 +977,7 @@ void init_mysql2_result() {
intern_local_offset = rb_intern("local_offset");
intern_civil = rb_intern("civil");
intern_new_offset = rb_intern("new_offset");
intern_BigDecimal = rb_intern("BigDecimal");

sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
sym_as = ID2SYM(rb_intern("as"));
Expand Down
9 changes: 6 additions & 3 deletions ext/mysql2/statement.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <mysql2_ext.h>

VALUE cMysql2Statement;
extern VALUE mMysql2, cMysql2Error, cMysql2TimeoutError, cBigDecimal, cDateTime, cDate;
extern VALUE mMysql2, cMysql2Error;
static VALUE cMysql2Statement, cBigDecimal, cDateTime, cDate;
static VALUE sym_stream, intern_new_with_args, intern_each, intern_to_s, intern_merge_bang;
static VALUE intern_sec_fraction, intern_usec, intern_sec, intern_min, intern_hour, intern_day, intern_month, intern_year;

Expand Down Expand Up @@ -547,8 +547,11 @@ static VALUE rb_mysql_stmt_close(VALUE self) {
}

void init_mysql2_statement() {
cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));
cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));

cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
rb_define_method(cMysql2Statement, "param_count", rb_mysql_stmt_param_count, 0);
rb_define_method(cMysql2Statement, "field_count", rb_mysql_stmt_field_count, 0);
rb_define_method(cMysql2Statement, "_execute", rb_mysql_stmt_execute, -1);
Expand Down
2 changes: 0 additions & 2 deletions ext/mysql2/statement.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef MYSQL2_STATEMENT_H
#define MYSQL2_STATEMENT_H

extern VALUE cMysql2Statement;

typedef struct {
VALUE client;
MYSQL_STMT *stmt;
Expand Down
4 changes: 2 additions & 2 deletions spec/mysql2/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def stmt_count

it "should handle as a decimal binding a BigDecimal" do
stmt = @client.prepare('SELECT ? AS decimal_test')
test_result = stmt.execute(BigDecimal.new("123.45")).first
test_result = stmt.execute(BigDecimal("123.45")).first
expect(test_result['decimal_test']).to be_an_instance_of(BigDecimal)
expect(test_result['decimal_test']).to eql(123.45)
end
Expand All @@ -193,7 +193,7 @@ def stmt_count
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_decimal_test'
@client.query 'CREATE TABLE mysql2_stmt_decimal_test (decimal_test DECIMAL(10,3))'

@client.prepare("INSERT INTO mysql2_stmt_decimal_test VALUES (?)").execute(BigDecimal.new("123.45"))
@client.prepare("INSERT INTO mysql2_stmt_decimal_test VALUES (?)").execute(BigDecimal("123.45"))

test_result = @client.query("SELECT * FROM mysql2_stmt_decimal_test").first
expect(test_result['decimal_test']).to eql(123.45)
Expand Down