-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
463 lines (426 loc) · 11.1 KB
/
connect.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package ok
import (
"database/sql"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
type ClickHouse interface {
DB() *sql.DB
Version() (*Version, error)
Exec(query string) error
ExecFromFile(path string) error
SetSearchPath(path ...string)
ShowDatabases() ([]string, error)
ShowTables(from ...string) ([]string, error)
DatabaseExists(database string) bool
TableExists(database, table string) bool
DictionaryExists(dictionary string) bool
ReloadDictionary(dictionary string) bool
CopyFromCSVReader(r io.Reader, sql string) bool
CopyFromTSVReader(r io.Reader, sql string) bool
CopyFromCSVFile(path, sql string) bool
CopyFromTSVFile(path, sql string) bool
DropDatabase(database string) bool
DropTable(database, table string) bool
Clear() bool
}
type Version struct {
Major int
Minor int
Patch int
}
func (v *Version) Less(v2 *Version) bool {
var (
majorEq = v.Major == v2.Major
minorEq = majorEq && v.Minor == v2.Minor
)
return v.Major < v2.Major || (majorEq && v.Minor < v2.Minor) || (minorEq && v.Patch < v2.Patch)
}
func (v *Version) Equal(v2 *Version) bool {
return v.Major == v2.Major && v.Minor == v2.Minor && v.Patch == v2.Patch
}
func (v *Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}
func Connect(test *testing.T, dsn string) ClickHouse {
conn, err := sql.Open("clickhouse", dsn)
if err != nil {
test.Fatalf("could not open ClickHouse driver: %v", err)
}
conn.SetMaxOpenConns(1)
var (
url, _ = url.Parse(dsn)
database = "default"
)
if value := url.Query().Get("database"); len(value) != 0 {
database = value
}
return &clickhouse{
test: test,
conn: conn,
database: database,
searchPath: []string{"", "."},
}
}
type clickhouse struct {
test *testing.T
conn *sql.DB
database string
searchPath []string
clear struct {
databases []string
tables [][]string
}
}
func (c *clickhouse) DB() *sql.DB {
return c.conn
}
func (c *clickhouse) SetSearchPath(path ...string) {
c.searchPath = path
}
func (c *clickhouse) Version() (*Version, error) {
var version Version
const query = `
WITH (splitByChar('.',version())) AS version
SELECT
toInt16(version[1]) AS major
, toInt16(version[2]) AS minor
, toInt16(version[3]) AS patch
`
if err := c.conn.QueryRow(query).Scan(&version.Major, &version.Minor, &version.Patch); err != nil {
return nil, err
}
return &version, nil
}
func (c *clickhouse) ShowDatabases() (databases []string, _ error) {
rows, err := c.conn.Query("SHOW DATABASES")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var database string
if err := rows.Scan(&database); err != nil {
return nil, err
}
databases = append(databases, database)
}
return databases, nil
}
func (c *clickhouse) ShowTables(from ...string) (tables []string, _ error) {
query := "SHOW TABLES"
if len(from) != 0 {
query = "SHOW TABLES FROM " + from[0]
}
rows, err := c.conn.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var table string
if err := rows.Scan(&table); err != nil {
return nil, err
}
tables = append(tables, table)
}
return tables, nil
}
func (c *clickhouse) DatabaseExists(database string) bool {
exists, err := c.exists("SELECT COUNT() FROM system.databases WHERE name = ?", database)
if err != nil {
c.test.Errorf("an error occurred while checking the database: %v", err)
return false
}
return exists
}
func (c *clickhouse) TableExists(database, table string) bool {
exists, err := c.exists("SELECT COUNT() FROM system.tables WHERE database = ? AND name = ?", database, table)
if err != nil {
c.test.Errorf("an error occurred while checking the table: %v", err)
return false
}
return exists
}
func (c *clickhouse) DictionaryExists(dictionary string) bool {
exists, err := c.exists("SELECT COUNT() FROM system.dictionaries WHERE name = ?", dictionary)
if err != nil {
c.test.Errorf("an error occurred while checking the dictionary: %v", err)
return false
}
return exists
}
func (c *clickhouse) exists(query string, args ...interface{}) (bool, error) {
var count int
if err := c.conn.QueryRow(query, args...).Scan(&count); err != nil {
return false, err
}
return count == 1, nil
}
func (c *clickhouse) ReloadDictionary(dictionary string) bool {
if _, err := c.conn.Exec("SYSTEM RELOAD DICTIONARY " + quote(dictionary)); err != nil {
c.test.Errorf("an error occurred while reloading dictionary: %v", err)
return false
}
return true
}
func (c *clickhouse) DropDatabase(database string) bool {
if err := c.Exec("DROP DATABASE IF EXISTS " + database); err != nil {
c.test.Errorf("an error occurred while deleting the database: %v", err)
return false
}
return true
}
func (c *clickhouse) DropTable(database, table string) bool {
if err := c.Exec("DROP TABLE IF EXISTS " + database + "." + table); err != nil {
c.test.Errorf("an error occurred while deleting the table: %v", err)
return false
}
return true
}
func (c *clickhouse) Exec(query string) error {
for _, query := range strings.Split(query, ";\n") {
if query = strings.TrimSpace(query); len(query) != 0 {
if _, err := c.conn.Exec(query); err != nil {
return err
}
if database := extractCreateDatabase(query); len(database) != 0 {
c.clear.databases = append(c.clear.databases, database)
}
if database, table := extractCreateTable(query); len(table) != 0 {
c.clear.tables = append(c.clear.tables, []string{database, table})
}
}
}
return nil
}
func (c *clickhouse) ExecFromFile(path string) (err error) {
var data []byte
for _, searchPath := range c.searchPath {
if data, err = ioutil.ReadFile(filepath.Join(searchPath, path)); err == nil {
return c.Exec(string(data))
}
}
return err
}
func (c *clickhouse) CopyFromCSVReader(r io.Reader, query string) bool {
return c.copyFromReader(r, query, ',')
}
func (c *clickhouse) CopyFromTSVReader(r io.Reader, query string) bool {
return c.copyFromReader(r, query, '\t')
}
func (c *clickhouse) CopyFromCSVFile(path, query string) bool {
return c.copyFromFile(path, query, ',')
}
func (c *clickhouse) CopyFromTSVFile(path, query string) bool {
return c.copyFromFile(path, query, '\t')
}
func (c *clickhouse) copyFromFile(path, query string, comma rune) bool {
file, err := c.openFile(path)
if err != nil {
c.test.Errorf("could not open file: %v", err)
return false
}
defer file.Close()
return c.copyFromReader(file, query, comma)
}
func (c *clickhouse) copyFromReader(r io.Reader, query string, comma rune) bool {
database, table, columns := parseQuery(query)
if len(table) == 0 {
c.test.Error("error while parsing query: cannot find table name")
return false
}
if len(database) == 0 {
database = c.database
}
columnTypes, err := c.columnTypes(database, table, columns)
if err != nil {
c.test.Error(err)
return false
}
rows, err := csvToArgs(columnTypes, r, comma)
if err != nil {
c.test.Error(err)
return false
}
scope, err := c.conn.Begin()
if err != nil {
c.test.Error(err)
return false
}
block, err := scope.Prepare(query)
if err != nil {
c.test.Error(err)
return false
}
for _, row := range rows {
if _, err := block.Exec(row...); err != nil {
c.test.Error(err)
return false
}
}
if err := scope.Commit(); err != nil {
c.test.Error(err)
return false
}
return true
}
func (c *clickhouse) Clear() bool {
ok := true
for _, tuple := range c.clear.tables {
database := c.database
if len(tuple[0]) != 0 {
database = tuple[0]
}
if !c.DropTable(database, tuple[1]) {
ok = false
}
}
for _, database := range c.clear.databases {
if !c.DropDatabase(database) {
ok = false
}
}
return ok
}
func (c *clickhouse) columnTypes(database, table string, columns []string) (types []string, err error) {
var (
args = []interface{}{database, table}
query = "SELECT name, type FROM system.columns WHERE database = ? AND table = ?"
)
if len(columns) != 0 {
query += " AND name IN(?)"
args = append(args, columns)
}
rows, err := c.conn.Query(query, args...)
if err != nil {
return nil, err
}
columnTypes := make(map[string]string, len(columns))
for rows.Next() {
var (
column, columnType string
)
if err := rows.Scan(&column, &columnType); err != nil {
return nil, err
}
switch {
case len(columns) == 0:
types = append(types, columnType)
default:
columnTypes[column] = columnType
}
}
for _, column := range columns {
if _, found := columnTypes[column]; !found {
return nil, fmt.Errorf("column '%s' does not exists", column)
}
types = append(types, columnTypes[column])
}
return types, nil
}
func (c *clickhouse) openFile(path string) (file *os.File, err error) {
for _, searchPath := range c.searchPath {
if file, err = os.Open(filepath.Join(searchPath, path)); err == nil {
return file, nil
}
}
return nil, err
}
var _ ClickHouse = (*clickhouse)(nil)
func quote(v string) string {
return "'" + strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace(v) + "'"
}
func parseQuery(query string) (database string, table string, columns []string) {
var (
isOpen bool
fields = strings.Fields(query)
appendColumn = func(field string) {
for _, column := range strings.Split(field, ",") {
if len(column) != 0 {
columns = append(columns, column)
}
}
}
)
parse:
for i, field := range fields {
if len(table) == 0 && strings.ToUpper(field) == "INTO" {
switch parts := strings.Split(fields[i+1], "."); len(parts) {
case 1:
table = parts[0]
case 2:
database, table = parts[0], parts[1]
}
}
if len(field) == 0 || field == "," {
continue parse
}
switch {
case field == "(" || strings.HasPrefix(field, "("):
switch {
case strings.HasSuffix(field, ")"):
appendColumn(field[1 : len(field)-1])
break parse
default:
appendColumn(field[1:])
}
isOpen = true
case field == ")" || strings.HasSuffix(field, ")"):
appendColumn(field[:len(field)-1])
break parse
case isOpen:
appendColumn(field)
}
}
return database, table, columns
}
func extractCreateDatabase(ddl string) string {
var check bool
if strings.HasPrefix(strings.ToUpper(strings.TrimSpace(ddl)), "CREATE") {
for _, field := range strings.Fields(ddl) {
switch {
case strings.ToUpper(field) == "DATABASE" && !check:
check = true
case check:
switch strings.ToUpper(field) {
case "IF", "NOT", "EXISTS":
default:
if len(field) != 0 {
return strings.TrimSpace(strings.TrimSuffix(field, ";"))
}
}
}
}
}
return ""
}
func extractCreateTable(ddl string) (string, string) {
var check bool
if strings.HasPrefix(strings.ToUpper(strings.TrimSpace(ddl)), "CREATE") {
for _, field := range strings.Fields(ddl) {
switch {
case strings.ToUpper(field) == "TABLE" && !check:
check = true
case check:
if field = strings.TrimSpace(strings.TrimSuffix(field, "(")); len(field) != 0 {
switch strings.ToUpper(field) {
case "IF", "NOT", "EXISTS":
default:
if parts := strings.Split(field, "."); len(parts) == 2 {
return parts[0], parts[1]
}
return "", field
}
}
}
}
}
return "", ""
}