-
Notifications
You must be signed in to change notification settings - Fork 37
/
dburl.go
476 lines (407 loc) · 10.8 KB
/
dburl.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
464
465
466
467
468
469
470
471
472
473
474
475
476
// Package dburl provides a standard, URL style mechanism for of parsing, and
// opening database connection strings (in the form of a URL).
//
// Database URL Connection Strings
//
// Supported database URLs are of the form:
//
// protocol+transport://user:pass@host/dbname?opt1=a&opt2=b
// protocol:/path/to/file
//
// Where:
//
// protocol - driver name or alias (see below)
// transport - the transport protocol [tcp, udp, unix] (only mysql for now)
// user - the username to connect as
// pass - the password to use
// host - the remote host
// dbname - the database or service name to connect to
// ?opt1=... - additional database driver options
// (see respective SQL driver for available options)
//
// Example URLs for Open or Parse
//
// The following are URLs that can be processed using Parse or Open:
//
// postgres://user:pass@localhost/dbname
// pg://user:pass@localhost/dbname?sslmode=disable
// mysql://user:pass@localhost/dbname
// mysql:/var/run/mysqld/mysqld.sock
// sqlserver://user:pass@remote-host.com/dbname
// oracle://user:pass@somehost.com/oracledb
// sap://user:pass@localhost/dbname
// sqlite:/path/to/file.db
// file:myfile.sqlite3?loc=auto
//
// Parsing Rules
//
// Parse relies heavily on the standard net/url/URL type, as such it has the
// same parsing conventions/semantics as any URL parsed by the standard
// library's net/url/Parse.
//
// SQL Driver Aliases
//
// The following protocol aliases are available, and any URL passed to Open or
// Parse will be processed the same as their respective driver:
//
// Database (driver) | Aliases
// ------------------------------------------------------------------
// Microsoft SQL Server (mssql) | ms, sqlserver
// MySQL (mysql) | my, mariadb, maria, percona, aurora
// Oracle (ora) | or, oracle, oci8, oci
// PostgreSQL (postgres) | pg, postgresql, pgsql
// SAP HANA (hdb) | sa, saphana, sap, hana
// SQLite3 (sqlite3) | sq, sqlite, file
//
// Usage Notes
//
// Please note that this package does not import actual SQL drivers, and only
// provides a standard mechanism to parse their respective URL string.
//
// For reference, these are the following "expected" SQL drivers that would need
// to be imported:
//
// Database (driver) | Package
// ------------------------------------------------------------------
// Microsoft SQL Server (mssql) | github.com/denisenkom/go-mssqldb
// MySQL (mysql) | github.com/go-sql-driver/mysql
// Oracle (ora) | gopkg.in/rana/ora.v4
// PostgreSQL (postgres) | github.com/lib/pq
// SAP HANA (hdb) | github.com/SAP/go-hdb/driver
// SQLite3 (sqlite3) | github.com/mattn/go-sqlite3
//
// Related Projects
//
// This package was written mainly to support xo (https://github.com/knq/xo)
// and usql (https://github.com/knq/usql).
package dburl
import (
"database/sql"
"errors"
"fmt"
"net/url"
"path"
"strconv"
"strings"
)
var (
// ErrInvalidDatabaseScheme is the invalid database scheme error.
ErrInvalidDatabaseScheme = errors.New("invalid database scheme")
// ErrInvalidTransportProtocol is the invalid transport protocol error.
ErrInvalidTransportProtocol = errors.New("invalid transport protocol")
// ErrUnknownDatabaseType is the unknown database type error.
ErrUnknownDatabaseType = errors.New("unknown database type")
// ErrInvalidPort is the invalid port error.
ErrInvalidPort = errors.New("invalid port")
// ErrOraMustProvideUsernameAndPassword is the ora (Oracle) must provide
// username and password error.
ErrOraMustProvideUsernameAndPassword = errors.New("ora: must provide username and password")
)
// URL wraps the standard net/url.URL type, adding OriginalScheme, Proto,
// Driver, and DSN strings.
type URL struct {
// URL is the base net/url/URL.
url.URL
// OriginalScheme is the original parsed scheme (ie, "sq", "mysql+unix", "sap", etc).
OriginalScheme string
// Proto is the specified protocol (ie, "tcp", "udp", "unix"), if provided.
Proto string
// Driver is the non-aliased SQL driver name that should be used in a call
// to sql/Open.
Driver string
// DSN is the built connection "data source name" that can be used in a
// call to sql/Open.
DSN string
}
// String satisfies the stringer interface.
func (u *URL) String() string {
p := &url.URL{
Scheme: u.OriginalScheme,
Opaque: u.Opaque,
User: u.User,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
}
return p.String()
}
// Short provides a short description of the user, host, and database.
func (u *URL) Short() string {
s := u.Driver[:2]
switch s {
case "po":
s = "pg"
case "hd":
s = "sa"
}
if u.Proto != "tcp" {
s += "+" + u.Proto
}
s += ":"
if u.User != nil {
if un := u.User.Username(); un != "" {
s += un + "@"
}
}
if u.Host != "" {
s += u.Host
}
if u.Path != "" && u.Path != "/" {
s += u.Path
}
if u.Opaque != "" {
s += u.Opaque
}
return s
}
// Parse parses a rawurl string and normalizes the scheme.
func Parse(rawurl string) (*URL, error) {
// parse url
u, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
if u.Scheme == "" {
return nil, ErrInvalidDatabaseScheme
}
// create url
v := &URL{URL: *u, OriginalScheme: u.Scheme, Proto: "tcp"}
v.Scheme = strings.ToLower(v.Scheme)
// check if +protocol is in scheme
if strings.Contains(v.Scheme, "+") {
p := strings.SplitN(v.Scheme, "+", 2)
v.Scheme = p[0]
v.Proto = p[1]
}
// check protocol
if v.Proto != "tcp" && v.Proto != "udp" && v.Proto != "unix" {
return nil, ErrInvalidTransportProtocol
}
// get loader
loader, ok := loaders[v.Scheme]
if !ok {
return nil, ErrUnknownDatabaseType
}
// process
v.Driver, v.DSN, err = loader(v)
if err != nil {
return nil, err
}
if v.Driver != "sqlite3" && v.Opaque != "" {
return Parse(v.OriginalScheme + "://" + v.Opaque)
}
return v, nil
}
// Open takes a rawurl like "protocol+transport://user:pass@host/dbname?option1=a&option2=b"
// and creates a standard sql.DB connection.
//
// See Parse for information on formatting URLs to work properly with Open.
func Open(rawurl string) (*sql.DB, error) {
u, err := Parse(rawurl)
if err != nil {
return nil, err
}
return sql.Open(u.Driver, u.DSN)
}
// mssqlProcess processes a mssql url and protocol.
func mssqlProcess(u *URL) (string, string, error) {
var err error
// build host or domain socket
host := u.Host
port := 1433
// grab dbname
var dbname string
if u.Path != "" {
dbname = u.Path[1:]
}
// extract port if present
pos := strings.Index(host, ":")
if pos != -1 {
port, err = strconv.Atoi(host[pos+1:])
if err != nil {
return "", "", ErrInvalidPort
}
host = host[:pos]
}
// format dsn
dsn := fmt.Sprintf("server=%s;port=%d", host, port)
if dbname != "" {
dsn += ";database=" + dbname
}
// add user/pass
if u.User != nil {
if user := u.User.Username(); len(user) > 0 {
dsn += ";user id=" + user
}
if pass, ok := u.User.Password(); ok {
dsn += ";password=" + pass
}
}
// add params
for k, v := range u.Query() {
dsn += ";" + k + "=" + v[0]
}
return "mssql", dsn, nil
}
// mysqlProcess processes a mysql url and protocol.
func mysqlProcess(u *URL) (string, string, error) {
// build host or domain socket
host := u.Host
dbname := u.Path
if strings.HasPrefix(dbname, "/") {
dbname = dbname[1:]
}
if u.Proto == "unix" {
if u.Opaque != "" {
host = path.Dir(u.Opaque)
dbname = path.Base(u.Opaque)
} else {
host = path.Join(u.Host, path.Dir(u.Path))
dbname = path.Base(u.Path)
}
host = host + "/" + dbname
dbname = ""
u.Host = host
u.Path = ""
} else if !strings.Contains(host, ":") {
// append default port
host = host + ":3306"
}
// create dsn
dsn := fmt.Sprintf("%s(%s)", u.Proto, host)
// build user/pass
if u.User != nil {
if un := u.User.Username(); len(un) > 0 {
if up, ok := u.User.Password(); ok {
un += ":" + up
}
dsn = un + "@" + dsn
}
}
// add database name
dsn += "/" + dbname
// add params
params := u.Query().Encode()
if len(params) > 0 {
dsn += "?" + params
}
// format
return "mysql", dsn, nil
}
// oracleProcess processes a ora (Oracle) url and protocol.
func oracleProcess(u *URL) (string, string, error) {
if u.User == nil {
return "", "", ErrOraMustProvideUsernameAndPassword
}
// build host or domain socket
host := u.Host
dbname := u.Path[1:]
// build user/pass
userinfo := ""
if un := u.User.Username(); len(un) > 0 {
userinfo = un
if up, ok := u.User.Password(); ok {
userinfo = userinfo + "/" + up
}
}
// format
return "ora", fmt.Sprintf(
"%s@%s/%s",
userinfo,
host,
dbname,
), nil
}
// postgresProcess processes a postgres url and protocol.
func postgresProcess(u *URL) (string, string, error) {
p := &url.URL{
Scheme: "postgres",
Opaque: u.Opaque,
User: u.User,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
}
return "postgres", p.String(), nil
}
// sapProcess processes a hdb url and protocol.
func sapProcess(u *URL) (string, string, error) {
p := &url.URL{
Scheme: "hdb",
Opaque: u.Opaque,
User: u.User,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
}
return "hdb", p.String(), nil
}
// sqliteProcess processes a sqlite3 url and protocol.
func sqliteProcess(u *URL) (string, string, error) {
dsn := u.Opaque
if u.Path != "" {
dsn = u.Path
}
if u.Host != "" && u.Host != "localhost" {
dsn = path.Join(u.Host, dsn)
}
// add params
params := u.Query().Encode()
if len(params) > 0 {
dsn += "?" + params
}
return "sqlite3", dsn, nil
}
var loaders = map[string]func(*URL) (string, string, error){
// mssql
"mssql": mssqlProcess,
"sqlserver": mssqlProcess,
"ms": mssqlProcess,
// mysql
"mysql": mysqlProcess,
"mariadb": mysqlProcess,
"maria": mysqlProcess,
"percona": mysqlProcess,
"aurora": mysqlProcess,
"my": mysqlProcess,
// oracle
"ora": oracleProcess,
"oracle": oracleProcess,
"oci8": oracleProcess,
"oci": oracleProcess,
"or": oracleProcess,
// postgresql
"postgres": postgresProcess,
"postgresql": postgresProcess,
"pgsql": postgresProcess,
"pg": postgresProcess,
// sqlite
"sqlite3": sqliteProcess,
"sqlite": sqliteProcess,
"file": sqliteProcess,
"sq": sqliteProcess,
// sap hana
"hdb": sapProcess,
"hana": sapProcess,
"sap": sapProcess,
"saphana": sapProcess,
"sa": sapProcess,
}
// AddLoaderAliases copies the existing loader set for name for each of the
// aliases.
func AddLoaderAliases(name string, aliases ...string) error {
f, ok := loaders[name]
if !ok {
return ErrInvalidDatabaseScheme
}
for _, alias := range aliases {
loaders[alias] = f
}
return nil
}