/
pgsql.go
345 lines (311 loc) · 8.67 KB
/
pgsql.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2011 John E. Barham. 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 provides high-level access to the PostgreSQL libpq client access library.
package pgsql
/*
#include <stdlib.h>
#include <libpq-fe.h>
static char**makeCharArray(int size) {
return calloc(sizeof(char*), size);
}
static void setArrayString(char **a, char *s, int n) {
a[n] = s;
}
static void freeCharArray(char **a, int size) {
int i;
for (i = 0; i < size; i++)
free(a[i]);
free(a);
}
*/
import "C"
import (
"os"
"fmt"
"time"
"unsafe"
"reflect"
"strings"
"strconv"
"regexp"
"encoding/hex"
)
func connError(db *C.PGconn) os.Error {
return os.NewError("conn error:" + C.GoString(C.PQerrorMessage(db)))
}
func resultError(res *C.PGresult) os.Error {
serr := C.GoString(C.PQresultErrorMessage(res))
if serr == "" {
return nil
}
return os.NewError("result error: " + serr)
}
const timeFormat = "2006-01-02 15:04:05-07:00"
// PostgreSQL ISO timestamp format: YYYY-mm-dd HH:MM:SS[.SSS]±TZ[:TZ]
var tsRe = regexp.MustCompile("^([0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+)(\\.[0-9]+)([\\+\\-]?[0-9:]+)$")
// ParseTimestamp parses the given PostgreSQL timestamp string and upon success
// returns a time.Time and the fractional seconds in the timestamp, since the
// standard time.Time can only represent whole seconds.
func ParseTimestamp(ts string) (t *time.Time, fraction float64, err os.Error) {
// Extract seconds fraction.
m := tsRe.FindStringSubmatch(ts)
if m == nil {
return nil, 0, os.NewError("invalid timestamp")
}
if m[2] != "" {
fraction, _ = strconv.Atof64(m[2])
}
// Parse timestamp excluding seconds fraction.
t, err = time.Parse(timeFormat, m[1]+m[3])
return
}
type Result struct {
res *C.PGresult
nrows int
currRow int
ncols int
cols []string
}
func newResult(res *C.PGresult) *Result {
ncols := int(C.PQnfields(res))
nrows := int(C.PQntuples(res))
return &Result{res: res, nrows: nrows, currRow: -1, ncols: ncols, cols: nil}
}
// Names returns the list of column (field) names, in order, in the result.
func (r *Result) Names() []string {
if r.cols == nil {
r.cols = make([]string, r.ncols)
for i := 0; i < r.ncols; i++ {
r.cols[i] = C.GoString(C.PQfname(r.res, C.int(i)))
}
}
return r.cols
}
// Next returns true and increments the result row index if there are any
// remaining rows in the result; otherwise it returns false.
// Next should be called, and its value checked, before processing the first
// row of a result using Result.Scan.
func (r *Result) Next() bool {
if r.currRow+1 < r.nrows {
r.currRow++
return true
}
return false
}
func argErr(i int, argType string, err string) os.Error {
return os.NewError(fmt.Sprintf("arg %d as %s: %s", i, argType, err))
}
// Scan parses the values of the current result row (set using Result.Next)
// into the given arguments.
func (r *Result) Scan(args ...interface{}) os.Error {
if len(args) != r.ncols {
return os.NewError(fmt.Sprintf("incorrect argument count for Result.Scan: have %d want %d", len(args), r.ncols))
}
for i, v := range args {
if int(C.PQgetisnull(r.res, C.int(r.currRow), C.int(i))) == 1 {
continue
}
val := C.GoString(C.PQgetvalue(r.res, C.int(r.currRow), C.int(i)))
switch v := v.(type) {
case *[]byte:
if !strings.HasPrefix(val, "\\x") {
return argErr(i, "[]byte", "invalid byte string format")
}
buf, err := hex.DecodeString(val[2:])
if err != nil {
return argErr(i, "[]byte", err.String())
}
*v = buf
case *string:
*v = val
case *bool:
*v = val == "t"
case *int:
x, err := strconv.Atoi(val)
if err != nil {
return argErr(i, "int", err.String())
}
*v = x
case *int64:
x, err := strconv.Atoi64(val)
if err != nil {
return argErr(i, "int64", err.String())
}
*v = x
case *float32:
x, err := strconv.Atof32(val)
if err != nil {
return argErr(i, "float32", err.String())
}
*v = x
case *float64:
x, err := strconv.Atof64(val)
if err != nil {
return argErr(i, "float64", err.String())
}
*v = x
case *time.Time:
x, _, err := ParseTimestamp(val)
if err != nil {
return argErr(i, "time.Time", err.String())
}
*v = *x
default:
return os.NewError("unsupported type in Scan: " + reflect.Typeof(v).String())
}
}
return nil
}
// Clear frees the memory associated with the result. Cleared results should
// not be subsequently used.
func (r *Result) Clear() {
if r.res != nil {
C.PQclear(r.res)
r.res = nil
}
}
func buildCArgs(params ...interface{}) **C.char {
sparams := make([]string, len(params))
for i, v := range params {
var str string
switch v := v.(type) {
case []byte:
str = "\\x" + hex.EncodeToString(v)
case bool:
if v {
str = "t"
} else {
str = "f"
}
case *time.Time:
str = v.Format(timeFormat)
default:
str = fmt.Sprint(v)
}
sparams[i] = str
}
cparams := C.makeCharArray(C.int(len(sparams)))
for i, s := range sparams {
C.setArrayString(cparams, C.CString(s), C.int(i))
}
return cparams
}
type Conn struct {
db *C.PGconn
stmtNum int
}
// Connect creates a new database connection using the given connection string.
// Each parameter setting is in the form 'keyword=value'.
// See http://www.postgresql.org/docs/9.0/static/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS
// for a list of recognized parameters.
func Connect(params string) (conn *Conn, err os.Error) {
cparams := C.CString(params)
defer C.free(unsafe.Pointer(cparams))
db := C.PQconnectdb(cparams)
if C.PQstatus(db) != C.CONNECTION_OK {
err = connError(db)
C.PQfinish(db)
return nil, err
}
return &Conn{db, 0}, nil
}
func (c *Conn) exec(stmt string, params ...interface{}) (cres *C.PGresult) {
stmtstr := C.CString(stmt)
defer C.free(unsafe.Pointer(stmtstr))
if len(params) == 0 {
cres = C.PQexec(c.db, stmtstr)
} else {
cparams := buildCArgs(params...)
defer C.freeCharArray(cparams, C.int(len(params)))
cres = C.PQexecParams(c.db, stmtstr, C.int(len(params)), nil, cparams, nil, nil, 0)
}
return cres
}
// Exec executes the given SQL query with the given parameters.
func (c *Conn) Exec(cmd string, params ...interface{}) os.Error {
cres := c.exec(cmd, params...)
defer C.PQclear(cres)
return resultError(cres)
}
// Query executes the given SQL query with the given parameters, returning a
// Result on successful execution.
func (c *Conn) Query(query string, params ...interface{}) (res *Result, err os.Error) {
cres := c.exec(query, params...)
if err = resultError(cres); err != nil {
C.PQclear(cres)
return
}
return newResult(cres), nil
}
// Prepare creates and returns a prepared statement with the given SQL statement.
func (c *Conn) Prepare(stmt string) (*Stmt, os.Error) {
// Generate unique statement name.
stmtname := strconv.Itoa(c.stmtNum)
stmtnamestr := C.CString(stmtname)
c.stmtNum++
defer C.free(unsafe.Pointer(stmtnamestr))
stmtstr := C.CString(stmt)
defer C.free(unsafe.Pointer(stmtstr))
res := C.PQprepare(c.db, stmtnamestr, stmtstr, 0, nil)
err := resultError(res)
if err != nil {
C.PQclear(res)
return nil, err
}
return &Stmt{stmtname, c.db, res}, nil
}
// Reset closes the connection to the server and attempts to re-establish a new
// connection using the parameters passed in the original Connect call.
func (c *Conn) Reset() os.Error {
if c == nil || c.db == nil {
return os.NewError("nil postgresql connection")
}
C.PQreset(c.db)
if C.PQstatus(c.db) != C.CONNECTION_OK {
return connError(c.db)
}
return nil
}
// Close closes the database connection and frees its associated memory.
func (c *Conn) Close() {
if c != nil && c.db != nil {
C.PQfinish(c.db)
c.db = nil
}
}
type Stmt struct {
name string
db *C.PGconn
res *C.PGresult
}
func (s *Stmt) exec(params ...interface{}) *C.PGresult {
stmtName := C.CString(s.name)
defer C.free(unsafe.Pointer(stmtName))
cparams := buildCArgs(params...)
defer C.freeCharArray(cparams, C.int(len(params)))
return C.PQexecPrepared(s.db, stmtName, C.int(len(params)), cparams, nil, nil, 0)
}
// Exec executes the prepared statement with the given parameters.
func (s *Stmt) Exec(params ...interface{}) os.Error {
cres := s.exec(params...)
defer C.PQclear(cres)
return resultError(cres)
}
// Query executes the prepared statement with the given parameters, returning a
// Result on successful execution.
func (s *Stmt) Query(params ...interface{}) (res *Result, err os.Error) {
cres := s.exec(params...)
if err = resultError(cres); err != nil {
C.PQclear(cres)
return
}
return newResult(cres), nil
}
// Clear frees the memory associated with the statement. Cleared statements should
// not be subsequently used.
func (s *Stmt) Clear() {
if s != nil && s.res != nil {
C.PQclear(s.res)
}
}