-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdriver.go
More file actions
216 lines (183 loc) · 3.98 KB
/
driver.go
File metadata and controls
216 lines (183 loc) · 3.98 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
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
package intersystems
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"net"
"unicode"
_ "io"
_ "math"
_ "reflect"
_ "strconv"
_ "strings"
_ "time"
_ "unsafe"
"github.com/caretdev/go-irisnative/src/connection"
)
var (
ErrCouldNotDetectUsername = errors.New("intersystems: Could not detect default username. Please provide one explicitly")
)
var (
_ driver.Driver = Driver{}
)
type values map[string]string
// Driver implements database/sql/driver.Driver.
type Driver struct{}
func (d Driver) Open(name string) (driver.Conn, error) {
return Open(name)
}
func init() {
sql.Register("intersystems", &Driver{})
sql.Register("iris", &Driver{})
}
func Open(dsn string) (_ driver.Conn, err error) {
c, err := NewConnector(dsn)
if err != nil {
return nil, err
}
return c.open(context.Background())
}
type conn struct {
c connection.Connection
tx bool
}
func (c *Connector) open(ctx context.Context) (cn *conn, err error) {
o := make(values)
for k, v := range c.opts {
o[k] = v
}
host := o["host"]
addr := net.JoinHostPort(host, o["port"])
namespace := o["namespace"]
login := o["user"]
password := o["password"]
cn = &conn{}
cn.c, err = connection.Connect(addr, namespace, login, password)
if err != nil {
return nil, err
}
return cn, nil
}
func (cn *conn) Begin() (driver.Tx, error) {
return cn.c.BeginTx(driver.TxOptions{})
}
func (cn *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return cn.c.BeginTx(opts)
}
func (cn *conn) Close() (err error) {
cn.c.Disconnect()
return nil
}
func (cn *conn) Prepare(q string) (st driver.Stmt, err error) {
return cn.c.Prepare(q)
}
func (cn *conn) Commit() error {
if !cn.tx {
panic("transaction already closed")
}
cn.tx = false
cn.c.Commit()
return nil
}
func (cn *conn) Rollback() error {
if !cn.tx {
panic("transaction already closed")
}
cn.tx = false
cn.c.Rollback()
return nil
}
func (cn *conn) Exec(query string, args []driver.NamedValue) (res driver.Result, err error) {
parameters := make([]interface{}, len(args))
for i, a := range args {
parameters[i] = a
}
_, err = cn.c.DirectUpdate(query, parameters...)
if err != nil {
return nil, err
}
return res, nil
}
func (cn *conn) Query(query string, args []driver.NamedValue) (rows driver.Rows, err error) {
parameters := make([]interface{}, len(args))
for i, a := range args {
parameters[i] = a
}
// var rs *connection.ResultSet
_, err = cn.c.Query(query, parameters...)
if err != nil {
return nil, err
}
// rows = &connection.Rows{
// cn: cn.c,
// rs: rs,
// }
return
}
func parseOpts(name string, o values) error {
s := newScanner(name)
for {
var (
keyRunes, valRunes []rune
r rune
ok bool
)
if r, ok = s.SkipSpaces(); !ok {
break
}
// Scan the key
for !unicode.IsSpace(r) && r != '=' {
keyRunes = append(keyRunes, r)
if r, ok = s.Next(); !ok {
break
}
}
// Skip any whitespace if we're not at the = yet
if r != '=' {
r, ok = s.SkipSpaces()
}
// The current character should be =
if r != '=' || !ok {
return fmt.Errorf(`missing "=" after %q in connection info string"`, string(keyRunes))
}
// Skip any whitespace after the =
if r, ok = s.SkipSpaces(); !ok {
// If we reach the end here, the last value is just an empty string as per libpq.
o[string(keyRunes)] = ""
break
}
if r != '\'' {
for !unicode.IsSpace(r) {
if r == '\\' {
if r, ok = s.Next(); !ok {
return fmt.Errorf(`missing character after backslash`)
}
}
valRunes = append(valRunes, r)
if r, ok = s.Next(); !ok {
break
}
}
} else {
quote:
for {
if r, ok = s.Next(); !ok {
return fmt.Errorf(`unterminated quoted string literal in connection string`)
}
switch r {
case '\'':
break quote
case '\\':
r, _ = s.Next()
fallthrough
default:
valRunes = append(valRunes, r)
}
}
}
o[string(keyRunes)] = string(valRunes)
}
return nil
}