Skip to content

Commit 6975adb

Browse files
committed
OpenDB check settings #463
1 parent ae97363 commit 6975adb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

clickhouse_std.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"database/sql"
66
"database/sql/driver"
77
"errors"
8+
"fmt"
89
"io"
910
"reflect"
11+
"strings"
1012
"sync/atomic"
1113

1214
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
@@ -17,13 +19,29 @@ func init() {
1719
}
1820

1921
func OpenDB(opt *Options) driver.Connector {
22+
var settings []string
23+
if opt.MaxIdleConns > 0 {
24+
settings = append(settings, "SetMaxIdleConns")
25+
}
26+
if opt.MaxOpenConns > 0 {
27+
settings = append(settings, "SetMaxOpenConns")
28+
}
29+
if opt.ConnMaxLifetime > 0 {
30+
settings = append(settings, "SetConnMaxLifetime")
31+
}
32+
if len(settings) != 0 {
33+
return &stdDriver{
34+
err: fmt.Errorf("can not connect. invalid setting. use %s (see https://pkg.go.dev/database/sql)", strings.Join(settings, ",")),
35+
}
36+
}
2037
opt.setDefaults()
2138
return &stdDriver{
2239
opt: opt,
2340
}
2441
}
2542

2643
type stdDriver struct {
44+
err error
2745
opt *Options
2846
conn *connect
2947
commit func() error
@@ -34,7 +52,10 @@ func (d *stdDriver) Driver() driver.Driver {
3452
return d
3553
}
3654

37-
func (d *stdDriver) Connect(context.Context) (_ driver.Conn, err error) {
55+
func (d *stdDriver) Connect(context.Context) (driver.Conn, error) {
56+
if d.err != nil {
57+
return nil, d.err
58+
}
3859
return d.Open("")
3960
}
4061

0 commit comments

Comments
 (0)