-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspecials.go
More file actions
155 lines (129 loc) · 2.85 KB
/
Copy pathspecials.go
File metadata and controls
155 lines (129 loc) · 2.85 KB
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
package uri
import (
"bytes"
"fmt"
)
// MySQLURIBuilder builds a URI for use with the gomysql database/sql
// driver. Useage is fairly simple:
//
// b := NewMySQLURIBuilder()
// b.SetUser(user)
// b.SetPassword(password)
// b.SetHost(host)
// b.SetPort(port)
//
// driverURI := b.String()
type MySQLURIBuilder interface {
URIBuilder
// mysql specific fun
SetUser(user string) MySQLURIBuilder
SetPassword(password string) MySQLURIBuilder
SetDB(db string) MySQLURIBuilder
}
type mysqlURI struct {
*uri
username string
password string
db string
}
// NewMySQLURIBuilder returns a URI builder for builder
// gomysql driver URIs.
func NewMySQLURIBuilder() MySQLURIBuilder {
return &mysqlURI{
uri: &uri{
authority: &authorityInfo{},
},
}
}
func (m *mysqlURI) SetUser(user string) MySQLURIBuilder {
m.username = user
return m
}
func (m *mysqlURI) SetPassword(password string) MySQLURIBuilder {
m.password = password
return m
}
func (m *mysqlURI) SetDB(db string) MySQLURIBuilder {
m.db = db
return m
}
func (m *mysqlURI) String() string {
host := m.uri.authority.host
if len(host) == 0 {
host = "localhost"
}
port := m.uri.authority.port
if len(port) == 0 {
port = "3306"
}
return fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?",
m.username,
m.password,
host,
port,
m.db,
)
}
// PostgresqlURIBuilder is a URI builder for
// connecting to Postgresql databases. Usage is similar to
// MySQLURIBuilder.
type PostgresqlURIBuilder interface {
URIBuilder
// mysql specific fun
SetUser(user string) PostgresqlURIBuilder
SetPassword(password string) PostgresqlURIBuilder
SetDB(db string) PostgresqlURIBuilder
}
type postgresqlURI struct {
*uri
username string
password string
db string
}
func NewPostgresqlURIBuilder() PostgresqlURIBuilder {
return &postgresqlURI{
uri: &uri{
authority: &authorityInfo{},
},
}
}
func (m *postgresqlURI) SetUser(user string) PostgresqlURIBuilder {
m.username = user
return m
}
func (m *postgresqlURI) SetPassword(password string) PostgresqlURIBuilder {
m.password = password
return m
}
func (m *postgresqlURI) SetDB(db string) PostgresqlURIBuilder {
m.db = db
return m
}
func (m *postgresqlURI) String() string {
// This is how postgresql connection strings should look:
//
// postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
var buf = bytes.NewBufferString("postgresql://")
if len(m.username) > 0 {
buf.WriteString(m.username)
if len(m.password) > 0 {
buf.Write(colonBytes)
buf.WriteString(m.password)
}
buf.Write(atBytes)
}
if len(m.uri.authority.host) > 0 {
buf.WriteString(m.uri.authority.host)
}
if len(m.uri.authority.port) > 0 {
buf.Write(colonBytes)
buf.WriteString(m.uri.authority.port)
}
if len(m.db) > 0 {
buf.Write(slashBytes)
buf.WriteString(m.db)
}
// TODO(ttacon): actually use options/query
return buf.String()
}