-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcargs.go
More file actions
120 lines (104 loc) · 2.33 KB
/
cargs.go
File metadata and controls
120 lines (104 loc) · 2.33 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
package libpq
/*
#include <stdlib.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 freeArrayElements(int n, char **a) {
int i;
for (i = 0; i < n; i++) {
free(a[i]);
a[i] = NULL;
}
}
*/
import "C"
import (
"database/sql/driver"
"encoding/hex"
"errors"
"strconv"
"time"
)
const timeFormat = time.RFC3339Nano
// wrapper for a request for a char** of length nargs
type pqPoolRequest struct {
nargs int
resp chan (**C.char)
}
// return a char** of length nargs to the pool
type pqPoolReturn struct {
nargs int
array **C.char
}
var (
argpool map[int][]**C.char
poolRequest chan pqPoolRequest
poolReturn chan pqPoolReturn
)
// convert database/sql/driver arguments into libpq-style char**
func buildCArgs(args []driver.Value) (**C.char, error) {
carray := getCharArrayFromPool(len(args))
for i, v := range args {
var str string
switch v := v.(type) {
case int64:
str = strconv.FormatInt(v, 10)
case float64:
str = strconv.FormatFloat(v, 'E', -1, 64)
case bool:
if v {
str = "t"
} else {
str = "f"
}
case []byte:
str = `\x` + hex.EncodeToString(v)
case string:
str = v
case time.Time:
str = v.Format(timeFormat)
case nil:
str = "NULL"
default:
returnCharArrayToPool(len(args), carray)
return nil, errors.New("libpq: unsupported type")
}
C.setArrayString(carray, C.CString(str), C.int(i))
}
return carray, nil
}
func getCharArrayFromPool(nargs int) **C.char {
ch := make(chan **C.char)
req := pqPoolRequest{nargs, ch}
poolRequest <- req
return <-ch
}
func returnCharArrayToPool(nargs int, array **C.char) {
C.freeArrayElements(C.int(nargs), array)
poolReturn <- pqPoolReturn{nargs, array}
}
// this is started in a goroutine in libpq's init() to reduce the number
// of calls to makeCharArray()
func handleArgpool() {
argpool = make(map[int][]**C.char)
poolRequest = make(chan pqPoolRequest)
poolReturn = make(chan pqPoolReturn)
for {
select {
case req := <-poolReturn:
list := append(argpool[req.nargs], req.array)
argpool[req.nargs] = list
case req := <-poolRequest:
list := argpool[req.nargs]
if len(list) == 0 {
list = append(list, C.makeCharArray(C.int(req.nargs)))
}
req.resp <- list[0]
argpool[req.nargs] = list[1:]
}
}
}