Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ bun install chdb-bun

#### Usage
```js
import { Execute } from 'chdb-bun';
console.log(Execute("SELECT version()", "CSV"));
// "23.10.1.1"
```
import { db, chdb } from 'chdb-bun';

const conn = new db('CSV')
var result;

// Test query
result = conn.query("SELECT version()");
console.log(result)

// Test session
conn.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
result = conn.session("SELECT hello()", "CSV");
console.log(result)
```
13 changes: 11 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Execute } from '.';
import { db, chdb } from '.';

const conn = new db('CSV', '/tmp')
var result;

console.log(Execute("SELECT version()", "CSV"));
// Test query
result = conn.query("SELECT version()");
console.log(result)

// Test session
conn.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
result = conn.session("SELECT hello()", "CSV");
console.log(result)
27 changes: 23 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@

import { dlopen, FFIType, suffix, CString, ptr } from "bun:ffi";

const path = `lib/libchdb_bun.${suffix}`;

const { symbols: chdb, } = dlopen(path, {
Execute: {
args: [FFIType.cstring, FFIType.cstring],
returns:FFIType.cstring,
},
ExecuteSession: {
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
returns:FFIType.cstring,
},
},
);

export function Execute(query, format){
if (!format) format = "CSV";
if (!query) return "";
return chdb.Execute(Buffer.from(query+"\0"), Buffer.from(format+"\0"));
function db(format, path) {
this.format = format || 'JSONCompact';
this.path = path || '.';
this.query = function(query, format){
if (!query) return "";
if (!format) format = "CSV";
return chdb.Execute(Buffer.from(query+"\0"), Buffer.from(format+"\0"));
}.bind(this);
this.session = function(query, format, path) {
if (!query) return "";
if (!format) format = "CSV";
if (!path) path = "/tmp";
return chdb.ExecuteSession(Buffer.from(query+"\0"), Buffer.from(format+"\0"), Buffer.from(path+"\0"));
}.bind(this);
return this;
}

export { chdb, db };
42 changes: 41 additions & 1 deletion lib/libchdb_bun.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,45 @@ char *Execute(char *query, char *format) {
free(argv[2]);
free(argv[3]);

return result->buf;
if (result == NULL) {
return NULL;
} else {
return result->buf;
}
}

char *ExecuteSession(char *query, char *format, char *path) {
char *argv[] = {(char *)"clickhouse", (char *)"--multiquery", (char *)"--output-format=CSV", (char *)"--query=", (char *)"--path=."};
char dataFormat[100];
char dataPath[100];
char *localQuery;
int argc = 5;
struct local_result *result;

snprintf(dataFormat, sizeof(dataFormat), "--format=%s", format);
argv[2] = strdup(dataFormat);

snprintf(dataPath, sizeof(dataPath), "--path=%s", path);
argv[4] = strdup(dataPath);

localQuery = (char *) malloc(strlen(query) + 10);
if (localQuery == NULL) {
return NULL;
}

sprintf(localQuery, "--query=%s", query);
argv[3] = strdup(localQuery);
free(localQuery);

result = query_stable(argc, argv);

free(argv[2]);
free(argv[3]);
free(argv[4]);

if (result == NULL) {
return NULL;
} else {
return result->buf;
}
}
1 change: 1 addition & 0 deletions lib/libchdb_bun.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define LIBCHDB_BUN_H

char *Execute(char *query, char *format);
char *ExecuteSession(char *query, char *format, char *path);

#endif
Binary file modified lib/libchdb_bun.so
Binary file not shown.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"name": "chdb-bun",
"version": "1.0.4",
"module": "index.js",
"type": "module",
"author": "Lorenzo Mangani <lorenzo.mangani@gmail.com>",
"license": "Apache2.0",
"scripts": {
"build": "cd lib && gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -lchdb"
},
"devDependencies": {
"bun-types": "^0.5.0"
},
"directories":{
"lib": "lib"
}
}