-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexpand.go
144 lines (130 loc) · 3.12 KB
/
expand.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
package jet
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
var (
markRx = regexp.MustCompile(`(\$\d+|\?)`)
)
func substituteMapAndArrayMarks(query string, args ...interface{}) (string, []interface{}) {
loc := markRx.FindStringIndex(query)
if loc == nil {
return query, args
}
usesNumberedMarkers := false
markFormat := "?"
markPlaceholder := "?"
if query[loc[0]:loc[0]+1] == "$" {
usesNumberedMarkers = true
markFormat = "$%d"
markPlaceholder = "$0"
}
newArgs := make([]interface{}, 0, len(args)*2)
newParts := []string{}
queryParts := markRx.Split(query, -1)
for i, part := range queryParts {
newParts = append(newParts, part)
if i > len(args)-1 {
break
}
arg := args[i]
val := reflect.ValueOf(arg)
k := val.Kind()
if k == reflect.Map {
serializeMap(markPlaceholder, val, &newArgs, &newParts)
} else if k == reflect.Slice && val.Type() != reflect.TypeOf([]byte{}) {
serializeSlice(markPlaceholder, val, &newArgs, &newParts)
} else {
newParts = append(newParts, markPlaceholder)
newArgs = append(newArgs, arg)
}
}
return sanitizeMarkEnumeration(usesNumberedMarkers, markFormat, strings.Join(newParts, "")), newArgs
}
func serializeSlice(markPlaceholder string, v reflect.Value, newArgs *[]interface{}, newParts *[]string) {
a := make([]string, 0, v.Len())
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
a = append(a, markPlaceholder)
*newArgs = append(*newArgs, val.Interface())
}
*newParts = append(*newParts, strings.Join(a, ", "))
}
func serializeMap(markPlaceholder string, v reflect.Value, newArgs *[]interface{}, newParts *[]string) {
a := make([]interface{}, 0, v.Len()*2)
for _, keyVal := range v.MapKeys() {
val := v.MapIndex(keyVal)
a = append(a, keyVal.Interface(), val.Interface())
}
serializeSlice(markPlaceholder, reflect.ValueOf(a), newArgs, newParts)
}
func sanitizeMarkEnumeration(usesNumberedMarkers bool, markFormat, query string) string {
parts := markRx.Split(query, -1)
a := make([]string, 0, len(parts)*2)
for i, v := range parts {
a = append(a, v)
if i < len(parts)-1 {
if usesNumberedMarkers {
a = append(a, fmt.Sprintf(markFormat, i+1))
} else {
a = append(a, markFormat)
}
}
}
return strings.Join(a, "")
}
func parseHstoreColumn(s string) map[string]interface{} {
lasti := 0
quoteOpen := false
escaped := false
a := make([]string, 0, len(s))
for i, r := range s {
switch r {
case '\\':
escaped = true
case 'N':
if !quoteOpen && strings.HasPrefix(s[i:], "NULL") {
a = append(a, "NULL")
}
case '"':
if !escaped {
quoteOpen = !quoteOpen
if quoteOpen {
lasti = i
} else {
a = append(a, s[lasti:i+1])
}
}
escaped = false
default:
escaped = false
}
}
if len(a)%2 == 1 {
panic(fmt.Sprintf("invalid hstore map: %v", a))
}
// Convert to map
m := make(map[string]interface{}, len(a)/2)
lastKey := ""
uq := ""
isNull := false
for i, v := range a {
if v == "NULL" {
isNull = true
} else {
uq, _ = strconv.Unquote(v)
isNull = false
}
if i%2 == 0 {
lastKey = uq
} else if isNull {
m[lastKey] = nil
} else {
m[lastKey] = uq
}
}
return m
}