forked from mintance/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
121 lines (92 loc) · 2.32 KB
/
helpers.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
package clickhouse
import (
"errors"
"strings"
"fmt"
"net/url"
)
type (
Column string
Columns []string
Row []interface{}
Rows []Row
Array []interface{}
VisitParamsString map[string]interface{}
StringArray []string
)
func NewHttpTransport() HttpTransport {
return HttpTransport{}
}
func NewConn(host string, t Transport) *Conn {
if strings.Index(host, "http://") < 0 && strings.Index(host, "https://") < 0 {
host = "http://" + host
}
host = strings.TrimRight(host, "/") + "/"
return &Conn{
Host: host,
transport: t,
params: url.Values{},
}
}
func NewQuery(stmt string, args ...interface{}) Query {
return Query{
Stmt: stmt,
args: args,
params:url.Values{},
}
}
func OptimizeTable(table string) Query {
return NewQuery("OPTIMIZE TABLE " + table)
}
func OptimizePartition(table string, partition string) Query {
return NewQuery("OPTIMIZE TABLE " + table + " PARTITION " + partition + " FINAL")
}
func IsLeader(table string, conn *Conn) bool {
var leader uint8
settings := strings.Split(table, ".")
database := "default"
if len(settings) > 1 {
database = settings[0]
table = settings[1]
}
query := NewQuery("SELECT is_leader FROM system.replicas WHERE database = '" + database + "' table = '" + table + "'")
iter := query.Iter(conn)
if iter.Error() != nil {
return false
}
for iter.Scan(&leader) {
return leader == 1
}
return false
}
func BuildInsert(tbl string, cols Columns, row Row) (Query, error) {
return BuildMultiInsert(tbl, cols, Rows{row})
}
func BuildMultiInsert(tbl string, cols Columns, rows Rows) (Query, error) {
var (
stmt string
args []interface{}
)
if len(cols) == 0 || len(rows) == 0 {
return Query{}, errors.New("rows and cols cannot be empty")
}
colCount := len(cols)
rowCount := len(rows)
args = make([]interface{}, colCount*rowCount)
argi := 0
for _, row := range rows {
if len(row) != colCount {
return Query{}, errors.New("Amount of row items does not match column count")
}
for _, val := range row {
args[argi] = val
argi++
}
}
binds := strings.Repeat(":value:,", colCount)
binds = "(" + binds[:len(binds)-1] + "),"
batch := strings.Repeat(binds, rowCount)
batch = batch[:len(batch)-1]
stmt = fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", tbl, strings.Join(cols, ","), batch)
return NewQuery(stmt, args...), nil
}