Skip to content

Commit ae97363

Browse files
committed
Add OpenDB. #463
1 parent f521553 commit ae97363

File tree

3 files changed

+116
-8
lines changed

3 files changed

+116
-8
lines changed

clickhouse_options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (o *Options) fromDSN(in string) error {
9191
}
9292
}
9393
}
94-
if secure {
94+
if secure && o.TLS == nil {
9595
o.TLS = &tls.Config{
9696
InsecureSkipVerify: skipVerify,
9797
}

clickhouse_std.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,44 @@ func init() {
1616
sql.Register("clickhouse", &stdDriver{})
1717
}
1818

19+
func OpenDB(opt *Options) driver.Connector {
20+
opt.setDefaults()
21+
return &stdDriver{
22+
opt: opt,
23+
}
24+
}
25+
1926
type stdDriver struct {
27+
opt *Options
2028
conn *connect
2129
commit func() error
2230
connID int64
2331
}
2432

33+
func (d *stdDriver) Driver() driver.Driver {
34+
return d
35+
}
36+
37+
func (d *stdDriver) Connect(context.Context) (_ driver.Conn, err error) {
38+
return d.Open("")
39+
}
40+
2541
func (d *stdDriver) Open(dsn string) (_ driver.Conn, err error) {
2642
var (
27-
opt Options
2843
conn *connect
2944
connID = int(atomic.AddInt64(&d.connID, 1))
3045
)
31-
if err = opt.fromDSN(dsn); err != nil {
32-
return nil, err
46+
if d.opt == nil {
47+
d.opt = &Options{}
48+
if err = d.opt.fromDSN(dsn); err != nil {
49+
return nil, err
50+
}
3351
}
34-
for num := range opt.Addr {
35-
if opt.ConnOpenStrategy == ConnOpenRoundRobin {
36-
num = int(connID) % len(opt.Addr)
52+
for num := range d.opt.Addr {
53+
if d.opt.ConnOpenStrategy == ConnOpenRoundRobin {
54+
num = int(connID) % len(d.opt.Addr)
3755
}
38-
if conn, err = dial(opt.Addr[num], connID, &opt); err == nil {
56+
if conn, err = dial(d.opt.Addr[num], connID, d.opt); err == nil {
3957
return &stdDriver{
4058
conn: conn,
4159
}, nil

examples/std/open_db.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"log"
8+
"time"
9+
10+
"github.com/ClickHouse/clickhouse-go/v2"
11+
)
12+
13+
func example() error {
14+
conn := sql.OpenDB(clickhouse.OpenDB(&clickhouse.Options{
15+
Addr: []string{"127.0.0.1:9000"},
16+
Auth: clickhouse.Auth{
17+
Database: "default",
18+
Username: "default",
19+
Password: "",
20+
},
21+
}))
22+
conn.SetMaxIdleConns(5)
23+
conn.SetMaxOpenConns(10)
24+
conn.SetConnMaxLifetime(time.Hour)
25+
ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{
26+
"max_block_size": 10,
27+
}), clickhouse.WithProgress(func(p *clickhouse.Progress) {
28+
fmt.Println("progress: ", p)
29+
}))
30+
if err := conn.PingContext(ctx); err != nil {
31+
if exception, ok := err.(*clickhouse.Exception); ok {
32+
fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
33+
}
34+
return err
35+
}
36+
if _, err := conn.ExecContext(ctx, `DROP TABLE IF EXISTS example`); err != nil {
37+
return err
38+
}
39+
_, err := conn.ExecContext(ctx, `
40+
CREATE TABLE IF NOT EXISTS example (
41+
Col1 UInt8,
42+
Col2 String,
43+
Col3 DateTime
44+
) engine=Memory
45+
`)
46+
if err != nil {
47+
return err
48+
}
49+
scope, err := conn.Begin()
50+
if err != nil {
51+
return err
52+
}
53+
{
54+
batch, err := scope.PrepareContext(ctx, "INSERT INTO example (Col1, Col2, Col3)")
55+
if err != nil {
56+
return err
57+
}
58+
for i := 0; i < 10; i++ {
59+
if _, err := batch.Exec(uint8(i), fmt.Sprintf("value_%d", i), time.Now()); err != nil {
60+
return err
61+
}
62+
}
63+
}
64+
if err := scope.Commit(); err != nil {
65+
return err
66+
}
67+
rows, err := conn.QueryContext(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE Col1 >= $1 AND Col2 <> $2 AND Col3 <= $3", 0, "xxx", time.Now())
68+
if err != nil {
69+
return err
70+
}
71+
for rows.Next() {
72+
var (
73+
col1 uint8
74+
col2 string
75+
col3 time.Time
76+
)
77+
if err := rows.Scan(&col1, &col2, &col3); err != nil {
78+
return err
79+
}
80+
fmt.Printf("row: col1=%d, col2=%s, col3=%s\n", col1, col2, col3)
81+
}
82+
rows.Close()
83+
return rows.Err()
84+
}
85+
86+
func main() {
87+
if err := example(); err != nil {
88+
log.Fatal(err)
89+
}
90+
}

0 commit comments

Comments
 (0)