Skip to content

Commit 28f1c01

Browse files
authored
Merge pull request #1 from chdb-io/latest
resync latest
2 parents 1036f42 + e254421 commit 28f1c01

File tree

7 files changed

+95
-11
lines changed

7 files changed

+95
-11
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,17 @@ bun install chdb-bun
1919

2020
#### Usage
2121
```js
22-
import { Execute } from 'chdb-bun';
23-
console.log(Execute("SELECT version()", "CSV"));
24-
// "23.10.1.1"
25-
```
22+
import { db, chdb } from 'chdb-bun';
23+
24+
const conn = new db('CSV')
25+
var result;
2626

27+
// Test query
28+
result = conn.query("SELECT version()");
29+
console.log(result)
30+
31+
// Test session
32+
conn.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
33+
result = conn.session("SELECT hello()", "CSV");
34+
console.log(result)
35+
```

example.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Execute } from '.';
1+
import { db, chdb } from '.';
22

3+
const conn = new db('CSV', '/tmp')
4+
var result;
35

4-
console.log(Execute("SELECT version()", "CSV"));
6+
// Test query
7+
result = conn.query("SELECT version()");
8+
console.log(result)
9+
10+
// Test session
11+
conn.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
12+
result = conn.session("SELECT hello()", "CSV");
13+
console.log(result)

index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11

22
import { dlopen, FFIType, suffix, CString, ptr } from "bun:ffi";
3+
34
const path = `lib/libchdb_bun.${suffix}`;
5+
46
const { symbols: chdb, } = dlopen(path, {
57
Execute: {
68
args: [FFIType.cstring, FFIType.cstring],
79
returns:FFIType.cstring,
810
},
11+
ExecuteSession: {
12+
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
13+
returns:FFIType.cstring,
14+
},
915
},
1016
);
1117

12-
export function Execute(query, format){
13-
if (!format) format = "CSV";
14-
if (!query) return "";
15-
return chdb.Execute(Buffer.from(query+"\0"), Buffer.from(format+"\0"));
18+
function db(format, path) {
19+
this.format = format || 'JSONCompact';
20+
this.path = path || '.';
21+
this.query = function(query, format){
22+
if (!query) return "";
23+
if (!format) format = "CSV";
24+
return chdb.Execute(Buffer.from(query+"\0"), Buffer.from(format+"\0"));
25+
}.bind(this);
26+
this.session = function(query, format, path) {
27+
if (!query) return "";
28+
if (!format) format = "CSV";
29+
if (!path) path = "/tmp";
30+
return chdb.ExecuteSession(Buffer.from(query+"\0"), Buffer.from(format+"\0"), Buffer.from(path+"\0"));
31+
}.bind(this);
32+
return this;
1633
}
34+
35+
export { chdb, db };

lib/libchdb_bun.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,45 @@ char *Execute(char *query, char *format) {
2727
free(argv[2]);
2828
free(argv[3]);
2929

30-
return result->buf;
30+
if (result == NULL) {
31+
return NULL;
32+
} else {
33+
return result->buf;
34+
}
35+
}
36+
37+
char *ExecuteSession(char *query, char *format, char *path) {
38+
char *argv[] = {(char *)"clickhouse", (char *)"--multiquery", (char *)"--output-format=CSV", (char *)"--query=", (char *)"--path=."};
39+
char dataFormat[100];
40+
char dataPath[100];
41+
char *localQuery;
42+
int argc = 5;
43+
struct local_result *result;
44+
45+
snprintf(dataFormat, sizeof(dataFormat), "--format=%s", format);
46+
argv[2] = strdup(dataFormat);
47+
48+
snprintf(dataPath, sizeof(dataPath), "--path=%s", path);
49+
argv[4] = strdup(dataPath);
50+
51+
localQuery = (char *) malloc(strlen(query) + 10);
52+
if (localQuery == NULL) {
53+
return NULL;
54+
}
55+
56+
sprintf(localQuery, "--query=%s", query);
57+
argv[3] = strdup(localQuery);
58+
free(localQuery);
59+
60+
result = query_stable(argc, argv);
61+
62+
free(argv[2]);
63+
free(argv[3]);
64+
free(argv[4]);
65+
66+
if (result == NULL) {
67+
return NULL;
68+
} else {
69+
return result->buf;
70+
}
3171
}

lib/libchdb_bun.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define LIBCHDB_BUN_H
33

44
char *Execute(char *query, char *format);
5+
char *ExecuteSession(char *query, char *format, char *path);
56

67
#endif

lib/libchdb_bun.so

-248 Bytes
Binary file not shown.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
{
22
"name": "chdb-bun",
3+
"version": "1.0.4",
34
"module": "index.js",
45
"type": "module",
6+
"author": "Lorenzo Mangani <[email protected]>",
7+
"license": "Apache2.0",
58
"scripts": {
69
"build": "cd lib && gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -lchdb"
710
},
811
"devDependencies": {
912
"bun-types": "^0.5.0"
13+
},
14+
"directories":{
15+
"lib": "lib"
1016
}
1117
}

0 commit comments

Comments
 (0)