Skip to content

Commit f6044a3

Browse files
committed
Fix session class
1 parent 87392dd commit f6044a3

File tree

11 files changed

+164
-119
lines changed

11 files changed

+164
-119
lines changed

lib/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
## libchdb `Execute` function binding
1+
## libchdb `Query` function binding
22

33
```bash
4+
# install dependencies
5+
./update_libchdb.sh
6+
47
# build the dynamic library
58

69
./build.sh

lib/binding.bun

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/build.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
#!/bin/bash
2+
13
if [ "$(uname)" == "Darwin" ]; then
2-
gcc -dynamiclib -o libchdb_bun.dylib -L. -lchdb libchdb_bun.c
4+
clang -O3 -dynamiclib -o chdb_bun.so -L. -lchdb chdb_bun.c
35
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
4-
gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -L. -lchdb
6+
clang -O3 -shared -fPIC -o chdb_bun.so -L. -lchdb chdb_bun.c
57
else
68
echo "Unsupported operating system"
79
exit 1
8-
fi
10+
fi
11+
12+
mv chdb_bun.so ../chdb_bun.so
13+
mv libchdb.so ../libchdb.so

lib/chdb.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
# include <cstddef>
5+
# include <cstdint>
6+
extern "C" {
7+
#else
8+
# include <stddef.h>
9+
# include <stdint.h>
10+
#endif
11+
12+
#define CHDB_EXPORT __attribute__((visibility("default")))
13+
struct CHDB_EXPORT local_result
14+
{
15+
char * buf;
16+
size_t len;
17+
void * _vec; // std::vector<char> *, for freeing
18+
double elapsed;
19+
uint64_t rows_read;
20+
uint64_t bytes_read;
21+
};
22+
23+
CHDB_EXPORT struct local_result * query_stable(int argc, char ** argv);
24+
CHDB_EXPORT void free_result(struct local_result * result);
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif

lib/chdb_bun.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "chdb.h"
2+
#include "chdb_bun.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
#define MAX_FORMAT_LENGTH 64
8+
#define MAX_PATH_LENGTH 4096
9+
#define MAX_ARG_COUNT 6
10+
11+
// Utility function to construct argument string
12+
void construct_arg(char *dest, const char *prefix, const char *value,
13+
size_t dest_size) {
14+
snprintf(dest, dest_size, "%s%s", prefix, value);
15+
}
16+
17+
// Generalized query function
18+
char *general_query(int argc, char *args[]) {
19+
struct local_result *result = query_stable(argc, args);
20+
21+
if (result == NULL) {
22+
return NULL;
23+
} else {
24+
return result->buf;
25+
}
26+
}
27+
28+
// Query function without session
29+
char *Query(const char *query, const char *format) {
30+
char dataFormat[MAX_FORMAT_LENGTH];
31+
char *dataQuery;
32+
char *args[MAX_ARG_COUNT] = {"clickhouse", "--multiquery", NULL, NULL};
33+
int argc = 4;
34+
35+
construct_arg(dataFormat, "--output-format=", format, MAX_FORMAT_LENGTH);
36+
args[2] = dataFormat;
37+
38+
dataQuery = (char *)malloc(strlen(query) + strlen("--query=") + 1);
39+
if (dataQuery == NULL) {
40+
return NULL;
41+
}
42+
construct_arg(dataQuery, "--query=", query,
43+
strlen(query) + strlen("--query=") + 1);
44+
args[3] = dataQuery;
45+
46+
char *result = general_query(argc, args);
47+
free(dataQuery);
48+
return result;
49+
}
50+
51+
// QuerySession function will save the session to the path
52+
// queries with same path will use the same session
53+
char *QuerySession(const char *query, const char *format, const char *path) {
54+
char dataFormat[MAX_FORMAT_LENGTH];
55+
char dataPath[MAX_PATH_LENGTH];
56+
char *dataQuery;
57+
char *args[MAX_ARG_COUNT] = {"clickhouse", "--multiquery", NULL, NULL, NULL};
58+
int argc = 5;
59+
60+
construct_arg(dataFormat, "--output-format=", format, MAX_FORMAT_LENGTH);
61+
args[2] = dataFormat;
62+
63+
dataQuery = (char *)malloc(strlen(query) + strlen("--query=") + 1);
64+
if (dataQuery == NULL) {
65+
return NULL;
66+
}
67+
construct_arg(dataQuery, "--query=", query,
68+
strlen(query) + strlen("--query=") + 1);
69+
args[3] = dataQuery;
70+
71+
construct_arg(dataPath, "--path=", path, MAX_PATH_LENGTH);
72+
args[4] = dataPath;
73+
74+
char *result = general_query(argc, args);
75+
free(dataQuery);
76+
return result;
77+
}

lib/chdb_bun.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
char *Query(const char *query, const char *format);
4+
char *QuerySession(const char *query, const char *format, const char *path);

lib/libchdb.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/libchdb_bun.c

Lines changed: 0 additions & 71 deletions
This file was deleted.

lib/libchdb_bun.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/libchdb_bun.so

-15.9 KB
Binary file not shown.

lib/update_libchdb.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
#!/bin/bash
3+
4+
# Get the newest release version
5+
LATEST_RELEASE=$(curl --silent "https://api.github.com/repos/chdb-io/chdb/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
6+
7+
# Download the correct version based on the platform
8+
case "$(uname -s)" in
9+
Linux)
10+
if [[ $(uname -m) == "aarch64" ]]; then
11+
PLATFORM="linux-aarch64-libchdb.tar.gz"
12+
else
13+
PLATFORM="linux-x86_64-libchdb.tar.gz"
14+
fi
15+
;;
16+
Darwin)
17+
if [[ $(uname -m) == "arm64" ]]; then
18+
PLATFORM="macos-arm64-libchdb.tar.gz"
19+
else
20+
PLATFORM="macos-x86_64-libchdb.tar.gz"
21+
fi
22+
;;
23+
*)
24+
echo "Unsupported platform"
25+
exit 1
26+
;;
27+
esac
28+
29+
DOWNLOAD_URL="https://github.com/chdb-io/chdb/releases/download/$LATEST_RELEASE/$PLATFORM"
30+
31+
echo "Downloading $PLATFORM from $DOWNLOAD_URL"
32+
33+
# Download the file
34+
curl -L -o libchdb.tar.gz $DOWNLOAD_URL
35+
36+
# Untar the file
37+
tar -xzf libchdb.tar.gz
38+
39+
# Set execute permission for libchdb.so
40+
chmod +x libchdb.so
41+
42+
# Clean up
43+
rm -f libchdb.tar.gz

0 commit comments

Comments
 (0)