forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.go
143 lines (130 loc) · 3.17 KB
/
bind.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
package sqlx
import (
"bytes"
"errors"
"strconv"
"unicode"
)
const (
UNKNOWN = iota
QUESTION
DOLLAR
)
// Return the bindtype for a given database given a drivername
func BindType(driverName string) int {
switch driverName {
case "postgres":
return DOLLAR
case "mysql":
return QUESTION
case "sqlite":
return QUESTION
}
return UNKNOWN
}
// Rebind a query from the default bindtype (QUESTION) to the target bindtype
func Rebind(bindType int, query string) string {
if bindType != DOLLAR {
return query
}
qb := []byte(query)
// Add space enough for 10 params before we have to allocate
rqb := make([]byte, 0, len(qb)+10)
j := 1
for _, b := range qb {
if b == '?' {
rqb = append(rqb, '$')
for _, b := range strconv.Itoa(j) {
rqb = append(rqb, byte(b))
}
j++
} else {
rqb = append(rqb, b)
}
}
return string(rqb)
}
// Bind a named parameter query with a map of arguments to a regular positional
// bindvar query and return arguments for the new query in a slice.
func BindMap(bindType int, query string, args map[string]interface{}) (string, []interface{}, error) {
arglist := make([]interface{}, 0, 5)
// In all likelihood, the rebound query will be shorter
qb := []byte(query)
rebound := make([]byte, 0, len(qb))
var name []byte
var sname string
var val interface{}
var ok, inName bool
var err error
var last, j int
inName = false
last = len(qb) - 1
j = 1
for i, b := range qb {
if b == ':' {
if inName {
err = errors.New("Unexpected `:` while reading named param at " + strconv.Itoa(i))
return "", arglist, err
}
inName = true
name = []byte{}
} else if inName && unicode.IsLetter(rune(b)) && i != last {
// append the rune to the name if we are in a name and not on the last rune
name = append(name, b)
} else if inName {
inName = false
// if this is the final rune of the string and it is part of the name, then
// make sure to add it to the name
if i == last && unicode.IsLetter(rune(b)) {
name = append(name, b)
}
sname = string(name)
val, ok = args[sname]
if !ok {
err = errors.New("Could not find name `" + sname + "` in args")
return "", arglist, err
}
// the name has been found and is complete, add to arglist and insert the
// proper bindvar for the bindType
arglist = append(arglist, val)
switch bindType {
case QUESTION, UNKNOWN:
rebound = append(rebound, '?')
case DOLLAR:
rebound = append(rebound, '$')
for _, b := range strconv.Itoa(j) {
rebound = append(rebound, byte(b))
}
j++
}
// add this rune to string unless if it is not last or if it
// is last but is not a letter
if i != last {
rebound = append(rebound, b)
} else if !unicode.IsLetter(rune(b)) {
rebound = append(rebound, b)
}
} else {
rebound = append(rebound, b)
}
}
return string(rebound), arglist, nil
}
func rebindBuff(bindType int, query string) string {
if bindType != DOLLAR {
return query
}
b := make([]byte, 0, len(query))
rqb := bytes.NewBuffer(b)
j := 1
for _, r := range query {
if r == '?' {
rqb.WriteRune('$')
rqb.WriteString(strconv.Itoa(j))
j++
} else {
rqb.WriteRune(r)
}
}
return rqb.String()
}