Skip to content

Commit

Permalink
add PreparedStatement#param_type.
Browse files Browse the repository at this point in the history
  • Loading branch information
suketa committed Jul 6, 2024
1 parent 02c0720 commit ae9fe52
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ext/duckdb/prepared_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static VALUE duckdb_prepared_statement_bind_varchar(VALUE self, VALUE vidx, VALU
static VALUE duckdb_prepared_statement_bind_blob(VALUE self, VALUE vidx, VALUE blob);
static VALUE duckdb_prepared_statement_bind_null(VALUE self, VALUE vidx);
static VALUE duckdb_prepared_statement__statement_type(VALUE self);
static VALUE duckdb_prepared_statement__param_type(VALUE self, VALUE vidx);
static VALUE duckdb_prepared_statement__bind_date(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day);
static VALUE duckdb_prepared_statement__bind_time(VALUE self, VALUE vidx, VALUE hour, VALUE min, VALUE sec, VALUE micros);
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);
Expand Down Expand Up @@ -280,6 +281,12 @@ static VALUE duckdb_prepared_statement__statement_type(VALUE self) {
return INT2FIX(duckdb_prepared_statement_type(ctx->prepared_statement));
}

static VALUE duckdb_prepared_statement__param_type(VALUE self, VALUE vidx) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
return INT2FIX(duckdb_param_type(ctx->prepared_statement, NUM2ULL(vidx)));
}

static VALUE duckdb_prepared_statement__bind_date(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day) {
rubyDuckDBPreparedStatement *ctx;
duckdb_date dt;
Expand Down Expand Up @@ -389,6 +396,7 @@ void rbduckdb_init_duckdb_prepared_statement(void) {
rb_define_method(cDuckDBPreparedStatement, "bind_blob", duckdb_prepared_statement_bind_blob, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_null", duckdb_prepared_statement_bind_null, 1);
rb_define_private_method(cDuckDBPreparedStatement, "_statement_type", duckdb_prepared_statement__statement_type, 0);
rb_define_private_method(cDuckDBPreparedStatement, "_param_type", duckdb_prepared_statement__param_type, 1);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_date", duckdb_prepared_statement__bind_date, 4);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_time", duckdb_prepared_statement__bind_time, 5);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_timestamp", duckdb_prepared_statement__bind_timestamp, 8);
Expand Down
9 changes: 9 additions & 0 deletions test/duckdb_test/prepared_statement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ def test_statement_type
assert_equal(:select, stmt.statement_type)
end

def test__param_type
con = PreparedStatementTest.con

stmt = DuckDB::PreparedStatement.new(con, 'SELECT * FROM a WHERE id = $1')
assert_equal(0, stmt.send(:_param_type, 0)) # INVALID
assert_equal(4, stmt.send(:_param_type, 1)) # INTEGER
assert_equal(0, stmt.send(:_param_type, 2)) # INVALID
end

def test_pending_prepared
con = PreparedStatementTest.con
stmt = DuckDB::PreparedStatement.new(con, 'SELECT * FROM a')
Expand Down

0 comments on commit ae9fe52

Please sign in to comment.