|
| 1 | +package chdbpurego |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "runtime" |
| 7 | + "unsafe" |
| 8 | +) |
| 9 | + |
| 10 | +type result struct { |
| 11 | + localResv2 *local_result_v2 |
| 12 | +} |
| 13 | + |
| 14 | +func newChdbResult(cRes *local_result_v2) ChdbResult { |
| 15 | + res := &result{ |
| 16 | + localResv2: cRes, |
| 17 | + } |
| 18 | + return res |
| 19 | + |
| 20 | +} |
| 21 | + |
| 22 | +// Buf implements ChdbResult. |
| 23 | +func (c *result) Buf() []byte { |
| 24 | + if c.localResv2 != nil { |
| 25 | + if c.localResv2.buf != nil && c.localResv2.len > 0 { |
| 26 | + return unsafe.Slice(c.localResv2.buf, c.localResv2.len) |
| 27 | + } |
| 28 | + } |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +// BytesRead implements ChdbResult. |
| 33 | +func (c *result) BytesRead() uint64 { |
| 34 | + if c.localResv2 != nil { |
| 35 | + return c.localResv2.bytes_read |
| 36 | + } |
| 37 | + return 0 |
| 38 | +} |
| 39 | + |
| 40 | +// Elapsed implements ChdbResult. |
| 41 | +func (c *result) Elapsed() float64 { |
| 42 | + if c.localResv2 != nil { |
| 43 | + return c.localResv2.elapsed |
| 44 | + } |
| 45 | + return 0 |
| 46 | +} |
| 47 | + |
| 48 | +// Error implements ChdbResult. |
| 49 | +func (c *result) Error() error { |
| 50 | + if c.localResv2 != nil { |
| 51 | + if c.localResv2.error_message != nil { |
| 52 | + return errors.New(ptrToGoString(c.localResv2.error_message)) |
| 53 | + } |
| 54 | + } |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// Free implements ChdbResult. |
| 59 | +func (c *result) Free() error { |
| 60 | + if c.localResv2 != nil { |
| 61 | + freeResultV2(c.localResv2) |
| 62 | + c.localResv2 = nil |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// Len implements ChdbResult. |
| 68 | +func (c *result) Len() int { |
| 69 | + if c.localResv2 != nil { |
| 70 | + return int(c.localResv2.len) |
| 71 | + } |
| 72 | + return 0 |
| 73 | +} |
| 74 | + |
| 75 | +// RowsRead implements ChdbResult. |
| 76 | +func (c *result) RowsRead() uint64 { |
| 77 | + if c.localResv2 != nil { |
| 78 | + return c.localResv2.rows_read |
| 79 | + } |
| 80 | + return 0 |
| 81 | +} |
| 82 | + |
| 83 | +// String implements ChdbResult. |
| 84 | +func (c *result) String() string { |
| 85 | + ret := c.Buf() |
| 86 | + if ret == nil { |
| 87 | + return "" |
| 88 | + } |
| 89 | + return string(ret) |
| 90 | +} |
| 91 | + |
| 92 | +type connection struct { |
| 93 | + conn **chdb_conn |
| 94 | + pinner runtime.Pinner |
| 95 | +} |
| 96 | + |
| 97 | +func NewChdbConn(conn **chdb_conn) ChdbConn { |
| 98 | + return &connection{ |
| 99 | + conn: conn, |
| 100 | + pinner: runtime.Pinner{}, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Close implements ChdbConn. |
| 105 | +func (c *connection) Close() { |
| 106 | + if c.conn != nil { |
| 107 | + closeConn(c.conn) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Query implements ChdbConn. |
| 112 | +func (c *connection) Query(queryStr string, formatStr string) (result ChdbResult, err error) { |
| 113 | + |
| 114 | + if c.conn == nil { |
| 115 | + return nil, fmt.Errorf("invalid connection") |
| 116 | + } |
| 117 | + defer c.pinner.Unpin() |
| 118 | + // qPtr := stringToPtr(queryStr, &c.pinner) |
| 119 | + // fPtr := stringToPtr(formatStr, &c.pinner) |
| 120 | + deref := *c.conn |
| 121 | + // fmt.Printf("queryPtr: %p, formatPtr: %p, conn: %p\n", qPtr, fPtr, deref) |
| 122 | + // fmt.Printf("query string: %s\n", queryStr) |
| 123 | + // fmt.Printf("format string: %s\n", formatStr) |
| 124 | + res := queryConnV2(deref, queryStr, formatStr) |
| 125 | + if res == nil { |
| 126 | + // According to the C ABI of chDB v1.2.0, the C function query_stable_v2 |
| 127 | + // returns nil if the query returns no data. This is not an error. We |
| 128 | + // will change this behavior in the future. |
| 129 | + return newChdbResult(res), nil |
| 130 | + } |
| 131 | + if res.error_message != nil { |
| 132 | + return nil, errors.New(ptrToGoString(res.error_message)) |
| 133 | + } |
| 134 | + |
| 135 | + return newChdbResult(res), nil |
| 136 | +} |
| 137 | + |
| 138 | +// Ready implements ChdbConn. |
| 139 | +func (c *connection) Ready() bool { |
| 140 | + if c.conn != nil { |
| 141 | + deref := *c.conn |
| 142 | + if deref != nil { |
| 143 | + return deref.connected |
| 144 | + } |
| 145 | + } |
| 146 | + return false |
| 147 | +} |
| 148 | + |
| 149 | +func RawQuery(argc int, argv []string) (result ChdbResult, err error) { |
| 150 | + pinner := runtime.Pinner{} |
| 151 | + defer pinner.Unpin() |
| 152 | + |
| 153 | + cArgv := make([]*byte, len(argv)) |
| 154 | + for idx, arg := range argv { |
| 155 | + cArgv[idx] = (*byte)(unsafe.Pointer(&([]byte(arg + "\x00")[0]))) |
| 156 | + |
| 157 | + } |
| 158 | + cArgvPtr := (**byte)(unsafe.Pointer(&cArgv[0])) |
| 159 | + pinner.Pin(cArgvPtr) |
| 160 | + |
| 161 | + res := queryStableV2(argc, cArgvPtr) |
| 162 | + if res == nil { |
| 163 | + // According to the C ABI of chDB v1.2.0, the C function query_stable_v2 |
| 164 | + // returns nil if the query returns no data. This is not an error. We |
| 165 | + // will change this behavior in the future. |
| 166 | + return newChdbResult(res), nil |
| 167 | + } |
| 168 | + if res.error_message != nil { |
| 169 | + return nil, errors.New(ptrToGoString(res.error_message)) |
| 170 | + } |
| 171 | + |
| 172 | + return newChdbResult(res), nil |
| 173 | +} |
| 174 | + |
| 175 | +func NewConnection(argc int, argv []string) (ChdbConn, error) { |
| 176 | + pinner := runtime.Pinner{} |
| 177 | + defer pinner.Unpin() |
| 178 | + |
| 179 | + cArgv := make([]*byte, len(argv)) |
| 180 | + for idx, arg := range argv { |
| 181 | + cArgv[idx] = (*byte)(unsafe.Pointer(&([]byte(arg + "\x00")[0]))) |
| 182 | + |
| 183 | + } |
| 184 | + cArgvPtr := (**byte)(unsafe.Pointer(&cArgv[0])) |
| 185 | + pinner.Pin(cArgvPtr) |
| 186 | + conn := connectChdb(argc, cArgvPtr) |
| 187 | + if conn == nil { |
| 188 | + return nil, fmt.Errorf("could not create a chdb connection") |
| 189 | + } |
| 190 | + return NewChdbConn(conn), nil |
| 191 | +} |
0 commit comments