Skip to content

Commit 173e057

Browse files
authored
Merge pull request #32 from s0und0fs1lence/main
2 parents 08f247d + 8872c02 commit 173e057

File tree

9 files changed

+291
-87
lines changed

9 files changed

+291
-87
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 *chdb_connection)
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 *chdb_connection, 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 & 47 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(s)
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,15 +100,10 @@ func (c *result) String() string {
95100
}
96101

97102
type connection struct {
98-
conn **chdb_conn
99-
}
100-
101-
// CancelQuery implements ChdbConn.
102-
func (c *connection) CancelQuery(query ChdbResult) (err error) {
103-
panic("unimplemented")
103+
conn *chdb_connection
104104
}
105105

106-
func newChdbConn(conn **chdb_conn) ChdbConn {
106+
func newChdbConn(conn *chdb_connection) ChdbConn {
107107
c := &connection{
108108
conn: conn,
109109
}
@@ -114,28 +114,26 @@ func newChdbConn(conn **chdb_conn) ChdbConn {
114114
// Close implements ChdbConn.
115115
func (c *connection) Close() {
116116
if c.conn != nil {
117-
closeConn(c.conn)
117+
chdbCloseConn(c.conn)
118118
}
119119
}
120120

121121
// Query implements ChdbConn.
122122
func (c *connection) Query(queryStr string, formatStr string) (result ChdbResult, err error) {
123-
124123
if c.conn == nil {
125124
return nil, fmt.Errorf("invalid connection")
126125
}
127126

128-
rawConn := *c.conn
129-
130-
res := queryConn(rawConn, queryStr, formatStr)
127+
res := chdbQuery(c.conn.internal_data, queryStr, formatStr)
131128
if res == nil {
132129
// According to the C ABI of chDB v1.2.0, the C function query_stable_v2
133130
// returns nil if the query returns no data. This is not an error. We
134131
// will change this behavior in the future.
135132
return newChdbResult(res), nil
136133
}
137-
if res.error_message != nil {
138-
return nil, errors.New(ptrToGoString(res.error_message))
134+
errMsg := chdbResultError(res)
135+
if errMsg != "" {
136+
return nil, errors.New(errMsg)
139137
}
140138

141139
return newChdbResult(res), nil
@@ -148,28 +146,23 @@ func (c *connection) QueryStreaming(queryStr string, formatStr string) (result C
148146
return nil, fmt.Errorf("invalid connection")
149147
}
150148

151-
rawConn := *c.conn
152-
153-
res := queryConnStreaming(rawConn, queryStr, formatStr)
149+
res := chdbStreamQuery(c.conn.internal_data, queryStr, formatStr)
154150
if res == nil {
155151
// According to the C ABI of chDB v1.2.0, the C function query_stable_v2
156152
// returns nil if the query returns no data. This is not an error. We
157153
// will change this behavior in the future.
158-
return newStreamingResult(rawConn, res), nil
154+
return newStreamingResult(c.conn, res), nil
159155
}
160-
if s := streamingResultError(res); s != nil {
161-
return nil, errors.New(*s)
156+
if s := chdbResultError(res); s != "" {
157+
return nil, errors.New(s)
162158
}
163159

164-
return newStreamingResult(rawConn, res), nil
160+
return newStreamingResult(c.conn, res), nil
165161
}
166162

167163
func (c *connection) Ready() bool {
168164
if c.conn != nil {
169-
deref := *c.conn
170-
if deref != nil {
171-
return deref.connected
172-
}
165+
return true
173166
}
174167
return false
175168
}
@@ -221,15 +214,15 @@ func NewConnection(argc int, argv []string) (ChdbConn, error) {
221214
// fmt.Println("arg: ", arg)
222215
// }
223216

224-
var conn **chdb_conn
217+
var conn *chdb_connection
225218
var err error
226219
func() {
227220
defer func() {
228221
if r := recover(); r != nil {
229222
err = fmt.Errorf("C++ exception: %v", r)
230223
}
231224
}()
232-
conn = connectChdb(len(new_argv), c_argv)
225+
conn = chdbConnect(len(new_argv), c_argv)
233226
}()
234227

235228
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(s)
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, 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)