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 pathwhere.go
156 lines (138 loc) · 4.05 KB
/
where.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
package dat
import (
"reflect"
"regexp"
"strconv"
"strings"
"github.com/syreclabs/dat/common"
)
// Eq is a map column -> value pairs which must be matched in a query
type Eq map[string]interface{}
type whereFragment struct {
Condition string
Values []interface{}
EqualityMap map[string]interface{}
}
func newWhereFragment(whereSqlOrMap interface{}, args []interface{}) *whereFragment {
switch pred := whereSqlOrMap.(type) {
case Expression:
return &whereFragment{Condition: pred.Sql, Values: pred.Args}
case *Expression:
return &whereFragment{Condition: pred.Sql, Values: pred.Args}
case string:
return &whereFragment{Condition: pred, Values: args}
case map[string]interface{}:
return &whereFragment{EqualityMap: pred}
case Eq:
return &whereFragment{EqualityMap: map[string]interface{}(pred)}
default:
panic("Invalid argument passed to Where. Pass a string or an Eq map.")
}
}
var rePlaceholder = regexp.MustCompile(`\$\d+`)
func remapPlaceholders(buf common.BufferWriter, statement string, start int64) int64 {
if !strings.Contains(statement, "$") {
buf.WriteString(statement)
return 0
}
highest := 0
pos := int(start) - 1 // 0-based
statement = rePlaceholder.ReplaceAllStringFunc(statement, func(s string) string {
i, _ := strconv.Atoi(s[1:])
if i > highest {
highest = i
}
sum := strconv.Itoa(pos + i)
return "$" + sum
})
buf.WriteString(statement)
return int64(highest)
}
// Invariant: for scope conditions only
func writeScopeCondition(buf common.BufferWriter, f *whereFragment, args *[]interface{}, pos *int64) {
buf.WriteRune(' ')
if len(f.Values) > 0 {
// map relative $1, $2 placeholders to absolute
replaced := remapPlaceholders(buf, f.Condition, *pos)
*pos += replaced
*args = append(*args, f.Values...)
} else {
buf.WriteString(f.Condition)
}
}
// Invariant: only called when len(fragments) > 0
func writeWhereFragmentsToSql(buf common.BufferWriter, fragments []*whereFragment, args *[]interface{}, pos *int64) {
hasConditions := false
for _, f := range fragments {
if f.Condition != "" {
if hasConditions {
buf.WriteString(" AND (")
} else {
buf.WriteRune('(')
hasConditions = true
}
if len(f.Values) > 0 {
// map relative $1, $2 placeholders to absolute
replaced := remapPlaceholders(buf, f.Condition, *pos)
*pos += replaced
*args = append(*args, f.Values...)
} else {
buf.WriteString(f.Condition)
}
buf.WriteRune(')')
} else if f.EqualityMap != nil {
hasConditions = writeEqualityMapToSql(buf, f.EqualityMap, args, hasConditions, pos)
} else {
panic("invalid equality map")
}
}
}
func writeEqualityMapToSql(buf common.BufferWriter, eq map[string]interface{}, args *[]interface{}, anyConditions bool, pos *int64) bool {
for k, v := range eq {
if v == nil {
anyConditions = writeWhereCondition(buf, k, " IS NULL", anyConditions)
} 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
}
func writeWhereCondition(buf common.BufferWriter, k string, pred string, anyConditions bool) bool {
if anyConditions {
buf.WriteString(" AND (")
} else {
buf.WriteRune('(')
anyConditions = true
}
Dialect.WriteIdentifier(buf, k)
buf.WriteString(pred)
buf.WriteRune(')')
return anyConditions
}