Skip to content

Commit 9efaa56

Browse files
committed
Convert large integer into string and set as MYSQL_TYPE_NEWDECIMAL
1 parent 774eca5 commit 9efaa56

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

ext/mysql2/statement.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static void set_buffer_for_string(MYSQL_BIND* bind_buffer, unsigned long *length
205205
}
206206

207207
/* return 0 if the given bignum can cast as LONG_LONG, otherwise 1 */
208-
int my_big2ll(VALUE bignum, LONG_LONG *ptr)
208+
static int my_big2ll(VALUE bignum, LONG_LONG *ptr)
209209
{
210210
unsigned LONG_LONG num;
211211
#ifdef HAVE_RB_ABSINT_SIZE
@@ -292,8 +292,22 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
292292
break;
293293
case T_BIGNUM:
294294
bind_buffers[i].buffer_type = MYSQL_TYPE_LONGLONG;
295-
bind_buffers[i].buffer = xmalloc(sizeof(long long int));
296-
my_big2ll(argv[i], (LONG_LONG*)(bind_buffers[i].buffer));
295+
{
296+
LONG_LONG num;
297+
if (my_big2ll(argv[i], &num)) {
298+
VALUE rb_val_as_string = rb_big2str(argv[i], 10);
299+
bind_buffers[i].buffer_type = MYSQL_TYPE_NEWDECIMAL;
300+
params_enc[i] = rb_val_as_string;
301+
#ifdef HAVE_RUBY_ENCODING_H
302+
params_enc[i] = rb_str_export_to_enc(params_enc[i], conn_enc);
303+
#endif
304+
set_buffer_for_string(&bind_buffers[i], &length_buffers[i], params_enc[i]);
305+
}
306+
else {
307+
bind_buffers[i].buffer = xmalloc(sizeof(long long int));
308+
*(LONG_LONG*)(bind_buffers[i].buffer) = num;
309+
}
310+
}
297311
break;
298312
case T_FLOAT:
299313
bind_buffers[i].buffer_type = MYSQL_TYPE_DOUBLE;

spec/mysql2/statement_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
it "should handle bignum but beyond int64_t" do
7272
stmt = @client.prepare('SELECT ? AS max1, ? AS min1')
7373
int64_max1 = (1 << 63)
74-
int64_min1 = -(1 << 63) - 2
74+
int64_min1 = -(1 << 63) - 1
7575
result = stmt.execute(int64_max1, int64_min1)
76-
expect(result.to_a).to eq(['max1' => -1, 'min1' => -1])
76+
expect(result.to_a).to eq(['max1' => "9223372036854775808", 'min1' => "-9223372036854775809"])
7777
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
7878
end
7979

0 commit comments

Comments
 (0)