-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
124 lines (102 loc) · 2.67 KB
/
datasource.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
package gdbc
import (
"database/sql"
"errors"
"fmt"
"net/url"
"strings"
)
type DataSourceOption func(dataSource DataSource)
const Scheme = "gdbc"
type DataSource interface {
GetDriverName() string
GetURL() *url.URL
GetConnection() (*sql.DB, error)
GetUsername() string
SetUsername(username string)
GetPassword() string
SetPassword(password string)
}
type SimpleDataSource struct {
driverName string
url *url.URL
username string
password string
}
func (dataSource *SimpleDataSource) GetDriverName() string {
return dataSource.driverName
}
func (dataSource *SimpleDataSource) GetURL() *url.URL {
return dataSource.url
}
func (dataSource *SimpleDataSource) GetUsername() string {
return dataSource.username
}
func (dataSource *SimpleDataSource) SetUsername(username string) {
dataSource.username = username
}
func (dataSource *SimpleDataSource) GetPassword() string {
return dataSource.password
}
func (dataSource *SimpleDataSource) SetPassword(password string) {
dataSource.password = password
}
func (dataSource *SimpleDataSource) GetConnection() (*sql.DB, error) {
dsnAdapter := GetDataSourceNameAdapter(dataSource.driverName)
if dsnAdapter == nil {
return nil, fmt.Errorf("sql: driver does not exist : %s", dataSource.driverName)
}
dataSourceName, err := dsnAdapter.GetDataSourceName(dataSource)
if err != nil {
return nil, err
}
driverName := driverNameMap[dataSource.driverName]
return sql.Open(driverName, dataSourceName)
}
func GetDataSource(url string, options ...DataSourceOption) (DataSource, error) {
dataSource, err := parse(url)
if err != nil {
return nil, err
}
for _, option := range options {
option(dataSource)
}
return dataSource, nil
}
func Username(username string) DataSourceOption {
return func(dataSource DataSource) {
dataSource.SetUsername(username)
}
}
func Password(password string) DataSourceOption {
return func(dataSource DataSource) {
dataSource.SetPassword(password)
}
}
func parse(dataSourceUrl string) (DataSource, error) {
src := strings.Split(dataSourceUrl, ":")
if len(src) < 3 {
return nil, errors.New("url format is wrong : " + dataSourceUrl)
}
scheme := src[0]
if Scheme != scheme {
return nil, errors.New("url must start with 'gdbc'")
}
driverName := src[1]
if len(driverName) == 0 {
return nil, errors.New("driver name must not be empty")
}
dataSource := &SimpleDataSource{
driverName: driverName,
}
rest := strings.Join(append(src[:1], src[2:]...), ":")
if rest != Scheme+":" {
parsedUrl, err := url.ParseRequestURI(rest)
if err != nil {
return nil, err
}
dataSource.url = parsedUrl
return dataSource, nil
}
return nil, errors.New("url format is wrong : " + dataSourceUrl)
}