|
| 1 | +package chdbpurego |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "unsafe" |
| 7 | +) |
| 8 | + |
| 9 | +type result struct { |
| 10 | + localResv2 *local_result_v2 |
| 11 | +} |
| 12 | + |
| 13 | +func newChdbResult(cRes *local_result_v2) ChdbResult { |
| 14 | + res := &result{ |
| 15 | + localResv2: cRes, |
| 16 | + } |
| 17 | + // runtime.SetFinalizer(res, res.Free) |
| 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() { |
| 60 | + if c.localResv2 != nil { |
| 61 | + freeResultV2(c.localResv2) |
| 62 | + c.localResv2 = nil |
| 63 | + } |
| 64 | + |
| 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 | +} |
| 95 | + |
| 96 | +func newChdbConn(conn **chdb_conn) ChdbConn { |
| 97 | + c := &connection{ |
| 98 | + conn: conn, |
| 99 | + } |
| 100 | + // runtime.SetFinalizer(c, c.Close) |
| 101 | + return c |
| 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 | + |
| 118 | + rawConn := *c.conn |
| 119 | + |
| 120 | + res := queryConn(rawConn, queryStr, formatStr) |
| 121 | + if res == nil { |
| 122 | + // According to the C ABI of chDB v1.2.0, the C function query_stable_v2 |
| 123 | + // returns nil if the query returns no data. This is not an error. We |
| 124 | + // will change this behavior in the future. |
| 125 | + return newChdbResult(res), nil |
| 126 | + } |
| 127 | + if res.error_message != nil { |
| 128 | + return nil, errors.New(ptrToGoString(res.error_message)) |
| 129 | + } |
| 130 | + |
| 131 | + return newChdbResult(res), nil |
| 132 | +} |
| 133 | + |
| 134 | +func (c *connection) Ready() bool { |
| 135 | + if c.conn != nil { |
| 136 | + deref := *c.conn |
| 137 | + if deref != nil { |
| 138 | + return deref.connected |
| 139 | + } |
| 140 | + } |
| 141 | + return false |
| 142 | +} |
| 143 | + |
| 144 | +// Session will keep the state of query. |
| 145 | +// If path is None, it will create a temporary directory and use it as the database path |
| 146 | +// and the temporary directory will be removed when the session is closed. |
| 147 | +// You can also pass in a path to create a database at that path where will keep your data. |
| 148 | +// |
| 149 | +// You can also use a connection string to pass in the path and other parameters. |
| 150 | +// Examples: |
| 151 | +// - ":memory:" (for in-memory database) |
| 152 | +// - "test.db" (for relative path) |
| 153 | +// - "file:test.db" (same as above) |
| 154 | +// - "/path/to/test.db" (for absolute path) |
| 155 | +// - "file:/path/to/test.db" (same as above) |
| 156 | +// - "file:test.db?param1=value1¶m2=value2" (for relative path with query params) |
| 157 | +// - "file::memory:?verbose&log-level=test" (for in-memory database with query params) |
| 158 | +// - "///path/to/test.db?param1=value1¶m2=value2" (for absolute path) |
| 159 | +// |
| 160 | +// Connection string args handling: |
| 161 | +// |
| 162 | +// Connection string can contain query params like "file:test.db?param1=value1¶m2=value2" |
| 163 | +// "param1=value1" will be passed to ClickHouse engine as start up args. |
| 164 | +// |
| 165 | +// For more details, see `clickhouse local --help --verbose` |
| 166 | +// Some special args handling: |
| 167 | +// - "mode=ro" would be "--readonly=1" for clickhouse (read-only mode) |
| 168 | +// |
| 169 | +// Important: |
| 170 | +// - There can be only one session at a time. If you want to create a new session, you need to close the existing one. |
| 171 | +// - Creating a new session will close the existing one. |
| 172 | +func NewConnection(argc int, argv []string) (ChdbConn, error) { |
| 173 | + conn := connectChdb(argc, argv) |
| 174 | + if conn == nil { |
| 175 | + return nil, fmt.Errorf("could not create a chdb connection") |
| 176 | + } |
| 177 | + return newChdbConn(conn), nil |
| 178 | +} |
0 commit comments