-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
clickhouse.go
91 lines (75 loc) · 1.54 KB
/
clickhouse.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
package testfixtures
import (
"database/sql"
"fmt"
)
type clickhouse struct {
baseHelper
cleanTableFn func(string) string
}
func (h *clickhouse) init(_ *sql.DB) error {
if h.cleanTableFn == nil {
h.cleanTableFn = func(tableName string) string {
return fmt.Sprintf("TRUNCATE TABLE %s", tableName)
}
}
return nil
}
func (*clickhouse) paramType() int {
return paramTypeDollar
}
func (*clickhouse) databaseName(q queryable) (string, error) {
var dbName string
err := q.QueryRow("SELECT DATABASE()").Scan(&dbName)
return dbName, err
}
func (h *clickhouse) tableNames(q queryable) ([]string, error) {
query := `
SELECT name
FROM system.tables
WHERE database = $1;
`
dbName, err := h.databaseName(q)
if err != nil {
return nil, err
}
rows, err := q.Query(query, dbName)
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()
var tables []string
for rows.Next() {
var table string
if err = rows.Scan(&table); err != nil {
return nil, err
}
tables = append(tables, table)
}
if err = rows.Err(); err != nil {
return nil, err
}
return tables, nil
}
func (h *clickhouse) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
tx, err := db.Begin()
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()
err = loadFn(tx)
if err != nil {
return err
}
return tx.Commit()
}
func (h *clickhouse) cleanTableQuery(tableName string) string {
if h.cleanTableFn == nil {
return h.baseHelper.cleanTableQuery(tableName)
}
return h.cleanTableFn(tableName)
}