From 6b7661e16f094d95a1194f4c9ab830d66fceabea Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Wed, 30 Oct 2024 17:17:04 +0100 Subject: [PATCH 1/3] Create prepared to pass strings via malloc --- lib/CMakeLists.txt | 1 + lib/src/webdb_api.cc | 7 +++++++ packages/duckdb-wasm/src/bindings/bindings_base.ts | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index de246193d..72d76dfb8 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -327,6 +327,7 @@ if(EMSCRIPTEN) _duckdb_web_pending_query_start_buffer, \ _duckdb_web_prepared_close, \ _duckdb_web_prepared_create, \ + _duckdb_web_prepared_create_buffer, \ _duckdb_web_prepared_run, \ _duckdb_web_prepared_send, \ _duckdb_web_query_fetch_results, \ diff --git a/lib/src/webdb_api.cc b/lib/src/webdb_api.cc index 0c8593e6d..674137430 100644 --- a/lib/src/webdb_api.cc +++ b/lib/src/webdb_api.cc @@ -170,6 +170,13 @@ void duckdb_web_prepared_create(WASMResponse* packed, ConnectionHdl connHdl, con auto r = c->CreatePreparedStatement(script); WASMResponseBuffer::Get().Store(*packed, std::move(r)); } +/// Prepare a query statement +void duckdb_web_prepared_create_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length) { + auto c = reinterpret_cast(connHdl); + std::string_view script(reinterpret_cast(buffer), buffer_length); + auto r = c->CreatePreparedStatement(script); + WASMResponseBuffer::Get().Store(*packed, std::move(r)); +} /// Close a prepared statement void duckdb_web_prepared_close(WASMResponse* packed, ConnectionHdl connHdl, size_t statement_id) { 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 a1d047207..04d85c4d5 100644 --- a/packages/duckdb-wasm/src/bindings/bindings_base.ts +++ b/packages/duckdb-wasm/src/bindings/bindings_base.ts @@ -288,7 +288,12 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings { /** Prepare a statement and return its identifier */ public createPrepared(conn: number, text: string): number { - const [s, d, n] = callSRet(this.mod, 'duckdb_web_prepared_create', ['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_prepared_create_buffer', ['number', 'number', 'number'], [conn, bufferPtr, BUF.length]); + this.mod._free(bufferPtr); if (s !== StatusCode.SUCCESS) { throw new Error(readString(this.mod, d, n)); } From bad10f6799178c126f315f2f8dfd1baa4c0cc393 Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Wed, 30 Oct 2024 17:17:17 +0100 Subject: [PATCH 2/3] getTableNames to pass strings via malloc --- lib/CMakeLists.txt | 1 + lib/src/webdb_api.cc | 7 +++++++ packages/duckdb-wasm/src/bindings/bindings_base.ts | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 72d76dfb8..8cfc7a59e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -316,6 +316,7 @@ if(EMSCRIPTEN) _duckdb_web_get_feature_flags, \ _duckdb_web_get_global_file_info, \ _duckdb_web_get_tablenames, \ + _duckdb_web_get_tablenames_buffer, \ _duckdb_web_get_version, \ _duckdb_web_insert_arrow_from_ipc_stream, \ _duckdb_web_insert_csv_from_path, \ diff --git a/lib/src/webdb_api.cc b/lib/src/webdb_api.cc index 674137430..1ccb4c431 100644 --- a/lib/src/webdb_api.cc +++ b/lib/src/webdb_api.cc @@ -248,6 +248,13 @@ void duckdb_web_get_tablenames(WASMResponse* packed, ConnectionHdl connHdl, cons auto r = c->GetTableNames(query); WASMResponseBuffer::Get().Store(*packed, std::move(r)); } +/// Get table names +void duckdb_web_get_tablenames_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length) { + auto c = reinterpret_cast(connHdl); + std::string_view query(reinterpret_cast(buffer), buffer_length); + auto r = c->GetTableNames(query); + WASMResponseBuffer::Get().Store(*packed, std::move(r)); +} /// Insert arrow from an ipc stream void duckdb_web_insert_arrow_from_ipc_stream(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length, const char* options) { diff --git a/packages/duckdb-wasm/src/bindings/bindings_base.ts b/packages/duckdb-wasm/src/bindings/bindings_base.ts index 04d85c4d5..70d5475cf 100644 --- a/packages/duckdb-wasm/src/bindings/bindings_base.ts +++ b/packages/duckdb-wasm/src/bindings/bindings_base.ts @@ -233,7 +233,12 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings { } /** Get table names */ public getTableNames(conn: number, text: string): string[] { - const [s, d, n] = callSRet(this.mod, 'duckdb_web_get_tablenames', ['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_get_tablenames_buffer', ['number', 'number', 'number'], [conn, bufferPtr, BUF.length]); + this.mod._free(bufferPtr); if (s !== StatusCode.SUCCESS) { throw new Error(readString(this.mod, d, n)); } From 9f0ebd30a7f0c43e4b6a8f2000d736903a61f35d Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Wed, 30 Oct 2024 18:52:25 +0100 Subject: [PATCH 3/3] Format fix webdb_api --- lib/src/webdb_api.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/webdb_api.cc b/lib/src/webdb_api.cc index 1ccb4c431..bad9c7f9e 100644 --- a/lib/src/webdb_api.cc +++ b/lib/src/webdb_api.cc @@ -171,7 +171,8 @@ void duckdb_web_prepared_create(WASMResponse* packed, ConnectionHdl connHdl, con WASMResponseBuffer::Get().Store(*packed, std::move(r)); } /// Prepare a query statement -void duckdb_web_prepared_create_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length) { +void duckdb_web_prepared_create_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, + size_t buffer_length) { auto c = reinterpret_cast(connHdl); std::string_view script(reinterpret_cast(buffer), buffer_length); auto r = c->CreatePreparedStatement(script); @@ -249,7 +250,8 @@ void duckdb_web_get_tablenames(WASMResponse* packed, ConnectionHdl connHdl, cons WASMResponseBuffer::Get().Store(*packed, std::move(r)); } /// Get table names -void duckdb_web_get_tablenames_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length) { +void duckdb_web_get_tablenames_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, + size_t buffer_length) { auto c = reinterpret_cast(connHdl); std::string_view query(reinterpret_cast(buffer), buffer_length); auto r = c->GetTableNames(query);