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 4b155784b..1b88b5878 100644 --- a/lib/src/webdb_api.cc +++ b/lib/src/webdb_api.cc @@ -210,6 +210,14 @@ void duckdb_web_pending_query_start(WASMResponse* packed, ConnectionHdl connHdl, auto r = c->PendingQuery(script, allow_stream_result); 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, bool allow_stream_result) { + auto c = reinterpret_cast(connHdl); + std::string_view S(reinterpret_cast(buffer), buffer_length, allow_stream_result); + auto r = c->PendingQuery(S, allow_stream_result); + 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 001dba4ae..4d836de98 100644 --- a/packages/duckdb-wasm/src/bindings/bindings_base.ts +++ b/packages/duckdb-wasm/src/bindings/bindings_base.ts @@ -190,19 +190,21 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings { * Results can then be fetched using `fetchQueryResults` */ public startPendingQuery(conn: number, text: string, allowStreamResult: boolean = false): Uint8Array | null { - const [s, d, n] = callSRet( - this.mod, - 'duckdb_web_pending_query_start', - ['number', 'string', 'boolean'], - [conn, text, allowStreamResult], - ); + 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', 'boolean'], [conn, bufferPtr, BUF.length, allowStreamResult]); 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; }