-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdriver.go
161 lines (142 loc) · 3.37 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
/*
Copyright 2020 JM Robles (@jmrobles)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package h2go
import (
"context"
"database/sql"
"database/sql/driver"
"net"
"net/url"
"strconv"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var doLogging = false
type h2connInfo struct {
host string
port int
database string
username string
password string
isMem bool
logging bool
dialer net.Dialer
}
type h2Driver struct {
driver.DriverContext
driver.Driver
}
type h2Connector struct {
driver.Connector
ci h2connInfo
driver h2Driver
}
func (h2d h2Driver) Open(dsn string) (driver.Conn, error) {
ci, err := parseURL(dsn)
L(log.InfoLevel, "Open")
L(log.DebugLevel, "Open with dsn: %s", dsn)
if err != nil {
return nil, err
}
return connect(ci)
}
func (h2d *h2Driver) OpenConnector(dsn string) (driver.Connector, error) {
L(log.DebugLevel, "OpenConnector")
ci, err := parseURL(dsn)
if err != nil {
return nil, err
}
return &h2Connector{ci: ci, driver: *h2d}, nil
}
func (h2c *h2Connector) Connect(ctx context.Context) (driver.Conn, error) {
L(log.DebugLevel, "Connect")
return connect(h2c.ci)
}
func (h2c *h2Connector) Driver() driver.Driver {
return h2c.driver
}
func init() {
sql.Register("h2", &h2Driver{})
}
// Helpers
func parseURL(dsnurl string) (h2connInfo, error) {
var ci h2connInfo
u, err := url.Parse(dsnurl)
if err != nil {
return ci, errors.Wrapf(err, "failed to parse connection url")
}
// Set host
if ci.host = u.Hostname(); len(ci.host) == 0 {
ci.host = "127.0.0.1"
}
// Set port
ci.port, _ = strconv.Atoi(u.Port())
if ci.port == 0 {
ci.port = defaultH2port
}
// Set database
if ci.database = u.Path; len(ci.database) == 0 {
ci.database = "~/test"
}
// Username & password
userinfo := u.User
if userinfo != nil {
ci.username = userinfo.Username()
if pass, ok := userinfo.Password(); ok {
ci.password = pass
}
}
for k, v := range u.Query() {
var val string
if len(v) > 0 {
val = strings.TrimSpace(v[0])
}
switch strings.ToLower(k) {
case "mem":
ci.isMem = val == "" || val == "1" || val == "yes" || val == "true"
if ci.isMem {
ci.database = strings.Replace(ci.database, "/", "", 1)
ci.database = "mem:" + ci.database
}
case "logging":
logType := strings.ToLower(v[0])
switch logType {
case "none":
doLogging = false
case "info":
doLogging = true
log.SetLevel(log.InfoLevel)
case "debug":
doLogging = true
log.SetLevel(log.DebugLevel)
case "error":
doLogging = true
log.SetLevel(log.ErrorLevel)
case "warn":
case "warning":
doLogging = true
log.SetLevel(log.WarnLevel)
case "panic":
doLogging = true
log.SetLevel(log.PanicLevel)
case "trace":
doLogging = true
log.SetLevel(log.TraceLevel)
}
default:
return ci, errors.Errorf("unknown H2 server connection parameters => \"%s\" : \"%s\"", k, val)
}
}
return ci, nil
}