@@ -7,40 +7,61 @@ package chdbstable
77*/
88import "C"
99import (
10+ "errors"
1011 "runtime"
1112 "unsafe"
1213)
1314
14- // LocalResult mirrors the C struct local_result in Go.
15+ // ChdbError is returned when the C function returns an error.
16+ type ChdbError struct {
17+ msg string
18+ }
19+
20+ func (e * ChdbError ) Error () string {
21+ return e .msg
22+ }
23+
24+ // ErrNilResult is returned when the C function returns a nil pointer.
25+ var ErrNilResult = errors .New ("chDB C function returned nil pointer" )
26+
27+
28+ // LocalResult mirrors the C struct local_result_v2 in Go.
1529type LocalResult struct {
16- cResult * C.struct_local_result
30+ cResult * C.struct_local_result_v2
1731}
1832
1933// newLocalResult creates a new LocalResult and sets a finalizer to free C memory.
20- func newLocalResult (cResult * C.struct_local_result ) * LocalResult {
34+ func newLocalResult (cResult * C.struct_local_result_v2 ) * LocalResult {
2135 result := & LocalResult {cResult : cResult }
2236 runtime .SetFinalizer (result , freeLocalResult )
2337 return result
2438}
2539
2640// freeLocalResult is called by the garbage collector.
2741func freeLocalResult (result * LocalResult ) {
28- C .free_result (result .cResult )
42+ C .free_result_v2 (result .cResult )
2943}
3044
31- // QueryStable calls the C function query_stable .
32- func QueryStable (argc int , argv []string ) * LocalResult {
45+ // QueryStable calls the C function query_stable_v2 .
46+ func QueryStable (argc int , argv []string ) ( result * LocalResult , err error ) {
3347 cArgv := make ([]* C.char , len (argv ))
3448 for i , s := range argv {
3549 cArgv [i ] = C .CString (s )
3650 defer C .free (unsafe .Pointer (cArgv [i ]))
3751 }
3852
39- cResult := C .query_stable (C .int (argc ), & cArgv [0 ])
40- return newLocalResult (cResult )
53+ cResult := C .query_stable_v2 (C .int (argc ), & cArgv [0 ])
54+ if cResult == nil {
55+ // According to the C ABI of chDB, it is not possible to return a nil pointer.
56+ return nil , ErrNilResult
57+ }
58+ if cResult .error_message != nil {
59+ return nil , & ChdbError {msg : C .GoString (cResult .error_message )}
60+ }
61+ return newLocalResult (cResult ), nil
4162}
4263
43- // Accessor methods to access fields of the local_result struct.
64+ // Accessor methods to access fields of the local_result_v2 struct.
4465func (r * LocalResult ) Buf () []byte {
4566 if r .cResult == nil {
4667 return nil
@@ -87,3 +108,10 @@ func (r *LocalResult) BytesRead() uint64 {
87108 }
88109 return uint64 (r .cResult .bytes_read )
89110}
111+
112+ func (r * LocalResult ) Error () string {
113+ if r .cResult == nil {
114+ return ""
115+ }
116+ return C .GoString (r .cResult .err )
117+ }
0 commit comments