/
datasource.go
69 lines (55 loc) · 1.29 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
package gdbc
import (
"codnect.io/procyon-data/sql"
dsql "database/sql"
"errors"
"strings"
)
type DataSource struct {
props DataSourceProperties
}
type DataSourceProperties struct {
Url string
Username string
Password string
}
func NewDataSource(props DataSourceProperties) *DataSource {
return &DataSource{
props: props,
}
}
func (ds *DataSource) GetConnection() (sql.Connection, error) {
var (
db *dsql.DB
driverName string
connectionUrl string
err error
)
driverName, connectionUrl, err = ds.parseDataSourceUrl()
if err != nil {
return nil, err
}
db, err = dsql.Open(driverName, connectionUrl)
if err != nil {
return nil, err
}
return NewConnection(db), nil
}
func (ds *DataSource) parseDataSourceUrl() (string, string, error) {
if len(ds.props.Url) == 0 {
return "", "", errors.New("url must not be empty")
}
src := strings.Split(ds.props.Url, ":")
if len(src) < 3 {
return "", "", errors.New("url format is wrong : " + ds.props.Url)
}
scheme := src[0]
if "gdbc" != scheme {
return "", "", errors.New("driver name must not be empty")
}
driverName := src[1]
if len(driverName) == 0 {
return "", "", errors.New("driver name must not be empty")
}
return driverName, strings.Join(append(src[:1], src[2:]...), ":"), nil
}