@@ -7,40 +7,61 @@ package chdbstable
7
7
*/
8
8
import "C"
9
9
import (
10
+ "errors"
10
11
"runtime"
11
12
"unsafe"
12
13
)
13
14
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.
15
29
type LocalResult struct {
16
- cResult * C.struct_local_result
30
+ cResult * C.struct_local_result_v2
17
31
}
18
32
19
33
// 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 {
21
35
result := & LocalResult {cResult : cResult }
22
36
runtime .SetFinalizer (result , freeLocalResult )
23
37
return result
24
38
}
25
39
26
40
// freeLocalResult is called by the garbage collector.
27
41
func freeLocalResult (result * LocalResult ) {
28
- C .free_result (result .cResult )
42
+ C .free_result_v2 (result .cResult )
29
43
}
30
44
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 ) {
33
47
cArgv := make ([]* C.char , len (argv ))
34
48
for i , s := range argv {
35
49
cArgv [i ] = C .CString (s )
36
50
defer C .free (unsafe .Pointer (cArgv [i ]))
37
51
}
38
52
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
41
62
}
42
63
43
- // Accessor methods to access fields of the local_result struct.
64
+ // Accessor methods to access fields of the local_result_v2 struct.
44
65
func (r * LocalResult ) Buf () []byte {
45
66
if r .cResult == nil {
46
67
return nil
@@ -87,3 +108,10 @@ func (r *LocalResult) BytesRead() uint64 {
87
108
}
88
109
return uint64 (r .cResult .bytes_read )
89
110
}
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