Skip to content

Commit 8a983de

Browse files
committed
update documentation
1 parent 342e4b5 commit 8a983de

File tree

9 files changed

+240
-215
lines changed

9 files changed

+240
-215
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func main() {
6767
fmt.Println(result)
6868

6969
tmp_path := filepath.Join(os.TempDir(), "chdb_test")
70-
defer os.RemoveAll(tmp_path)
7170
// Stateful Query (persistent)
7271
session, _ := chdb.NewSession(tmp_path)
72+
// session cleanup will also delete the folder
7373
defer session.Cleanup()
7474

7575
_, err = session.Query("CREATE DATABASE IF NOT EXISTS testdb; " +

chdb-purego/chdb.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type connection struct {
9393
conn **chdb_conn
9494
}
9595

96-
func NewChdbConn(conn **chdb_conn) ChdbConn {
96+
func newChdbConn(conn **chdb_conn) ChdbConn {
9797
c := &connection{
9898
conn: conn,
9999
}
@@ -131,7 +131,6 @@ func (c *connection) Query(queryStr string, formatStr string) (result ChdbResult
131131
return newChdbResult(res), nil
132132
}
133133

134-
// Ready implements ChdbConn.
135134
func (c *connection) Ready() bool {
136135
if c.conn != nil {
137136
deref := *c.conn
@@ -142,6 +141,7 @@ func (c *connection) Ready() bool {
142141
return false
143142
}
144143

144+
// RawQuery will execute the given clickouse query without using any session.
145145
func RawQuery(argc int, argv []string) (result ChdbResult, err error) {
146146
res := queryStableV2(argc, argv)
147147
if res == nil {
@@ -157,10 +157,38 @@ func RawQuery(argc int, argv []string) (result ChdbResult, err error) {
157157
return newChdbResult(res), nil
158158
}
159159

160+
// Session will keep the state of query.
161+
// If path is None, it will create a temporary directory and use it as the database path
162+
// and the temporary directory will be removed when the session is closed.
163+
// You can also pass in a path to create a database at that path where will keep your data.
164+
//
165+
// You can also use a connection string to pass in the path and other parameters.
166+
// Examples:
167+
// - ":memory:" (for in-memory database)
168+
// - "test.db" (for relative path)
169+
// - "file:test.db" (same as above)
170+
// - "/path/to/test.db" (for absolute path)
171+
// - "file:/path/to/test.db" (same as above)
172+
// - "file:test.db?param1=value1&param2=value2" (for relative path with query params)
173+
// - "file::memory:?verbose&log-level=test" (for in-memory database with query params)
174+
// - "///path/to/test.db?param1=value1&param2=value2" (for absolute path)
175+
//
176+
// Connection string args handling:
177+
//
178+
// Connection string can contain query params like "file:test.db?param1=value1&param2=value2"
179+
// "param1=value1" will be passed to ClickHouse engine as start up args.
180+
//
181+
// For more details, see `clickhouse local --help --verbose`
182+
// Some special args handling:
183+
// - "mode=ro" would be "--readonly=1" for clickhouse (read-only mode)
184+
//
185+
// Important:
186+
// - There can be only one session at a time. If you want to create a new session, you need to close the existing one.
187+
// - Creating a new session will close the existing one.
160188
func NewConnection(argc int, argv []string) (ChdbConn, error) {
161189
conn := connectChdb(argc, argv)
162190
if conn == nil {
163191
return nil, fmt.Errorf("could not create a chdb connection")
164192
}
165-
return NewChdbConn(conn), nil
193+
return newChdbConn(conn), nil
166194
}

chdb-purego/chdb.h

Lines changed: 0 additions & 123 deletions
This file was deleted.

chdb-purego/helpers.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package chdbpurego
22

33
import (
4-
"runtime"
54
"unsafe"
65
)
76

@@ -20,21 +19,3 @@ func ptrToGoString(ptr *byte) string {
2019

2120
return string(unsafe.Slice(ptr, length))
2221
}
23-
24-
func stringToPtr(s string, pinner *runtime.Pinner) *byte {
25-
// Pinne for convert string to bytes
26-
// maybe there is simpler solution but it was late when I write this code.
27-
data := make([]byte, len(s)+1)
28-
copy(data, s)
29-
data[len(s)] = 0 // Null-terminator
30-
31-
ptr := &data[0]
32-
pinner.Pin(ptr)
33-
34-
return (*byte)(unsafe.Pointer(ptr))
35-
}
36-
37-
func strToBytePtr(s string) *byte {
38-
b := append([]byte(s), 0) // Convert to []byte and append null terminator
39-
return &b[0] // Get pointer to first byte
40-
}

chdb-purego/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,29 @@ type chdb_conn struct {
3232
}
3333

3434
type ChdbResult interface {
35+
// Raw bytes result buffer, used for reading the result of clickhouse query
3536
Buf() []byte
37+
// String rapresentation of the the buffer
3638
String() string
39+
// Lenght in bytes of the buffer
3740
Len() int
41+
// Number of seconds elapsed for the query execution
3842
Elapsed() float64
43+
// Amount of rows returned by the query
3944
RowsRead() uint64
45+
// Amount of bytes returned by the query
4046
BytesRead() uint64
47+
// If the query had any error during execution, here you can retrieve the cause.
4148
Error() error
49+
// Free the query result and all the allocated memory
4250
Free() error
4351
}
4452

4553
type ChdbConn interface {
54+
//Query executes the given queryStr in the underlying clickhouse connection, and output the result in the given formatStr
4655
Query(queryStr string, formatStr string) (result ChdbResult, err error)
56+
//Ready returns a boolean indicating if the connections is successfully established.
4757
Ready() bool
58+
//Close the connection and free the underlying allocated memory
4859
Close()
4960
}

chdb.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@ import "github.com/chdb-io/chdb-go/chdb"
88

99
## Index
1010

11-
- [func Query\(queryStr string, outputFormats ...string\) \*chdbstable.LocalResult](<#Query>)
11+
- [func Query\(queryStr string, outputFormats ...string\) \(result chdbpurego.ChdbResult, err error\)](<#Query>)
1212
- [type Session](<#Session>)
1313
- [func NewSession\(paths ...string\) \(\*Session, error\)](<#NewSession>)
1414
- [func \(s \*Session\) Cleanup\(\)](<#Session.Cleanup>)
1515
- [func \(s \*Session\) Close\(\)](<#Session.Close>)
16+
- [func \(s \*Session\) ConnStr\(\) string](<#Session.ConnStr>)
1617
- [func \(s \*Session\) IsTemp\(\) bool](<#Session.IsTemp>)
1718
- [func \(s \*Session\) Path\(\) string](<#Session.Path>)
18-
- [func \(s \*Session\) Query\(queryStr string, outputFormats ...string\) \*chdbstable.LocalResult](<#Session.Query>)
19+
- [func \(s \*Session\) Query\(queryStr string, outputFormats ...string\) \(result chdbpurego.ChdbResult, err error\)](<#Session.Query>)
1920

2021

2122
<a name="Query"></a>
22-
## func [Query](<https://github.com/chdb-io/chdb-go/blob/main/chdb/wrapper.go#L8>)
23+
## func [Query](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/wrapper.go#L8>)
2324

2425
```go
25-
func Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult
26+
func Query(queryStr string, outputFormats ...string) (result chdbpurego.ChdbResult, err error)
2627
```
2728

28-
Query calls queryToBuffer with a default output format of "CSV" if not provided.
29+
Query calls query\_conn with a default in\-memory session and default output format of "CSV" if not provided.
2930

3031
<a name="Session"></a>
31-
## type [Session](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L11-L14>)
32+
## type [Session](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L15-L20>)
3233

3334

3435

@@ -39,7 +40,7 @@ type Session struct {
3940
```
4041

4142
<a name="NewSession"></a>
42-
### func [NewSession](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L19>)
43+
### func [NewSession](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L25>)
4344

4445
```go
4546
func NewSession(paths ...string) (*Session, error)
@@ -48,7 +49,7 @@ func NewSession(paths ...string) (*Session, error)
4849
NewSession creates a new session with the given path. If path is empty, a temporary directory is created. Note: The temporary directory is removed when Close is called.
4950

5051
<a name="Session.Cleanup"></a>
51-
### func \(\*Session\) [Cleanup](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L57>)
52+
### func \(\*Session\) [Cleanup](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L77>)
5253

5354
```go
5455
func (s *Session) Cleanup()
@@ -57,7 +58,7 @@ func (s *Session) Cleanup()
5758
Cleanup closes the session and removes the directory.
5859

5960
<a name="Session.Close"></a>
60-
### func \(\*Session\) [Close](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L49>)
61+
### func \(\*Session\) [Close](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L67>)
6162

6263
```go
6364
func (s *Session) Close()
@@ -69,8 +70,17 @@ Close closes the session and removes the temporary directory
6970
temporary directory is created when NewSession was called with an empty path.
7071
```
7172

73+
<a name="Session.ConnStr"></a>
74+
### func \(\*Session\) [ConnStr](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L88>)
75+
76+
```go
77+
func (s *Session) ConnStr() string
78+
```
79+
80+
ConnStr returns the current connection string used for the underlying connection
81+
7282
<a name="Session.IsTemp"></a>
73-
### func \(\*Session\) [IsTemp](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L68>)
83+
### func \(\*Session\) [IsTemp](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L93>)
7484

7585
```go
7686
func (s *Session) IsTemp() bool
@@ -79,7 +89,7 @@ func (s *Session) IsTemp() bool
7989
IsTemp returns whether the session is temporary.
8090

8191
<a name="Session.Path"></a>
82-
### func \(\*Session\) [Path](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L63>)
92+
### func \(\*Session\) [Path](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L83>)
8393

8494
```go
8595
func (s *Session) Path() string
@@ -88,12 +98,12 @@ func (s *Session) Path() string
8898
Path returns the path of the session.
8999

90100
<a name="Session.Query"></a>
91-
### func \(\*Session\) [Query](<https://github.com/chdb-io/chdb-go/blob/main/chdb/session.go#L39>)
101+
### func \(\*Session\) [Query](<https://github.com/agoncear-mwb/chdb-go/blob/main/chdb/session.go#L56>)
92102

93103
```go
94-
func (s *Session) Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult
104+
func (s *Session) Query(queryStr string, outputFormats ...string) (result chdbpurego.ChdbResult, err error)
95105
```
96106

97-
Query calls queryToBuffer with a default output format of "CSV" if not provided.
107+
Query calls \`query\_conn\` function with the current connection and a default output format of "CSV" if not provided.
98108

99109
Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)

0 commit comments

Comments
 (0)