Skip to content

Commit f16b3b0

Browse files
kamiposodabrew
authored andcommitted
Fix a BigDecimal value binding (brianmario#783)
Currently a BigDecimal value is bound as a string because `set_buffer_for_string` overwrites `buffer_type` to `MYSQL_TYPE_STRING`. This commit fixes to a BigDecimal value is bound as a decimal.
1 parent cb564e3 commit f16b3b0

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

ext/mysql2/statement.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ static void *nogvl_execute(void *ptr) {
180180
static void set_buffer_for_string(MYSQL_BIND* bind_buffer, unsigned long *length_buffer, VALUE string) {
181181
unsigned long length;
182182

183-
bind_buffer->buffer_type = MYSQL_TYPE_STRING;
184183
bind_buffer->buffer = RSTRING_PTR(string);
185184

186185
length = RSTRING_LEN(string);
@@ -275,13 +274,13 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
275274
*(double*)(bind_buffers[i].buffer) = NUM2DBL(argv[i]);
276275
break;
277276
case T_STRING:
278-
{
279-
params_enc[i] = argv[i];
277+
bind_buffers[i].buffer_type = MYSQL_TYPE_STRING;
278+
279+
params_enc[i] = argv[i];
280280
#ifdef HAVE_RUBY_ENCODING_H
281-
params_enc[i] = rb_str_export_to_enc(params_enc[i], conn_enc);
281+
params_enc[i] = rb_str_export_to_enc(params_enc[i], conn_enc);
282282
#endif
283-
set_buffer_for_string(&bind_buffers[i], &length_buffers[i], params_enc[i]);
284-
}
283+
set_buffer_for_string(&bind_buffers[i], &length_buffers[i], params_enc[i]);
285284
break;
286285
default:
287286
// TODO: what Ruby type should support MYSQL_TYPE_TIME

spec/mysql2/statement_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ def stmt_count
119119
expect(list[1]).to eq('2')
120120
end
121121

122+
it "should handle as a decimal binding a BigDecimal" do
123+
stmt = @client.prepare('SELECT ? AS decimal_test')
124+
test_result = stmt.execute(BigDecimal.new("123.45")).first
125+
expect(test_result['decimal_test']).to be_an_instance_of(BigDecimal)
126+
expect(test_result['decimal_test']).to eql(123.45)
127+
end
128+
122129
it "should update a DECIMAL value passing a BigDecimal" do
123130
@client.query 'USE test'
124131
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_decimal_test'

0 commit comments

Comments
 (0)