Skip to content

Commit f0e40e7

Browse files
committed
feat: Expose LogicalType(s) for columns in QueryResult, fix #24
1 parent 2722553 commit f0e40e7

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

lib/duckdb.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ export class Connection {
108108
unregister_buffer(name: string, callback?: Callback<void>): void;
109109
}
110110

111+
export type LogicalType = {
112+
id: number,
113+
name: string,
114+
}
115+
111116
export class QueryResult implements AsyncIterable<RowData> {
117+
getColumns(): Record<string, LogicalType>;
112118
[Symbol.asyncIterator](): AsyncIterator<RowData>;
113119
}
114120

lib/duckdb.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ QueryResult.prototype.nextChunk;
8484
*/
8585
QueryResult.prototype.nextIpcBuffer;
8686

87+
/**
88+
* Function to return logical types for columns
89+
*
90+
* @method
91+
*/
92+
QueryResult.prototype.getColumns;
93+
8794
/**
8895
* @name asyncIterator
8996
* @memberof module:duckdb~QueryResult
@@ -218,12 +225,9 @@ Connection.prototype.each = function (sql) {
218225
* @param {...*} params
219226
* @yields row chunks
220227
*/
221-
Connection.prototype.stream = async function* (sql) {
228+
Connection.prototype.stream = async function (sql) {
222229
const statement = new Statement(this, sql);
223-
const queryResult = await statement.stream.apply(statement, arguments);
224-
for await (const result of queryResult) {
225-
yield result;
226-
}
230+
return statement.stream.apply(statement, arguments);
227231
}
228232

229233
/**

src/duckdb_node.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class QueryResult : public Napi::ObjectWrap<QueryResult> {
208208
public:
209209
Napi::Value NextChunk(const Napi::CallbackInfo &info);
210210
Napi::Value NextIpcBuffer(const Napi::CallbackInfo &info);
211+
Napi::Value GetColumns(const Napi::CallbackInfo &info);
211212
duckdb::shared_ptr<ArrowSchema> cschema;
212213

213214
private:

src/statement.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ Napi::FunctionReference QueryResult::Init(Napi::Env env, Napi::Object exports) {
636636

637637
Napi::Function t = DefineClass(env, "QueryResult",
638638
{InstanceMethod("nextChunk", &QueryResult::NextChunk),
639-
InstanceMethod("nextIpcBuffer", &QueryResult::NextIpcBuffer)});
639+
InstanceMethod("nextIpcBuffer", &QueryResult::NextIpcBuffer),
640+
InstanceMethod("getColumns", &QueryResult::GetColumns)});
640641

641642
exports.Set("QueryResult", t);
642643

@@ -742,6 +743,26 @@ Napi::Value QueryResult::NextIpcBuffer(const Napi::CallbackInfo &info) {
742743
return deferred.Promise();
743744
}
744745

746+
Napi::Value QueryResult::GetColumns(const Napi::CallbackInfo &info)
747+
{
748+
auto env = info.Env();
749+
auto result = Napi::Object::New(env);
750+
751+
for (duckdb::idx_t column_idx = 0; column_idx < this->result->ColumnCount(); column_idx++)
752+
{
753+
auto column_name = this->result->ColumnName(column_idx);
754+
auto column_type = this->result->types[column_idx];
755+
756+
auto logic_type = Napi::Object::New(env);
757+
logic_type.Set("id", Napi::Number::New(env, (double)column_type.id()));
758+
logic_type.Set("name", Napi::String::New(env, column_type.ToString()));
759+
760+
result.Set(column_name, logic_type);
761+
}
762+
763+
return result;
764+
}
765+
745766
Napi::Object QueryResult::NewInstance(const Napi::Object &db) {
746767
return NodeDuckDB::GetData(db.Env())->query_result_constructor.New({db});
747768
}

test/query_result.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ describe('QueryResult', () => {
1414

1515
it('streams results', async () => {
1616
let retrieved = 0;
17-
const stream = conn.stream('SELECT * FROM range(0, ?)', total);
17+
18+
const stream = await conn.stream("SELECT * FROM range(0, ?)", total);
19+
assert.deepEqual(stream.getColumns(), {
20+
range: {
21+
id: 14,
22+
name: "1",
23+
},
24+
});
25+
1826
for await (const row of stream) {
1927
retrieved++;
2028
}

test/typescript_decls.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe("typescript: stream and QueryResult", function () {
222222

223223
it("streams results", async () => {
224224
let retrieved = 0;
225-
const stream = conn.stream("SELECT * FROM range(0, ?)", total);
225+
const stream = await conn.stream("SELECT * FROM range(0, ?)", total);
226226
for await (const row of stream) {
227227
retrieved++;
228228
}

0 commit comments

Comments
 (0)