|
1 |
| -import { dlopen, FFIType, suffix, CString, ptr } from "bun:ffi"; |
| 1 | +import { dlopen, FFIType } from "bun:ffi"; |
| 2 | +import fs from "fs"; |
2 | 3 |
|
3 |
| -const path = `lib/libchdb_bun.${suffix}`; |
| 4 | +const path = `chdb_bun.so`; |
4 | 5 |
|
5 | 6 | const { symbols: chdb } = dlopen(path, {
|
6 |
| - Execute: { |
| 7 | + Query: { |
7 | 8 | args: [FFIType.cstring, FFIType.cstring],
|
8 | 9 | returns: FFIType.cstring,
|
9 | 10 | },
|
10 |
| - ExecuteSession: { |
| 11 | + QuerySession: { |
11 | 12 | args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
|
12 | 13 | returns: FFIType.cstring,
|
13 | 14 | },
|
14 | 15 | });
|
15 | 16 |
|
16 |
| -class db { |
17 |
| - format: string; |
| 17 | +// Standalone exported query function |
| 18 | +export function query(query: string, format: string = "CSV") { |
| 19 | + if (!query) { |
| 20 | + return ""; |
| 21 | + } |
| 22 | + return chdb.Query(Buffer.from(query + "\0"), Buffer.from(format + "\0")); |
| 23 | +} |
| 24 | + |
| 25 | +// Session class with path handling |
| 26 | +class Session { |
18 | 27 | path: string;
|
| 28 | + isTemp: boolean; |
| 29 | + |
19 | 30 | query(query: string, format: string = "CSV") {
|
20 |
| - if (!query) { |
21 |
| - return ""; |
22 |
| - } |
23 |
| - return chdb.Execute(Buffer.from(query + "\0"), Buffer.from(format + "\0")); |
24 |
| - } |
25 |
| - session(query: string, format: string = "CSV", path: string = "/tmp") { |
26 | 31 | if (!query) return "";
|
27 |
| - return chdb.ExecuteSession( |
| 32 | + return chdb.QuerySession( |
28 | 33 | Buffer.from(query + "\0"),
|
29 | 34 | Buffer.from(format + "\0"),
|
30 |
| - Buffer.from(path + "\0") |
| 35 | + Buffer.from(this.path + "\0") |
31 | 36 | );
|
32 | 37 | }
|
33 |
| - constructor(format: string = "JSONCompact", path: string = ".") { |
34 |
| - this.format = format; |
35 |
| - this.path = path; |
| 38 | + |
| 39 | + constructor(path: string = "") { |
| 40 | + if (path === "") { |
| 41 | + // Create a temporary directory |
| 42 | + this.path = fs.mkdtempSync("tmp-"); |
| 43 | + this.isTemp = true; |
| 44 | + } else { |
| 45 | + this.path = path; |
| 46 | + this.isTemp = false; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Cleanup method to delete the temporary directory |
| 51 | + cleanup() { |
| 52 | + fs.rmdirSync(this.path, { recursive: true }); |
36 | 53 | }
|
37 | 54 | }
|
38 | 55 |
|
39 |
| -export { chdb, db }; |
| 56 | +export { chdb, Session }; |
0 commit comments