|
| 1 | +const path = "chdb_bun.so"; |
| 2 | + |
| 3 | +const { symbols: chdb } = Deno.dlopen(path, { |
| 4 | + Query: { |
| 5 | + parameters: ["buffer", "buffer"], |
| 6 | + result: "buffer", |
| 7 | + }, |
| 8 | + QuerySession: { |
| 9 | + parameters: ["buffer", "buffer", "buffer"], |
| 10 | + result: "buffer", |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +const enc = new TextEncoder(); |
| 15 | +// Standalone exported query function |
| 16 | +export function query(query: string, format: string = "CSV") { |
| 17 | + if (!query) { |
| 18 | + return ""; |
| 19 | + } |
| 20 | + const result = chdb.Query( |
| 21 | + enc.encode(query + "\0"), |
| 22 | + enc.encode(format + "\0"), |
| 23 | + ); |
| 24 | + if (result == null) { |
| 25 | + return ""; |
| 26 | + } |
| 27 | + return new Deno.UnsafePointerView(result).getCString(); |
| 28 | +} |
| 29 | + |
| 30 | +// Session class with path handling |
| 31 | +class Session { |
| 32 | + path: string; |
| 33 | + isTemp: boolean; |
| 34 | + |
| 35 | + query(query: string, format: string = "CSV") { |
| 36 | + if (!query) return ""; |
| 37 | + return chdb.QuerySession( |
| 38 | + enc.encode(query + "\0"), |
| 39 | + enc.encode(format + "\0"), |
| 40 | + enc.encode(this.path + "\0"), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + constructor(path: string = "") { |
| 45 | + if (path === "") { |
| 46 | + // Create a temporary directory |
| 47 | + this.path = Deno.makeTempDirSync(); |
| 48 | + this.isTemp = true; |
| 49 | + } else { |
| 50 | + this.path = path; |
| 51 | + this.isTemp = false; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Cleanup method to delete the temporary directory |
| 56 | + cleanup() { |
| 57 | + Deno.removeSync(this.path, { recursive: true }); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export { chdb, Session }; |
0 commit comments