Skip to content

Commit c37c4ee

Browse files
bump chdb version
1 parent e203b48 commit c37c4ee

File tree

7 files changed

+271
-66
lines changed

7 files changed

+271
-66
lines changed

chdb-purego/binding.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chdbpurego
33
import (
44
"os"
55
"os/exec"
6+
"unsafe"
67

78
"github.com/ebitengine/purego"
89
)
@@ -35,6 +36,7 @@ func findLibrary() string {
3536
}
3637

3738
var (
39+
// old API
3840
queryStable func(argc int, argv []string) *local_result
3941
freeResult func(result *local_result)
4042
queryStableV2 func(argc int, argv []string) *local_result_v2
@@ -47,6 +49,23 @@ var (
4749
streamingResultNext func(conn *chdb_conn, result *chdb_streaming_result) *local_result_v2
4850
streamingResultDestroy func(result *chdb_streaming_result)
4951
streamingResultCancel func(conn *chdb_conn, result *chdb_streaming_result)
52+
53+
// new API
54+
chdbConnect func(argc int, argv []*byte) *chdb_connection
55+
chdbCloseConn func(conn unsafe.Pointer)
56+
chdbQuery func(conn unsafe.Pointer, query string, format string) *chdb_result
57+
chdbStreamQuery func(conn unsafe.Pointer, query string, format string) *chdb_result
58+
chdbStreamFetchResult func(conn unsafe.Pointer, result *chdb_result) *chdb_result
59+
chdbStreamCancelQuery func(conn unsafe.Pointer, result *chdb_result)
60+
chdbDestroyQueryResult func(result *chdb_result)
61+
chdbResultBuffer func(result *chdb_result) *byte
62+
chdbResultLen func(result *chdb_result) uint //size_t
63+
chdbResultElapsed func(result *chdb_result) float64 // double
64+
chdbResultRowsRead func(result *chdb_result) uint64
65+
chdbResultBytesRead func(result *chdb_result) uint64
66+
chdbResultStorageRowsRead func(result *chdb_result) uint64
67+
chdbResultStorageBytesRead func(result *chdb_result) uint64
68+
chdbResultError func(result *chdb_result) string
5069
)
5170

5271
func init() {
@@ -69,4 +88,21 @@ func init() {
6988
purego.RegisterLibFunc(&streamingResultCancel, libchdb, "chdb_streaming_cancel_query")
7089
purego.RegisterLibFunc(&streamingResultDestroy, libchdb, "chdb_destroy_result")
7190

91+
// new API
92+
purego.RegisterLibFunc(&chdbConnect, libchdb, "chdb_connect")
93+
purego.RegisterLibFunc(&chdbCloseConn, libchdb, "chdb_close_conn")
94+
purego.RegisterLibFunc(&chdbQuery, libchdb, "chdb_query")
95+
purego.RegisterLibFunc(&chdbStreamQuery, libchdb, "chdb_stream_query")
96+
purego.RegisterLibFunc(&chdbStreamFetchResult, libchdb, "chdb_stream_fetch_result")
97+
purego.RegisterLibFunc(&chdbStreamCancelQuery, libchdb, "chdb_stream_cancel_query")
98+
purego.RegisterLibFunc(&chdbDestroyQueryResult, libchdb, "chdb_destroy_query_result")
99+
purego.RegisterLibFunc(&chdbResultBuffer, libchdb, "chdb_result_buffer")
100+
purego.RegisterLibFunc(&chdbResultLen, libchdb, "chdb_result_length")
101+
purego.RegisterLibFunc(&chdbResultElapsed, libchdb, "chdb_result_elapsed")
102+
purego.RegisterLibFunc(&chdbResultRowsRead, libchdb, "chdb_result_rows_read")
103+
purego.RegisterLibFunc(&chdbResultBytesRead, libchdb, "chdb_result_bytes_read")
104+
purego.RegisterLibFunc(&chdbResultStorageRowsRead, libchdb, "chdb_result_storage_rows_read")
105+
purego.RegisterLibFunc(&chdbResultStorageBytesRead, libchdb, "chdb_result_storage_bytes_read")
106+
purego.RegisterLibFunc(&chdbResultError, libchdb, "chdb_result_error")
107+
72108
}

chdb-purego/chdb.go

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
)
1313

1414
type result struct {
15-
localResv2 *local_result_v2
15+
chdb_result *chdb_result
1616
}
1717

18-
func newChdbResult(cRes *local_result_v2) ChdbResult {
18+
func newChdbResult(cRes *chdb_result) ChdbResult {
1919
res := &result{
20-
localResv2: cRes,
20+
chdb_result: cRes,
2121
}
2222
// runtime.SetFinalizer(res, res.Free)
2323
return res
@@ -26,61 +26,66 @@ func newChdbResult(cRes *local_result_v2) ChdbResult {
2626

2727
// Buf implements ChdbResult.
2828
func (c *result) Buf() []byte {
29-
if c.localResv2 != nil {
30-
if c.localResv2.buf != nil && c.localResv2.len > 0 {
31-
return unsafe.Slice(c.localResv2.buf, c.localResv2.len)
29+
if c.chdb_result != nil {
30+
buf := chdbResultBuffer(c.chdb_result)
31+
if buf != nil {
32+
// Assuming we have a way to get the length of the buffer
33+
// Thlis is a placeholder; replace with actual length retrieva logic
34+
length := c.Len() // Replace with actual length
35+
return unsafe.Slice(buf, length)
3236
}
37+
3338
}
3439
return nil
3540
}
3641

3742
// BytesRead implements ChdbResult.
3843
func (c *result) BytesRead() uint64 {
39-
if c.localResv2 != nil {
40-
return c.localResv2.bytes_read
44+
if c.chdb_result != nil {
45+
return chdbResultBytesRead(c.chdb_result)
4146
}
4247
return 0
4348
}
4449

4550
// Elapsed implements ChdbResult.
4651
func (c *result) Elapsed() float64 {
47-
if c.localResv2 != nil {
48-
return c.localResv2.elapsed
52+
if c.chdb_result != nil {
53+
return chdbResultElapsed(c.chdb_result)
4954
}
5055
return 0
5156
}
5257

5358
// Error implements ChdbResult.
5459
func (c *result) Error() error {
55-
if c.localResv2 != nil {
56-
if c.localResv2.error_message != nil {
57-
return errors.New(ptrToGoString(c.localResv2.error_message))
60+
if c.chdb_result != nil {
61+
if s := chdbResultError(c.chdb_result); s != "" {
62+
return errors.New("test")
5863
}
5964
}
6065
return nil
6166
}
6267

6368
// Free implements ChdbResult.
6469
func (c *result) Free() {
65-
if c.localResv2 != nil {
66-
freeResultV2(c.localResv2)
67-
c.localResv2 = nil
70+
if c.chdb_result != nil {
71+
chdbDestroyQueryResult(c.chdb_result)
72+
c.chdb_result = nil
6873
}
6974

7075
}
7176

7277
// Len implements ChdbResult.
7378
func (c *result) Len() int {
74-
if c.localResv2 != nil {
75-
return int(c.localResv2.len)
79+
if c.chdb_result != nil {
80+
return int(chdbResultLen(c.chdb_result))
7681
}
7782
return 0
7883
}
7984

8085
// RowsRead implements ChdbResult.
8186
func (c *result) RowsRead() uint64 {
82-
if c.localResv2 != nil {
83-
return c.localResv2.rows_read
87+
if c.chdb_result != nil {
88+
return chdbResultRowsRead(c.chdb_result)
8489
}
8590
return 0
8691
}
@@ -95,10 +100,10 @@ func (c *result) String() string {
95100
}
96101

97102
type connection struct {
98-
conn **chdb_conn
103+
conn *chdb_connection
99104
}
100105

101-
func newChdbConn(conn **chdb_conn) ChdbConn {
106+
func newChdbConn(conn *chdb_connection) ChdbConn {
102107
c := &connection{
103108
conn: conn,
104109
}
@@ -109,28 +114,26 @@ func newChdbConn(conn **chdb_conn) ChdbConn {
109114
// Close implements ChdbConn.
110115
func (c *connection) Close() {
111116
if c.conn != nil {
112-
closeConn(c.conn)
117+
chdbCloseConn(c.conn.internal_data)
113118
}
114119
}
115120

116121
// Query implements ChdbConn.
117122
func (c *connection) Query(queryStr string, formatStr string) (result ChdbResult, err error) {
118-
119123
if c.conn == nil {
120124
return nil, fmt.Errorf("invalid connection")
121125
}
122126

123-
rawConn := *c.conn
124-
125-
res := queryConn(rawConn, queryStr, formatStr)
127+
res := chdbQuery(c.conn.internal_data, queryStr, formatStr)
126128
if res == nil {
127129
// According to the C ABI of chDB v1.2.0, the C function query_stable_v2
128130
// returns nil if the query returns no data. This is not an error. We
129131
// will change this behavior in the future.
130132
return newChdbResult(res), nil
131133
}
132-
if res.error_message != nil {
133-
return nil, errors.New(ptrToGoString(res.error_message))
134+
errMsg := chdbResultError(res)
135+
if errMsg != "" {
136+
return nil, errors.New("test")
134137
}
135138

136139
return newChdbResult(res), nil
@@ -143,28 +146,23 @@ func (c *connection) QueryStreaming(queryStr string, formatStr string) (result C
143146
return nil, fmt.Errorf("invalid connection")
144147
}
145148

146-
rawConn := *c.conn
147-
148-
res := queryConnStreaming(rawConn, queryStr, formatStr)
149+
res := chdbStreamQuery(c.conn.internal_data, queryStr, formatStr)
149150
if res == nil {
150151
// According to the C ABI of chDB v1.2.0, the C function query_stable_v2
151152
// returns nil if the query returns no data. This is not an error. We
152153
// will change this behavior in the future.
153-
return newStreamingResult(rawConn, res), nil
154+
return newStreamingResult(c.conn, res), nil
154155
}
155-
if s := streamingResultError(res); s != nil {
156-
return nil, errors.New(*s)
156+
if s := chdbResultError(res); s != "" {
157+
return nil, errors.New(s)
157158
}
158159

159-
return newStreamingResult(rawConn, res), nil
160+
return newStreamingResult(c.conn, res), nil
160161
}
161162

162163
func (c *connection) Ready() bool {
163164
if c.conn != nil {
164-
deref := *c.conn
165-
if deref != nil {
166-
return deref.connected
167-
}
165+
return true
168166
}
169167
return false
170168
}
@@ -216,15 +214,15 @@ func NewConnection(argc int, argv []string) (ChdbConn, error) {
216214
// fmt.Println("arg: ", arg)
217215
// }
218216

219-
var conn **chdb_conn
217+
var conn *chdb_connection
220218
var err error
221219
func() {
222220
defer func() {
223221
if r := recover(); r != nil {
224222
err = fmt.Errorf("C++ exception: %v", r)
225223
}
226224
}()
227-
conn = connectChdb(len(new_argv), c_argv)
225+
conn = chdbConnect(len(new_argv), c_argv)
228226
}()
229227

230228
if err != nil {

chdb-purego/streaming.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package chdbpurego
33
import "errors"
44

55
type streamingResult struct {
6-
curConn *chdb_conn
7-
stream *chdb_streaming_result
6+
curConn *chdb_connection
7+
stream *chdb_result
88
curChunk ChdbResult
99
}
1010

11-
func newStreamingResult(conn *chdb_conn, cRes *chdb_streaming_result) ChdbStreamResult {
11+
func newStreamingResult(conn *chdb_connection, cRes *chdb_result) ChdbStreamResult {
1212

1313
// nextChunk := streamingResultNext(conn, cRes)
1414
// if nextChunk == nil {
@@ -28,16 +28,19 @@ func newStreamingResult(conn *chdb_conn, cRes *chdb_streaming_result) ChdbStream
2828

2929
// Error implements ChdbStreamResult.
3030
func (c *streamingResult) Error() error {
31-
if s := streamingResultError(c.stream); s != nil {
32-
return errors.New(*s)
31+
if s := chdbResultError(c.stream); s != "" {
32+
return errors.New("test")
3333
}
3434
return nil
3535
}
3636

3737
// Free implements ChdbStreamResult.
3838
func (c *streamingResult) Free() {
39-
streamingResultCancel(c.curConn, c.stream)
40-
streamingResultDestroy(c.stream)
39+
if c.curConn != nil && c.stream != nil {
40+
//chdbStreamCancelQuery(c.curConn.internal_data, c.stream)
41+
chdbDestroyQueryResult(c.stream)
42+
}
43+
4144
c.stream = nil
4245
if c.curChunk != nil {
4346
c.curChunk.Free()
@@ -53,7 +56,7 @@ func (c *streamingResult) Cancel() {
5356
// GetNext implements ChdbStreamResult.
5457
func (c *streamingResult) GetNext() ChdbResult {
5558
if c.curChunk == nil {
56-
nextChunk := streamingResultNext(c.curConn, c.stream)
59+
nextChunk := chdbStreamFetchResult(c.curConn.internal_data, c.stream)
5760
if nextChunk == nil {
5861
return nil
5962
}
@@ -63,7 +66,7 @@ func (c *streamingResult) GetNext() ChdbResult {
6366
// free the current chunk before getting the next one
6467
c.curChunk.Free()
6568
c.curChunk = nil
66-
nextChunk := streamingResultNext(c.curConn, c.stream)
69+
nextChunk := chdbStreamFetchResult(c.curConn.internal_data, c.stream)
6770
if nextChunk == nil {
6871
return nil
6972
}

chdb-purego/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ type chdb_conn struct {
3636
queue unsafe.Pointer
3737
}
3838

39+
type chdb_connection struct {
40+
internal_data unsafe.Pointer
41+
}
42+
43+
type chdb_result struct {
44+
internal_data unsafe.Pointer
45+
}
46+
3947
type ChdbResult interface {
4048
Buf() []byte
4149
// String rapresentation of the the buffer

0 commit comments

Comments
 (0)