Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strings at interface /2 #1906

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 All @@ -327,6 +328,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, \
Expand Down
16 changes: 16 additions & 0 deletions lib/src/webdb_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ 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<WebDB::Connection*>(connHdl);
std::string_view script(reinterpret_cast<const char*>(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<WebDB::Connection*>(connHdl);
Expand Down Expand Up @@ -241,6 +249,14 @@ 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
14 changes: 12 additions & 2 deletions 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 Expand Up @@ -288,7 +293,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));
}
Expand Down