From 5c08f060de92a9c76d8761ac2f4151238e2eec8f Mon Sep 17 00:00:00 2001 From: Roj Date: Sun, 25 Aug 2024 21:08:08 +0300 Subject: [PATCH 1/2] Add Deno support --- mod.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 mod.ts diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..87b9c92 --- /dev/null +++ b/mod.ts @@ -0,0 +1,61 @@ +const path = "chdb_bun.so"; + +const { symbols: chdb } = Deno.dlopen(path, { + Query: { + parameters: ["buffer", "buffer"], + result: "buffer", + }, + QuerySession: { + parameters: ["buffer", "buffer", "buffer"], + result: "buffer", + }, +}); + +const enc = new TextEncoder(); +// Standalone exported query function +export function query(query: string, format: string = "CSV") { + if (!query) { + return ""; + } + const result = chdb.Query( + enc.encode(query + "\0"), + enc.encode(format + "\0"), + ); + if (result == null) { + return ""; + } + return new Deno.UnsafePointerView(result).getCString(); +} + +// Session class with path handling +class Session { + path: string; + isTemp: boolean; + + query(query: string, format: string = "CSV") { + if (!query) return ""; + return chdb.QuerySession( + enc.encode(query + "\0"), + enc.encode(format + "\0"), + enc.encode(this.path + "\0"), + ); + } + + constructor(path: string = "") { + if (path === "") { + // Create a temporary directory + this.path = Deno.makeTempDirSync(); + this.isTemp = true; + } else { + this.path = path; + this.isTemp = false; + } + } + + // Cleanup method to delete the temporary directory + cleanup() { + Deno.removeSync(this.path, { recursive: true }); + } +} + +export { chdb, Session }; From 87dd1348b00bb76ea4c9bad066331935ac70eaeb Mon Sep 17 00:00:00 2001 From: Roj Date: Mon, 26 Aug 2024 09:01:25 +0300 Subject: [PATCH 2/2] Add an example and fix a bug --- example.deno.ts | 18 ++++++++++++++++++ mod.ts | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 example.deno.ts diff --git a/example.deno.ts b/example.deno.ts new file mode 100644 index 0000000..a37de8b --- /dev/null +++ b/example.deno.ts @@ -0,0 +1,18 @@ +// Run with `deno run --unstable-ffi -A example.deno.ts` +import { query, Session } from "./mod.ts"; + +// Create a new session instance +const session = new Session("./chdb-bun-tmp"); +let result; + +// Test standalone query +result = query("SELECT version(), 'Hello chDB', chdb()", "CSV"); +console.log(result); + +// Test session query +session.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'", "CSV"); +result = session.query("SELECT hello()", "CSV"); +console.log(result); + +// Clean up the session +session.cleanup(); diff --git a/mod.ts b/mod.ts index 87b9c92..b20e550 100644 --- a/mod.ts +++ b/mod.ts @@ -34,11 +34,15 @@ class Session { query(query: string, format: string = "CSV") { if (!query) return ""; - return chdb.QuerySession( + const result = chdb.QuerySession( enc.encode(query + "\0"), enc.encode(format + "\0"), enc.encode(this.path + "\0"), ); + if (result == null) { + return ""; + } + return new Deno.UnsafePointerView(result).getCString(); } constructor(path: string = "") {