Skip to content

Commit

Permalink
getTableNames to pass strings via malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Oct 30, 2024
1 parent 9173599 commit 9c8b5b5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
7 changes: 7 additions & 0 deletions lib/src/webdb_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebDB::Connection*>(connHdl);
std::string_view query(reinterpret_cast<const char*>(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) {
Expand Down
7 changes: 6 additions & 1 deletion packages/duckdb-wasm/src/bindings/bindings_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 9c8b5b5

Please sign in to comment.