Skip to content

Commit 571faa6

Browse files
committed
Fix Session
1 parent 3eb2d2b commit 571faa6

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

index.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
1-
import { dlopen, FFIType, suffix, CString, ptr } from "bun:ffi";
1+
import { dlopen, FFIType } from "bun:ffi";
2+
import fs from "fs";
23

3-
const path = `lib/libchdb_bun.${suffix}`;
4+
const path = `chdb_bun.so`;
45

56
const { symbols: chdb } = dlopen(path, {
6-
Execute: {
7+
Query: {
78
args: [FFIType.cstring, FFIType.cstring],
89
returns: FFIType.cstring,
910
},
10-
ExecuteSession: {
11+
QuerySession: {
1112
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
1213
returns: FFIType.cstring,
1314
},
1415
});
1516

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 {
1827
path: string;
28+
isTemp: boolean;
29+
1930
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") {
2631
if (!query) return "";
27-
return chdb.ExecuteSession(
32+
return chdb.QuerySession(
2833
Buffer.from(query + "\0"),
2934
Buffer.from(format + "\0"),
30-
Buffer.from(path + "\0")
35+
Buffer.from(this.path + "\0")
3136
);
3237
}
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 });
3653
}
3754
}
3855

39-
export { chdb, db };
56+
export { chdb, Session };

0 commit comments

Comments
 (0)