-
Notifications
You must be signed in to change notification settings - Fork 22
/
driver.go
221 lines (164 loc) · 3.35 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
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
217
218
219
220
221
// Copyright 2012 The go-pgsql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgsql
import (
"database/sql"
"database/sql/driver"
"fmt"
"io"
"time"
)
func init() {
sql.Register("postgres", sqlDriver{})
}
type sqlDriver struct {
}
func (sqlDriver) Open(name string) (driver.Conn, error) {
conn, err := Connect(name, LogNothing)
if err != nil {
return nil, err
}
return &sqlConn{conn}, nil
}
type sqlConn struct {
conn *Conn
}
func (c *sqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
n, err := c.conn.Execute(query, paramsFromValues(nil, args)...)
if err != nil {
return nil, err
}
return driver.RowsAffected(n), nil
}
func (c *sqlConn) Prepare(query string) (driver.Stmt, error) {
stmt, err := c.conn.Prepare(query)
if err != nil {
return nil, err
}
return &sqlStmt{stmt}, nil
}
func (c *sqlConn) Close() error {
return c.conn.Close()
}
func (c *sqlConn) Begin() (driver.Tx, error) {
if _, err := c.conn.Execute("BEGIN;"); err != nil {
return nil, err
}
return &sqlTx{c.conn}, nil
}
type sqlStmt struct {
stmt *Statement
}
func (s *sqlStmt) Close() error {
return s.stmt.Close()
}
func (s *sqlStmt) NumInput() int {
return -1
}
func (s *sqlStmt) Exec(args []driver.Value) (driver.Result, error) {
s.stmt.params = paramsFromValues(s.stmt.params, args)
n, err := s.stmt.Execute()
if err != nil {
return nil, err
}
return driver.RowsAffected(n), nil
}
func (s *sqlStmt) Query(args []driver.Value) (driver.Rows, error) {
s.stmt.params = paramsFromValues(s.stmt.params, args)
rs, err := s.stmt.Query()
if err != nil {
return nil, err
}
return &sqlRows{rs}, nil
}
type sqlTx struct {
conn *Conn
}
func (t *sqlTx) Commit() error {
_, err := t.conn.Execute("COMMIT;")
return err
}
func (t *sqlTx) Rollback() error {
_, err := t.conn.Execute("ROLLBACK;")
return err
}
type sqlRows struct {
rs *ResultSet
}
func (r *sqlRows) Columns() []string {
names := make([]string, len(r.rs.fields))
for i, f := range r.rs.fields {
names[i] = f.name
}
return names
}
func (r *sqlRows) Close() error {
return r.rs.Close()
}
func (r *sqlRows) Next(dest []driver.Value) error {
fetched, err := r.rs.FetchNext()
if err != nil {
return err
}
if !fetched {
return io.EOF
}
for i := range dest {
val, isNull, err := r.rs.Any(i)
if err != nil {
return err
}
if isNull {
val = nil
} else {
switch v := val.(type) {
case float32:
val = float64(v)
case int:
val = int64(v)
case int8:
val = int64(v)
case int16:
val = int64(v)
case int32:
val = int64(v)
case string:
val = ([]byte)(v)
}
}
dest[i] = val
}
return nil
}
func paramsFromValues(params []*Parameter, vals []driver.Value) []*Parameter {
if len(params) < len(vals) {
params = make([]*Parameter, len(vals))
}
for i, val := range vals {
p := params[i]
if p == nil {
var typ Type
switch val.(type) {
case nil:
typ = Integer
case bool:
typ = Boolean
case []byte, string:
typ = Varchar
case float64:
typ = Double
case int64:
typ = Bigint
case time.Time:
typ = TimestampTZ
default:
panic("unexpected value type")
}
p = NewParameter(fmt.Sprintf("$%d", i), typ)
params[i] = p
}
p.SetValue(val)
}
return params
}