From fa138416d8a67f8bf6c4c6d4d910c0e8907a2218 Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Wed, 30 Oct 2024 13:31:43 +0100 Subject: [PATCH] Move from duckdb_web_pending_query_start to duckdb_web_pending_query_start_buffer --- lib/CMakeLists.txt | 1 + lib/src/webdb_api.cc | 8 ++++++++ packages/duckdb-wasm/src/bindings/bindings_base.ts | 9 ++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 949dfe141..de246193d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -324,6 +324,7 @@ if(EMSCRIPTEN) _duckdb_web_pending_query_cancel, \ _duckdb_web_pending_query_poll, \ _duckdb_web_pending_query_start, \ + _duckdb_web_pending_query_start_buffer, \ _duckdb_web_prepared_close, \ _duckdb_web_prepared_create, \ _duckdb_web_prepared_run, \ diff --git a/lib/src/webdb_api.cc b/lib/src/webdb_api.cc index 3e477a68b..9fa89e4dd 100644 --- a/lib/src/webdb_api.cc +++ b/lib/src/webdb_api.cc @@ -209,6 +209,14 @@ void duckdb_web_pending_query_start(WASMResponse* packed, ConnectionHdl connHdl, auto r = c->PendingQuery(script); WASMResponseBuffer::Get().Store(*packed, std::move(r)); } +/// Start a pending query +void duckdb_web_pending_query_start_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, + size_t buffer_length) { + auto c = reinterpret_cast(connHdl); + std::string_view S(reinterpret_cast(buffer), buffer_length); + auto r = c->PendingQuery(S); + WASMResponseBuffer::Get().Store(*packed, std::move(r)); +} /// Poll a pending query void duckdb_web_pending_query_poll(WASMResponse* packed, ConnectionHdl connHdl, const char* script) { auto c = reinterpret_cast(connHdl); diff --git a/packages/duckdb-wasm/src/bindings/bindings_base.ts b/packages/duckdb-wasm/src/bindings/bindings_base.ts index e8b1decde..6091be605 100644 --- a/packages/duckdb-wasm/src/bindings/bindings_base.ts +++ b/packages/duckdb-wasm/src/bindings/bindings_base.ts @@ -190,14 +190,21 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings { * Results can then be fetched using `fetchQueryResults` */ public startPendingQuery(conn: number, text: string): Uint8Array | null { - const [s, d, n] = callSRet(this.mod, 'duckdb_web_pending_query_start', ['number', 'string'], [conn, text]); + const BUF = TEXT_ENCODER.encode(text); + const bufferPtr = this.mod._malloc(BUF.length ); + const bufferOfs = this.mod.HEAPU8.subarray(bufferPtr, bufferPtr + BUF.length ); + bufferOfs.set(BUF); + const [s, d, n] = callSRet(this.mod, 'duckdb_web_pending_query_start_buffer', ['number', 'number', 'number'], [conn, bufferPtr, BUF.length]); if (s !== StatusCode.SUCCESS) { + this.mod._free(bufferPtr); throw new Error(readString(this.mod, d, n)); } if (d == 0) { + this.mod._free(bufferPtr); return null; } const res = copyBuffer(this.mod, d, n); + this.mod._free(bufferPtr); dropResponseBuffers(this.mod); return res; }