Skip to content

Commit 4702612

Browse files
authored
Merge pull request #933 from suketa/bind_uint32
add DuckDB::PreparedStatement#bind_uint32(refs #704).
2 parents aec4ff4 + d6c90a5 commit 4702612

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
All notable changes to this project will be documented in this file.
44
# Unreleased
5-
- add `DuckDB::PreparedStatement#bind_uint8`, `DuckDB::PreparedStatement#bind_uint16`.
5+
- add `DuckDB::PreparedStatement#bind_uint8`, `DuckDB::PreparedStatement#bind_uint16`,
6+
`DuckDB::PreparedStatement#bind_uint32`.
67
- support Enum type in `DuckDB::LogicalType` class.
78
- `DuckDB::LogicalType#internal_type`, `DuckDB::LogicalType#dictionary_size`,
89
`DuckDB::LogicalType#dictionary_value_at`, and `DuckDB::LogicalType#each_dictionary_value` are

ext/duckdb/prepared_statement.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static VALUE duckdb_prepared_statement_bind_null(VALUE self, VALUE vidx);
2828
static VALUE duckdb_prepared_statement__statement_type(VALUE self);
2929
static VALUE duckdb_prepared_statement__param_type(VALUE self, VALUE vidx);
3030
static VALUE duckdb_prepared_statement__bind_uint8(VALUE self, VALUE vidx, VALUE val);
31+
static VALUE duckdb_prepared_statement__bind_uint16(VALUE self, VALUE vidx, VALUE val);
32+
static VALUE duckdb_prepared_statement__bind_uint32(VALUE self, VALUE vidx, VALUE val);
3133
static VALUE duckdb_prepared_statement__bind_date(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day);
3234
static VALUE duckdb_prepared_statement__bind_time(VALUE self, VALUE vidx, VALUE hour, VALUE min, VALUE sec, VALUE micros);
3335
static VALUE duckdb_prepared_statement__bind_timestamp(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day, VALUE hour, VALUE min, VALUE sec, VALUE micros);
@@ -389,6 +391,20 @@ static VALUE duckdb_prepared_statement__bind_uint16(VALUE self, VALUE vidx, VALU
389391
return self;
390392
}
391393

394+
/* :nodoc: */
395+
static VALUE duckdb_prepared_statement__bind_uint32(VALUE self, VALUE vidx, VALUE val) {
396+
rubyDuckDBPreparedStatement *ctx;
397+
idx_t idx = check_index(vidx);
398+
uint32_t ui32val = (uint32_t)NUM2UINT(val);
399+
400+
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
401+
402+
if (duckdb_bind_uint32(ctx->prepared_statement, idx, ui32val) == DuckDBError) {
403+
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
404+
}
405+
return self;
406+
}
407+
392408
/* :nodoc: */
393409
static VALUE duckdb_prepared_statement__bind_date(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day) {
394410
rubyDuckDBPreparedStatement *ctx;
@@ -543,6 +559,7 @@ void rbduckdb_init_duckdb_prepared_statement(void) {
543559
rb_define_method(cDuckDBPreparedStatement, "bind_null", duckdb_prepared_statement_bind_null, 1);
544560
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint8", duckdb_prepared_statement__bind_uint8, 2);
545561
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint16", duckdb_prepared_statement__bind_uint16, 2);
562+
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint32", duckdb_prepared_statement__bind_uint32, 2);
546563
rb_define_private_method(cDuckDBPreparedStatement, "_statement_type", duckdb_prepared_statement__statement_type, 0);
547564
rb_define_private_method(cDuckDBPreparedStatement, "_param_type", duckdb_prepared_statement__param_type, 1);
548565
rb_define_private_method(cDuckDBPreparedStatement, "_bind_date", duckdb_prepared_statement__bind_date, 4);

lib/duckdb/prepared_statement.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ def bind_uint16(index, val)
125125
raise DuckDB::Error, "can't bind uint16(bind_uint16) to `#{val}`. The `#{val}` is out of range 0..65535."
126126
end
127127

128+
# binds i-th parameter with SQL prepared statement.
129+
# The first argument is index of parameter.
130+
# The index of first parameter is 1 not 0.
131+
# The second argument value is to expected Integer value between 0 to 4294967295.
132+
def bind_uint32(index, val)
133+
return _bind_uint32(index, val) if val.between?(0, 4_294_967_295)
134+
135+
raise DuckDB::Error, "can't bind uint32(bind_uint32) to `#{val}`. The `#{val}` is out of range 0..4294967295."
136+
end
128137
# binds i-th parameter with SQL prepared statement.
129138
# The first argument is index of parameter.
130139
# The index of first parameter is 1 not 0.

test/duckdb_test/prepared_statement_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,26 @@ def test_bind_int32
307307
assert_nil(stmt.execute.each.first)
308308
end
309309

310+
def test_bind_uint32
311+
value = (2**32) - 1
312+
@con.query('CREATE TABLE values (value UINTEGER)')
313+
314+
stmt = DuckDB::PreparedStatement.new(@con, 'INSERT INTO values(value) VALUES ($1)')
315+
stmt.bind_uint32(1, value)
316+
stmt.execute
317+
318+
stmt = DuckDB::PreparedStatement.new(@con, 'SELECT * FROM values WHERE value = $1')
319+
stmt.bind_uint32(1, value)
320+
assert_equal(value, stmt.execute.each.first[0])
321+
end
322+
323+
def test_bind_uint32_with_negative
324+
@con.query('CREATE TABLE values (value UINTEGER)')
325+
326+
stmt = DuckDB::PreparedStatement.new(@con, 'INSERT INTO values(value) VALUES ($1)')
327+
assert_raises(DuckDB::Error) { stmt.bind_uint32(1, -1) }
328+
end
329+
310330
def test_bind_int64
311331
stmt = DuckDB::PreparedStatement.new(@con, 'SELECT * FROM a WHERE col_smallint = $1')
312332

0 commit comments

Comments
 (0)