Skip to content

Commit 0cc4a33

Browse files
committed
Add an example and fix the bug
1 parent 5c08f06 commit 0cc4a33

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

example.deno.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Run with `deno run --unstable-ffi -A example.deno.ts`
2+
import { query, Session } from "./mod.ts";
3+
4+
// Create a new session instance
5+
const session = new Session("./chdb-bun-tmp");
6+
let result;
7+
8+
// Test standalone query
9+
result = query("SELECT version(), 'Hello chDB', chdb()", "CSV");
10+
console.log(result);
11+
12+
// Test session query
13+
session.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'", "CSV");
14+
result = session.query("SELECT hello()", "CSV");
15+
console.log(result);
16+
17+
// Clean up the session
18+
session.cleanup();

mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ class Session {
3434

3535
query(query: string, format: string = "CSV") {
3636
if (!query) return "";
37-
return chdb.QuerySession(
37+
const result = chdb.QuerySession(
3838
enc.encode(query + "\0"),
3939
enc.encode(format + "\0"),
4040
enc.encode(this.path + "\0"),
4141
);
42+
if (result == null) {
43+
return "";
44+
}
45+
return new Deno.UnsafePointerView(result).getCString();
4246
}
4347

4448
constructor(path: string = "") {

0 commit comments

Comments
 (0)