Skip to content

Commit

Permalink
feat: Expose LogicalType(s) for columns in QueryResult, fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Nov 2, 2023
1 parent 2722553 commit f0e40e7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/duckdb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ export class Connection {
unregister_buffer(name: string, callback?: Callback<void>): void;
}

export type LogicalType = {
id: number,
name: string,
}

export class QueryResult implements AsyncIterable<RowData> {
getColumns(): Record<string, LogicalType>;
[Symbol.asyncIterator](): AsyncIterator<RowData>;
}

Expand Down
14 changes: 9 additions & 5 deletions lib/duckdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ QueryResult.prototype.nextChunk;
*/
QueryResult.prototype.nextIpcBuffer;

/**
* Function to return logical types for columns
*
* @method
*/
QueryResult.prototype.getColumns;

/**
* @name asyncIterator
* @memberof module:duckdb~QueryResult
Expand Down Expand Up @@ -218,12 +225,9 @@ Connection.prototype.each = function (sql) {
* @param {...*} params
* @yields row chunks
*/
Connection.prototype.stream = async function* (sql) {
Connection.prototype.stream = async function (sql) {
const statement = new Statement(this, sql);
const queryResult = await statement.stream.apply(statement, arguments);
for await (const result of queryResult) {
yield result;
}
return statement.stream.apply(statement, arguments);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/duckdb_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class QueryResult : public Napi::ObjectWrap<QueryResult> {
public:
Napi::Value NextChunk(const Napi::CallbackInfo &info);
Napi::Value NextIpcBuffer(const Napi::CallbackInfo &info);
Napi::Value GetColumns(const Napi::CallbackInfo &info);
duckdb::shared_ptr<ArrowSchema> cschema;

private:
Expand Down
23 changes: 22 additions & 1 deletion src/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ Napi::FunctionReference QueryResult::Init(Napi::Env env, Napi::Object exports) {

Napi::Function t = DefineClass(env, "QueryResult",
{InstanceMethod("nextChunk", &QueryResult::NextChunk),
InstanceMethod("nextIpcBuffer", &QueryResult::NextIpcBuffer)});
InstanceMethod("nextIpcBuffer", &QueryResult::NextIpcBuffer),
InstanceMethod("getColumns", &QueryResult::GetColumns)});

exports.Set("QueryResult", t);

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

Napi::Value QueryResult::GetColumns(const Napi::CallbackInfo &info)
{
auto env = info.Env();
auto result = Napi::Object::New(env);

for (duckdb::idx_t column_idx = 0; column_idx < this->result->ColumnCount(); column_idx++)
{
auto column_name = this->result->ColumnName(column_idx);
auto column_type = this->result->types[column_idx];

auto logic_type = Napi::Object::New(env);
logic_type.Set("id", Napi::Number::New(env, (double)column_type.id()));
logic_type.Set("name", Napi::String::New(env, column_type.ToString()));

result.Set(column_name, logic_type);
}

return result;
}

Napi::Object QueryResult::NewInstance(const Napi::Object &db) {
return NodeDuckDB::GetData(db.Env())->query_result_constructor.New({db});
}
Expand Down
10 changes: 9 additions & 1 deletion test/query_result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ describe('QueryResult', () => {

it('streams results', async () => {
let retrieved = 0;
const stream = conn.stream('SELECT * FROM range(0, ?)', total);

const stream = await conn.stream("SELECT * FROM range(0, ?)", total);
assert.deepEqual(stream.getColumns(), {
range: {
id: 14,
name: "1",
},
});

for await (const row of stream) {
retrieved++;
}
Expand Down
2 changes: 1 addition & 1 deletion test/typescript_decls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe("typescript: stream and QueryResult", function () {

it("streams results", async () => {
let retrieved = 0;
const stream = conn.stream("SELECT * FROM range(0, ?)", total);
const stream = await conn.stream("SELECT * FROM range(0, ?)", total);
for await (const row of stream) {
retrieved++;
}
Expand Down

0 comments on commit f0e40e7

Please sign in to comment.