-
Notifications
You must be signed in to change notification settings - Fork 541
/
utils.go
211 lines (193 loc) · 5.62 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package std
import (
"crypto/tls"
"database/sql"
"encoding/json"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"net/url"
"strconv"
"strings"
"time"
)
func GetStdTestEnvironment() (clickhouse_tests.ClickHouseTestEnvironment, error) {
return clickhouse_tests.GetTestEnvironment("std")
}
func CheckMinServerVersion(conn *sql.DB, major, minor, patch uint64) bool {
var res string
if err := conn.QueryRow("SELECT version()").Scan(&res); err != nil {
panic(err)
}
var version proto.Version
for i, v := range strings.Split(res, ".") {
switch i {
case 0:
version.Major, _ = strconv.ParseUint(v, 10, 64)
case 1:
version.Minor, _ = strconv.ParseUint(v, 10, 64)
case 2:
version.Patch, _ = strconv.ParseUint(v, 10, 64)
}
}
return proto.CheckMinVersion(proto.Version{
Major: major,
Minor: minor,
Patch: patch,
}, version)
}
func GetDSNConnection(environment string, protocol clickhouse.Protocol, secure bool, opts url.Values) (*sql.DB, error) {
env, err := clickhouse_tests.GetTestEnvironment(environment)
if err != nil {
return nil, err
}
insertQuorum := clickhouse_tests.GetEnv("CLICKHOUSE_QUORUM_INSERT", "1")
scheme := "clickhouse"
port := env.Port
query := opts
if query == nil {
query = make(url.Values)
}
query.Set("insert_quorum", insertQuorum)
query.Set("insert_quorum_parallel", "0")
query.Set("select_sequential_consistency", "1")
if proto.CheckMinVersion(proto.Version{
Major: 22,
Minor: 8,
Patch: 0,
}, env.Version) {
query.Set("database_replicated_enforce_synchronous_settings", "1")
}
if protocol == clickhouse.HTTP {
query.Set("wait_end_of_query", "1")
if secure {
scheme = "https"
port = env.HttpsPort
} else {
scheme = "http"
port = env.HttpPort
}
} else {
if secure {
port = env.SslPort
}
}
if secure {
query.Set("secure", "true")
}
dsn := url.URL{
Scheme: scheme,
User: url.UserPassword(env.Username, env.Password),
Host: fmt.Sprintf("%s:%d", env.Host, port),
Path: env.Database,
RawQuery: query.Encode(),
}
return sql.Open("clickhouse", dsn.String())
}
func GetConnectionFromDSN(dsn string) (*sql.DB, error) {
conn, err := sql.Open("clickhouse", dsn)
if err != nil {
return conn, err
}
if CheckMinServerVersion(conn, 22, 8, 0) {
dsn = fmt.Sprintf("%s&database_replicated_enforce_synchronous_settings=1", dsn)
}
insertQuorum := clickhouse_tests.GetEnv("CLICKHOUSE_QUORUM_INSERT", "1")
dsn = fmt.Sprintf("%s&insert_quorum=%s&insert_quorum_parallel=0&select_sequential_consistency=1", dsn, insertQuorum)
if strings.HasPrefix(dsn, "http") {
dsn = fmt.Sprintf("%s&wait_end_of_query=1", dsn)
}
return sql.Open("clickhouse", dsn)
}
func GetConnectionWithOptions(options *clickhouse.Options) *sql.DB {
if options.Settings == nil {
options.Settings = clickhouse.Settings{}
}
conn := clickhouse.OpenDB(options)
if CheckMinServerVersion(conn, 22, 8, 0) {
options.Settings["database_replicated_enforce_synchronous_settings"] = "1"
}
var err error
options.Settings["insert_quorum"], err = strconv.Atoi(clickhouse_tests.GetEnv("CLICKHOUSE_QUORUM_INSERT", "1"))
options.Settings["insert_quorum_parallel"] = 0
options.Settings["select_sequential_consistency"] = 1
if err != nil {
return nil
}
return clickhouse.OpenDB(options)
}
func GetOpenDBConnection(environment string, protocol clickhouse.Protocol, settings clickhouse.Settings, tlsConfig *tls.Config, compression *clickhouse.Compression) (*sql.DB, error) {
env, err := clickhouse_tests.GetTestEnvironment(environment)
if err != nil {
return nil, err
}
var port int
switch protocol {
case clickhouse.HTTP:
port = env.HttpPort
if tlsConfig != nil {
port = env.HttpsPort
}
case clickhouse.Native:
port = env.Port
if tlsConfig != nil {
port = env.SslPort
}
}
if settings == nil {
settings = clickhouse.Settings{}
}
if protocol == clickhouse.HTTP {
settings["wait_end_of_query"] = 1
}
settings["insert_quorum"], err = strconv.Atoi(clickhouse_tests.GetEnv("CLICKHOUSE_QUORUM_INSERT", "1"))
settings["insert_quorum_parallel"] = 0
settings["select_sequential_consistency"] = 1
if proto.CheckMinVersion(proto.Version{
Major: 22,
Minor: 8,
Patch: 0,
}, env.Version) {
settings["database_replicated_enforce_synchronous_settings"] = "1"
}
if err != nil {
return nil, err
}
return clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", env.Host, port)},
Auth: clickhouse.Auth{
Database: env.Database,
Username: env.Username,
Password: env.Password,
},
Settings: settings,
DialTimeout: 5 * time.Second,
Compression: compression,
TLS: tlsConfig,
Protocol: protocol,
}), nil
}
func ToJson(obj any) string {
bytes, err := json.Marshal(obj)
if err != nil {
return "unable to marshal"
}
return string(bytes)
}