Skip to content

Commit c0ba3d3

Browse files
authored
Call BigDecimal(num) instead of BigDecimal.new(num) (#928)
Call BigDecimal(num) instead of BigDecimal.new(num) to be ready for this deprecation in Ruby 2.6, and eliminate a deprecation warning now. Reduce the number of extern classes - while developing this change, I was chasing a NULL pointer on cBigDecimal from statement.c because I removed the class lookup from result.c, but extern meant the compiler did not flag it as an uninitialized value.
1 parent e52e9ce commit c0ba3d3

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

benchmark/query_with_mysql_casting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def mysql_cast(type, value)
2121
Mysql::Field::TYPE_INT24, Mysql::Field::TYPE_LONGLONG, Mysql::Field::TYPE_YEAR
2222
value.to_i
2323
when Mysql::Field::TYPE_DECIMAL, Mysql::Field::TYPE_NEWDECIMAL
24-
BigDecimal.new(value)
24+
BigDecimal(value)
2525
when Mysql::Field::TYPE_DOUBLE, Mysql::Field::TYPE_FLOAT
2626
value.to_f
2727
when Mysql::Field::TYPE_DATE

ext/mysql2/result.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ typedef struct {
3232
VALUE block_given;
3333
} result_each_args;
3434

35-
VALUE cBigDecimal, cDateTime, cDate;
36-
static VALUE cMysql2Result;
37-
static VALUE opt_decimal_zero, opt_float_zero, opt_time_year, opt_time_month, opt_utc_offset;
3835
extern VALUE mMysql2, cMysql2Client, cMysql2Error;
39-
static ID intern_new, intern_utc, intern_local, intern_localtime, intern_local_offset, intern_civil, intern_new_offset;
40-
static VALUE sym_symbolize_keys, sym_as, sym_array, sym_database_timezone, sym_application_timezone,
41-
sym_local, sym_utc, sym_cast_booleans, sym_cache_rows, sym_cast, sym_stream, sym_name;
42-
static ID intern_merge;
36+
static VALUE cMysql2Result, cDateTime, cDate;
37+
static VALUE opt_decimal_zero, opt_float_zero, opt_time_year, opt_time_month, opt_utc_offset;
38+
static ID intern_new, intern_utc, intern_local, intern_localtime, intern_local_offset,
39+
intern_civil, intern_new_offset, intern_merge, intern_BigDecimal;
40+
static VALUE sym_symbolize_keys, sym_as, sym_array, sym_database_timezone,
41+
sym_application_timezone, sym_local, sym_utc, sym_cast_booleans,
42+
sym_cache_rows, sym_cast, sym_stream, sym_name;
4343

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

961961
void init_mysql2_result() {
962-
cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
963962
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
964963
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));
965964

@@ -978,6 +977,7 @@ void init_mysql2_result() {
978977
intern_local_offset = rb_intern("local_offset");
979978
intern_civil = rb_intern("civil");
980979
intern_new_offset = rb_intern("new_offset");
980+
intern_BigDecimal = rb_intern("BigDecimal");
981981

982982
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
983983
sym_as = ID2SYM(rb_intern("as"));

ext/mysql2/statement.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <mysql2_ext.h>
22

3-
VALUE cMysql2Statement;
4-
extern VALUE mMysql2, cMysql2Error, cMysql2TimeoutError, cBigDecimal, cDateTime, cDate;
3+
extern VALUE mMysql2, cMysql2Error;
4+
static VALUE cMysql2Statement, cBigDecimal, cDateTime, cDate;
55
static VALUE sym_stream, intern_new_with_args, intern_each, intern_to_s, intern_merge_bang;
66
static VALUE intern_sec_fraction, intern_usec, intern_sec, intern_min, intern_hour, intern_day, intern_month, intern_year;
77

@@ -547,8 +547,11 @@ static VALUE rb_mysql_stmt_close(VALUE self) {
547547
}
548548

549549
void init_mysql2_statement() {
550-
cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
550+
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
551+
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));
552+
cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
551553

554+
cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
552555
rb_define_method(cMysql2Statement, "param_count", rb_mysql_stmt_param_count, 0);
553556
rb_define_method(cMysql2Statement, "field_count", rb_mysql_stmt_field_count, 0);
554557
rb_define_method(cMysql2Statement, "_execute", rb_mysql_stmt_execute, -1);

ext/mysql2/statement.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef MYSQL2_STATEMENT_H
22
#define MYSQL2_STATEMENT_H
33

4-
extern VALUE cMysql2Statement;
5-
64
typedef struct {
75
VALUE client;
86
MYSQL_STMT *stmt;

spec/mysql2/statement_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def stmt_count
183183

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

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

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

0 commit comments

Comments
 (0)