-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdriver.go
368 lines (296 loc) · 7.11 KB
/
driver.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
package sqlagent
import (
"errors"
"fmt"
"net/url"
"strings"
)
// Connector takes a map of connection parameters and converts them into a
// connection string. In order to support a uniform way of specifying connection
// options, a connector must be used to conver the map of options to a
// connection the underlying driver supports.
type connector func(map[string]interface{}) string
// cleanParams removes any key with empty string values.
func cleanParams(p map[string]interface{}) map[string]interface{} {
c := make(map[string]interface{})
for k, v := range p {
switch x := v.(type) {
case string:
if x == "" {
continue
}
}
c[k] = v
}
return c
}
// Set of connectors for each internal driver.
var connectors = map[string]connector{
// Postgres supports space-delimited key=value pairs.
// See http://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters
"postgres": func(params map[string]interface{}) string {
toks := make([]string, len(params))
i := 0
for k, v := range params {
toks[i] = fmt.Sprintf("%s=%v", k, v)
i++
}
return strings.Join(toks, " ")
},
// MySQL has a more complex format.
// See: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
"mysql": func(params map[string]interface{}) string {
var (
user, pass, db interface{}
host interface{} = "localhost"
port interface{} = 3306
query []string
)
for k, v := range params {
switch k {
case "user":
user = v
case "password":
pass = v
case "host":
host = v
case "port":
port = v
case "database":
db = v
default:
query = append(query, fmt.Sprintf("%s=%s", k, url.QueryEscape(fmt.Sprint(v))))
}
}
var conn string
if user != nil {
conn += fmt.Sprintf("%s", user)
}
if pass != nil {
conn += fmt.Sprintf(":%s", pass)
}
if conn != "" {
conn += "@"
}
// Only TCP is supported.
conn += "tcp"
// If a host is supplied a port must as well or vice versa.
if host != nil || port != nil {
conn += fmt.Sprintf("(%s:%v)", host, port)
}
conn += fmt.Sprintf("/%v", db)
if len(query) > 0 {
conn += fmt.Sprintf("?%s", strings.Join(query, "&"))
}
return conn
},
// SQLite3 requires the path and supports other query parameters.
// See http://godoc.org/github.com/mattn/go-sqlite3#SQLiteDriver.Open
"sqlite3": func(params map[string]interface{}) string {
var (
db interface{} = ":memory:"
query []string
)
for k, v := range params {
if k == "database" {
db = v
} else {
query = append(query, fmt.Sprintf("%s=%s", k, url.QueryEscape(fmt.Sprint(v))))
}
}
if len(query) == 0 {
return fmt.Sprint(db)
}
return fmt.Sprintf("%s?%s", db, strings.Join(query, "&"))
},
// MSSQL supports semicolon delimited key=value parameters.
// See https://github.com/denisenkom/go-mssqldb#connection-parameters-and-dsn
"mssql": func(params map[string]interface{}) string {
toks := make([]string, len(params))
i := 0
for k, v := range params {
switch k {
case "host":
k = "server"
case "user":
k = "user id"
}
toks[i] = fmt.Sprintf("%s=%v", k, v)
i++
}
return strings.Join(toks, ";")
},
// Oracle supports a standard URI-based connection string.
// See http://godoc.org/github.com/mattn/go-oci8#ParseDSN
"oci8": func(params map[string]interface{}) string {
var (
user, pass, db interface{}
host interface{} = "localhost"
port interface{} = 1521
query []string
)
for k, v := range params {
switch k {
case "user":
user = v
case "password":
pass = v
case "host":
host = v
case "port":
port = v
case "database":
db = v
default:
query = append(query, fmt.Sprintf("%s=%s", k, url.QueryEscape(fmt.Sprint(v))))
}
}
var conn string
if user != nil {
conn += fmt.Sprintf("%s", user)
}
if pass != nil {
conn += fmt.Sprintf("/%s", pass)
}
if conn != "" {
conn += "@"
}
// If a host is supplied a port must as well or vice versa.
if host != nil {
conn += fmt.Sprint(host)
}
if port != nil {
conn += fmt.Sprintf(":%v", port)
}
conn += fmt.Sprintf("/%s", db)
if len(query) > 0 {
conn += fmt.Sprintf("?%s", strings.Join(query, "&"))
}
return conn
},
// Snowflake supports space-delimited key=value pairs.
// See https://github.com/snowflakedb/gosnowflake#dsn-data-source-name
"snowflake": func(params map[string]interface{}) string {
var (
user, pass, db interface{}
host interface{} = "localhost"
port interface{}
query []string
)
for k, v := range params {
switch k {
case "user":
user = v
case "password":
pass = v
case "host":
host = v
case "port":
port = v
case "database":
db = v
default:
query = append(query, fmt.Sprintf("%s=%s", k, url.QueryEscape(fmt.Sprint(v))))
}
}
var conn string
if user != nil {
conn += fmt.Sprintf("%s", user)
}
if pass != nil {
conn += fmt.Sprintf(":%s", pass)
}
if conn != "" {
conn += "@"
}
conn += fmt.Sprint(host)
// If a host is supplied a port must as well or vice versa.
if port != nil {
conn += fmt.Sprintf(":%v", port)
}
conn += fmt.Sprintf("/%v", db)
if len(query) > 0 {
conn += fmt.Sprintf("?%s", strings.Join(query, "&"))
}
return conn
},
"odbc": func(params map[string]interface{}) string {
toks := make([]string, len(params))
i := 0
for k, v := range params {
switch k {
case "host":
k = "server"
case "user":
k = "username"
}
toks[i] = fmt.Sprintf("%s=%v", k, v)
i++
}
return strings.Join(toks, ";")
},
"presto": func(params map[string]interface{}) string {
var (
query = url.Values{}
host interface{}
port interface{}
user string
password string
)
for k, v := range params {
switch k {
case "host":
host = v
case "port":
port = v
case "user":
user = fmt.Sprint(v)
case "password":
password = fmt.Sprint(v)
default:
query.Add(k, fmt.Sprint(v))
}
}
u := url.URL{
Scheme: "http",
}
if port != nil {
u.Host = fmt.Sprintf("%s:%d", host, port)
} else {
u.Host = fmt.Sprintf("%s", host)
}
if user != "" && password != "" {
u.User = url.UserPassword(user, password)
} else if user != "" {
u.User = url.User(user)
}
u.RawQuery = query.Encode()
return u.String()
},
}
// mapBytesToString ensures byte slices that were returned from the database
// are represented as strings.
// See https://github.com/jmoiron/sqlx/issues/135
func mapBytesToString(m map[string]interface{}) {
for k, v := range m {
if b, ok := v.([]byte); ok {
m[k] = string(b)
}
}
}
// ErrUnknownDriver is returned when an unknown driver is used when attempting to connect.
var ErrUnknownDriver = errors.New("sqlagent: Unknown driver")
// Drivers contains a map of public driver names to registered driver names.
var Drivers = map[string]string{
"postgresql": "postgres",
"postgres": "postgres",
"mysql": "mysql",
"mariadb": "mysql",
"sqlite": "sqlite3",
"mssql": "mssql",
"sqlserver": "mssql",
"oracle": "oci8",
"snowflake": "snowflake",
"odbc": "odbc",
"presto": "presto",
}