Skip to content

Commit 089ccf1

Browse files
authored
Merge pull request #152 from duckdb/jray/support-param-logical-type
support param_logical_type
2 parents da4618a + 3513737 commit 089ccf1

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

api/src/DuckDBPreparedStatement.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import duckdb from '@duckdb/node-bindings';
22
import { createValue } from './createValue';
3+
import { DuckDBLogicalType } from './DuckDBLogicalType';
34
import { DuckDBMaterializedResult } from './DuckDBMaterializedResult';
45
import { DuckDBPendingResult } from './DuckDBPendingResult';
56
import { DuckDBResult } from './DuckDBResult';
@@ -49,6 +50,12 @@ export class DuckDBPreparedStatement {
4950
parameterIndex
5051
) as number as DuckDBTypeId;
5152
}
53+
public parameterType(parameterIndex: number): DuckDBType {
54+
return DuckDBLogicalType.create(duckdb.param_logical_type(
55+
this.prepared_statement,
56+
parameterIndex
57+
)).asType();
58+
}
5259
public clearBindings() {
5360
duckdb.clear_bindings(this.prepared_statement);
5461
}

api/test/api.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
DuckDBTimestampVector,
5353
DuckDBTinyIntVector,
5454
DuckDBType,
55+
DuckDBTypeId,
5556
DuckDBUBigIntVector,
5657
DuckDBUHugeIntVector,
5758
DuckDBUIntegerVector,
@@ -422,6 +423,23 @@ describe('api', () => {
422423
);
423424
prepared.bindArray(7, arrayValue([100, 200, 300]), ARRAY(INTEGER, 3));
424425
prepared.bindNull(8);
426+
assert.equal(prepared.parameterTypeId(1), DuckDBTypeId.INTEGER);
427+
assert.deepEqual(prepared.parameterType(1), INTEGER);
428+
// See https://github.com/duckdb/duckdb/issues/16137
429+
// assert.equal(prepared.parameterTypeId(2), DuckDBTypeId.VARCHAR);
430+
// assert.deepEqual(prepared.parameterType(2), VARCHAR);
431+
assert.equal(prepared.parameterTypeId(3), DuckDBTypeId.BOOLEAN);
432+
assert.deepEqual(prepared.parameterType(3), BOOLEAN);
433+
assert.equal(prepared.parameterTypeId(4), DuckDBTypeId.TIME_TZ);
434+
assert.deepEqual(prepared.parameterType(4), TIMETZ);
435+
assert.equal(prepared.parameterTypeId(5), DuckDBTypeId.LIST);
436+
assert.deepEqual(prepared.parameterType(5), LIST(INTEGER));
437+
assert.equal(prepared.parameterTypeId(6), DuckDBTypeId.STRUCT);
438+
assert.deepEqual(prepared.parameterType(6), STRUCT({ 'a': INTEGER, 'b': VARCHAR }));
439+
assert.equal(prepared.parameterTypeId(7), DuckDBTypeId.ARRAY);
440+
assert.deepEqual(prepared.parameterType(7), ARRAY(INTEGER, 3));
441+
assert.equal(prepared.parameterTypeId(8), DuckDBTypeId.SQLNULL);
442+
assert.deepEqual(prepared.parameterType(8), SQLNULL);
425443
const result = await prepared.run();
426444
assertColumns(result, [
427445
{ name: 'a', type: INTEGER },

bindings/pkgs/@duckdb/node-bindings/duckdb.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ export function parameter_name(prepared_statement: PreparedStatement, index: num
461461
export function param_type(prepared_statement: PreparedStatement, index: number): Type;
462462

463463
// DUCKDB_API duckdb_logical_type duckdb_param_logical_type(duckdb_prepared_statement prepared_statement, idx_t param_idx);
464+
export function param_logical_type(prepared_statement: PreparedStatement, index: number): LogicalType;
464465

465466
// DUCKDB_API duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement);
466467
export function clear_bindings(prepared_statement: PreparedStatement): void;

bindings/src/duckdb_node_bindings.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ class DuckDBNodeAddon : public Napi::Addon<DuckDBNodeAddon> {
10561056
InstanceMethod("nparams", &DuckDBNodeAddon::nparams),
10571057
InstanceMethod("parameter_name", &DuckDBNodeAddon::parameter_name),
10581058
InstanceMethod("param_type", &DuckDBNodeAddon::param_type),
1059+
InstanceMethod("param_logical_type", &DuckDBNodeAddon::param_logical_type),
10591060
InstanceMethod("clear_bindings", &DuckDBNodeAddon::clear_bindings),
10601061
InstanceMethod("prepared_statement_type", &DuckDBNodeAddon::prepared_statement_type),
10611062
InstanceMethod("bind_value", &DuckDBNodeAddon::bind_value),
@@ -1794,6 +1795,17 @@ class DuckDBNodeAddon : public Napi::Addon<DuckDBNodeAddon> {
17941795
}
17951796

17961797
// DUCKDB_API duckdb_logical_type duckdb_param_logical_type(duckdb_prepared_statement prepared_statement, idx_t param_idx);
1798+
// function param_logical_type(prepared_statement: PreparedStatement, index: number): LogicalType
1799+
Napi::Value param_logical_type(const Napi::CallbackInfo& info) {
1800+
auto env = info.Env();
1801+
auto prepared_statement = GetPreparedStatementFromExternal(env, info[0]);
1802+
auto index = info[1].As<Napi::Number>().Uint32Value();
1803+
auto logical_type = duckdb_param_logical_type(prepared_statement, index);
1804+
if (!logical_type) {
1805+
throw Napi::Error::New(env, "Failed to get logical type");
1806+
}
1807+
return CreateExternalForLogicalType(env, logical_type);
1808+
}
17971809

17981810
// DUCKDB_API duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement);
17991811
// function clear_bindings(prepared_statement: PreparedStatement): void
@@ -3896,9 +3908,8 @@ NODE_API_ADDON(DuckDBNodeAddon)
38963908
---
38973909
411 total functions
38983910
3899-
213 instance methods
3911+
214 instance methods
39003912
3 unimplemented instance cache functions
3901-
1 unimplemented prepared statement function
39023913
1 unimplemented logical type function
39033914
10 unimplemented value creation functions
39043915
13 unimplemented value inspection functions

0 commit comments

Comments
 (0)