This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_helpers.go
182 lines (161 loc) · 4.52 KB
/
list_helpers.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
package dat
import (
"bytes"
"reflect"
"strconv"
"github.com/syreclabs/dat/common"
)
var bufPool = common.NewBufferPool()
// listIdentifiers returns a CSV with quoted column identifiers
//
// return "col1", "col2", ... "coln"
func joinIdentifiers(columns []string, join string) string {
buf := bufPool.Get()
defer bufPool.Put(buf)
for i, column := range columns {
if i > 0 {
buf.WriteString(join)
}
Dialect.WriteIdentifier(buf, column)
}
return buf.String()
}
func writeIdentifiers(buf *bytes.Buffer, columns []string, join string) {
for i, column := range columns {
if i > 0 {
buf.WriteString(join)
}
Dialect.WriteIdentifier(buf, column)
}
}
func writeIdentifier(buf *bytes.Buffer, name string) {
Dialect.WriteIdentifier(buf, name)
}
// joinKVPlaceholders returns "col1" = $1 and "col2" = $2 ... and "coln" = $n
func joinKVPlaceholders(columns []string, join string, offset int) string {
buf := bufPool.Get()
defer bufPool.Put(buf)
for i, column := range columns {
if i > 0 {
buf.WriteString(join)
}
Dialect.WriteIdentifier(buf, column)
buf.WriteRune('=')
writePlaceholder(buf, i+offset)
}
return buf.String()
}
func buildPlaceholders(buf *bytes.Buffer, start, length int) {
// Build the placeholder like "($1,$2,$3)"
buf.WriteRune('(')
for i := start; i < start+length; i++ {
if i > start {
buf.WriteRune(',')
}
writePlaceholder(buf, i)
}
buf.WriteRune(')')
}
// joinPlaceholders returns $1, $2 ... , $n
func joinPlaceholders(length int, join string, offset int) string {
buf := bufPool.Get()
defer bufPool.Put(buf)
for i := 0; i < length; i++ {
if i > 0 {
buf.WriteString(join)
}
writePlaceholder(buf, i+offset)
}
return buf.String()
}
// joinPlaceholders returns $1, $2 ... , $n
func writePlaceholders(buf *bytes.Buffer, length int, join string, offset int) {
for i := 0; i < length; i++ {
if i > 0 {
buf.WriteString(join)
}
writePlaceholder(buf, i+offset)
}
}
// writeKVAssign wrties "col1" = $1, "col2" = $2, ... "coln" = $n
func writeKVAssign(buf *bytes.Buffer, columns []string, values []interface{}, args *[]interface{}) {
var placeholderStartPos int64 = 1
// Build SET clause SQL with placeholders and add values to args
for i, column := range columns {
if i > 0 {
buf.WriteString(", ")
}
value := values[i]
writeIdentifier(buf, column)
if e, ok := value.(*Expression); ok {
start := placeholderStartPos
buf.WriteString(" = ")
// map relative $1, $2 placeholders to absolute
remapPlaceholders(buf, e.Sql, start)
*args = append(*args, e.Args...)
placeholderStartPos += int64(len(e.Args))
} else {
if i < maxLookup {
buf.WriteString(equalsPlaceholderTab[placeholderStartPos])
} else {
if placeholderStartPos < maxLookup {
buf.WriteString(equalsPlaceholderTab[placeholderStartPos])
} else {
buf.WriteString(" = $")
buf.WriteString(strconv.FormatInt(placeholderStartPos, 10))
}
}
placeholderStartPos++
*args = append(*args, value)
}
}
}
// writeKVWhere writes "col1" = $1 AND "col2" = $2, ... "coln" = $n
func writeKVWhere(buf common.BufferWriter, columns []string, values []interface{}, args *[]interface{}, anyConditions bool, pos *int64) bool {
if len(columns) != len(values) {
panic("Mismatch of column and values")
}
for i, k := range columns {
v := values[i]
if v == nil {
anyConditions = writeWhereCondition(buf, k, " IS NULL", anyConditions)
} else if e, ok := v.(*Expression); ok {
start := pos
buf.WriteString(" = ")
// map relative $1, $2 placeholders to absolute
remapPlaceholders(buf, e.Sql, *start)
*args = append(*args, e.Args...)
*pos += int64(len(e.Args))
} else {
vVal := reflect.ValueOf(v)
if vVal.Kind() == reflect.Array || vVal.Kind() == reflect.Slice {
vValLen := vVal.Len()
if vValLen == 0 {
if vVal.IsNil() {
anyConditions = writeWhereCondition(buf, k, " IS NULL", anyConditions)
} else {
if anyConditions {
buf.WriteString(" AND (1=0)")
} else {
buf.WriteString("(1=0)")
}
}
} else if vValLen == 1 {
anyConditions = writeWhereCondition(buf, k, equalsPlaceholderTab[*pos], anyConditions)
*args = append(*args, vVal.Index(0).Interface())
*pos++
} else {
// " IN $n"
anyConditions = writeWhereCondition(buf, k, inPlaceholderTab[*pos], anyConditions)
*args = append(*args, v)
*pos++
}
} else {
anyConditions = writeWhereCondition(buf, k, equalsPlaceholderTab[*pos], anyConditions)
*args = append(*args, v)
*pos++
}
}
}
return anyConditions
}