This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
forked from jraede/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressions.go
187 lines (148 loc) · 3.91 KB
/
expressions.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
183
184
185
186
187
package sqlbuilder
import (
"fmt"
"reflect"
"strings"
)
// "="" expression. "field = value"
type Equal struct {
Field string
Value interface{}
}
func GetSQLFromInterface(cache *VarCache, i interface{}) string {
if provider, ok := i.(SQLProvider); ok {
return "(" + provider.GetSQL(cache) + ")"
} else {
return cache.add(i)
}
}
func (e Equal) GetSQL(cache *VarCache) string {
return e.Field + " = " + GetSQLFromInterface(cache, e.Value)
}
// "!=" expression. "field != value"
type NotEqual struct {
Field string
Value interface{}
}
func (e NotEqual) GetSQL(cache *VarCache) string {
return e.Field + " != " + GetSQLFromInterface(cache, e.Value)
}
// ">" expression. "field > value"
type GreaterThan struct {
Field string
Value interface{}
}
func (e GreaterThan) GetSQL(cache *VarCache) string {
return e.Field + " > " + GetSQLFromInterface(cache, e.Value)
}
// "<" expression. "field < value"
type LessThan struct {
Field string
Value interface{}
}
func (e LessThan) GetSQL(cache *VarCache) string {
return e.Field + " < " + GetSQLFromInterface(cache, e.Value)
}
// ">=" expression. "field >= value"
type GreaterOrEqual struct {
Field string
Value interface{}
}
func (e GreaterOrEqual) GetSQL(cache *VarCache) string {
return e.Field + " >= " + GetSQLFromInterface(cache, e.Value)
}
// "<=" expression. "field <= value"
type LessOrEqual struct {
Field string
Value interface{}
}
func (e LessOrEqual) GetSQL(cache *VarCache) string {
return e.Field + " <= " + GetSQLFromInterface(cache, e.Value)
}
func getInKeys(val interface{}, cache *VarCache) string {
if provider, ok := val.(SQLProvider); ok {
return provider.GetSQL(cache)
}
// Make sure value is a slice
t := reflect.TypeOf(val)
v := reflect.ValueOf(val)
for t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
if t.Kind() != reflect.Slice {
panic(`Value for "In" expression must be a slice`)
}
keys := make([]string, v.Len())
for i := 0; i < v.Len(); i++ {
keys[i] = cache.add(v.Index(i).Interface())
}
return strings.Join(keys, ", ")
}
// "IN" expression. "field IN (values...)". Values must be a slice or a subquery, otherwise this function will panic.
type In struct {
Field string
Value interface{}
}
func (e In) GetSQL(cache *VarCache) string {
return e.Field + " IN (" + getInKeys(e.Value, cache) + ")"
}
// "NOT IN" expression. "field NOT IN (values...)". Values must be a slice or a subquery, otherwise this function will panic.
type NotIn struct {
Field string
Value interface{}
}
func (e NotIn) GetSQL(cache *VarCache) string {
return e.Field + " NOT IN (" + getInKeys(e.Value, cache) + ")"
}
// "LIKE" expression. "field LIKE value"
type Like struct {
Field string
Value interface{}
}
func (e Like) GetSQL(cache *VarCache) string {
return e.Field + " LIKE " + cache.add(e.Value)
}
// "IS NULL" expression. "field IS NULL"
type IsNull struct {
Field string
}
func (e IsNull) GetSQL(cache *VarCache) string {
return e.Field + " IS NULL"
}
// "IS NOT NULL" expression. "field IS NOT NULL"
type IsNotNull struct {
Field string
}
func (e IsNotNull) GetSQL(cache *VarCache) string {
return e.Field + " IS NOT NULL"
}
// Raw expression. BE CAREFUL!!! THERE IS NO SQL INJECTION PROTECTION HERE. This is used internally for joining on columns. Use at your own risk
type Raw struct {
Expr string
}
func (e Raw) GetSQL(cache *VarCache) string {
return e.Expr
}
type PGOverlap struct {
Field string
Value []string
}
func (e PGOverlap) GetSQL(cache *VarCache) string {
parsed := make([]string, len(e.Value))
for idx, item := range e.Value {
parsed[idx] = cache.add(item)
}
return fmt.Sprintf("%s && ARRAY[%s]::varchar[]", e.Field, strings.Join(parsed, ","))
}
type Expression struct {
Format string
Values []interface{}
}
func (e Expression) GetSQL(cache *VarCache) string {
parsed := make([]interface{}, len(e.Values))
for idx, item := range e.Values {
parsed[idx] = cache.add(item)
}
return fmt.Sprintf(e.Format, parsed...)
}