forked from dimkus/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
158 lines (125 loc) · 2.89 KB
/
transport.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
package clickhouse
import (
"bytes"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
)
const (
httpTransportBodyType = "text/plain"
)
type Transport interface {
Exec(conn *Conn, q Query, readOnly bool) (res string, err error)
}
type HttpTransport struct {
Timeout time.Duration
}
func (t HttpTransport) Exec(conn *Conn, q Query, readOnly bool) (res string, err error) {
var resp *http.Response
query := prepareHttp(q.Stmt, q.args)
client := &http.Client{Timeout: t.Timeout}
if readOnly {
if len(query) > 0 {
query = "?query=" + query
}
params := conn.params.Encode()
if len(params) > 0 {
if len(query) > 0 {
query += "&" + params
} else {
query += "?" + params
}
}
resp, err = client.Get(conn.Host + query)
} else {
var req *http.Request
// Set global parameters for query, like: user, password, max_memory_limit, etc.
// But it skips already defined params.
req, err = prepareExecPostRequest(conn, q)
if err != nil {
return "", err
}
resp, err = client.Do(req)
}
if err != nil {
return "", err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
return buf.String(), err
}
func prepareExecPostRequest(conn *Conn, q Query) (*http.Request, error) {
query := prepareHttp(q.Stmt, q.args)
var req *http.Request
var err error = nil
if len(q.externals) > 0 {
if len(query) > 0 {
query = "?query=" + url.QueryEscape(query)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, ext := range q.externals {
query = query + "&" + ext.Name + "_structure=" + url.QueryEscape(ext.Structure)
part, err := writer.CreateFormFile(ext.Name, ext.Name)
if err != nil {
return nil, err
}
_, err = part.Write(ext.Data)
if err != nil {
return nil, err
}
}
params := q.params.Encode()
if len(params) > 0 {
query += "&" + params
}
paramsCon := conn.params.Encode()
if len(paramsCon) > 0 {
query += "&" + paramsCon
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err = http.NewRequest("POST", conn.Host + query, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
} else {
req, err = http.NewRequest("POST", conn.Host + "?" + conn.params.Encode(), strings.NewReader(query))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", httpTransportBodyType)
}
return req, err
}
func prepareHttp(stmt string, args []interface{}) string {
if len(args) == 0 {
return stmt
}
var res []byte
buf := []byte(stmt)
res = make([]byte, 0)
k := 0
skip_to := -1
for key, ch := range buf {
if skip_to != -1 && key < skip_to {
continue
} else {
skip_to = -1
}
if ch == ':' && stmt[key:key + 7] == ":value:" {
res = append(res, []byte(marshal(args[k]))...)
k++
skip_to = key + 7
} else {
res = append(res, ch)
}
}
return string(res)
}