-
Notifications
You must be signed in to change notification settings - Fork 19
/
functions.go
248 lines (202 loc) · 9.96 KB
/
functions.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package postgis
import (
"fmt"
"strings"
entities "github.com/gost/core"
"github.com/gost/godata"
)
var funcToStringMap map[string]func(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string
// init is used to work around the initialization loop error (circular reference)
func init() {
funcToStringMap = map[string]func(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string{
"contains": containsToString,
"substringof": substringofToString,
"endswith": endswithToString,
"startswith": startswithToString,
"length": lengthToString,
"indexof": indexofToString,
"substring": substringToString,
"tolower": tolowerToString,
"toupper": toupperToString,
"trim": trimToString,
"concat": concatToString,
"round": roundToString,
"floor": floorToString,
"ceiling": ceilingToString,
"year": yearToString,
"month": monthToString,
"day": dayToString,
"hour": hourToString,
"minute": minuteToString,
"second": secondToString,
"fractionalseconds": fractionalsecondsToString,
"date": dateToString,
"time": timeToString,
"totaloffsetminutes": totaloffsetminutesToString,
"now": nowToString,
"maxdatetime": maxdatetimeToString,
"mindatetime": mindatetimeToString,
"totalseconds": totalsecondsToString,
"geo.length": geolengthToString,
"geo.distance": geodistanceToString,
"geo.intersects": stintersectsToString,
"st_equals": stequalsToString,
"st_touches": sttouchesToString,
"st_overlaps": stoverlapsToString,
"st_crosses": stcrossesToString,
"st_contains": stcontainsToString,
"st_disjoint": stdisjointToString,
"st_relate": strelateToString,
"st_within": stwithinToString,
"st_intersects": stintersectsToString,
}
}
func getLeftAndRightFilter(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType, ignoreLeftSelectAs, ignoreRightSelectAs bool) (string, string) {
left := qb.createFilter(et, pn.Children[0], ignoreLeftSelectAs)
right := qb.createFilter(et, pn.Children[1], ignoreRightSelectAs)
return left, right
}
func containsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
return fmt.Sprintf("%s LIKE %s", qb.createLike(left, LikeContains), qb.createLike(right, LikeContains))
}
func substringofToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
return fmt.Sprintf("%s LIKE %s", qb.createLike(right, LikeContains), qb.createLike(left, LikeContains))
}
func endswithToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
return fmt.Sprintf("%s LIKE %s", qb.createLike(left, LikeEndsWith), qb.createLike(right, LikeEndsWith))
}
func startswithToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
return fmt.Sprintf("%s LIKE %s", qb.createLike(left, LikeStartsWith), qb.createLike(right, LikeStartsWith))
}
func lengthToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("LENGTH(%s)", left)
}
func indexofToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
return fmt.Sprintf("STRPOS(%s, %s) -1", left, right)
}
func substringToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
right2 := ""
if len(pn.Children) > 2 {
right2 = qb.createFilter(et, pn.Children[2], true)
return fmt.Sprintf("SUBSTRING(%s from (%s + 1) for %s)", left, right, right2)
}
return fmt.Sprintf("SUBSTRING(%s from (%s + 1) for LENGTH(%s))", left, right, left)
}
func tolowerToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("LOWER(%s)", left)
}
func toupperToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("UPPER(%s)", left)
}
func trimToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("TRIM(both ' ' from %s)", left)
}
func concatToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left, right := getLeftAndRightFilter(qb, pn, et, true, true)
return fmt.Sprintf("CONCAT(%s, %s)", left, right)
}
func roundToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("ROUND(CAST(%s as double precision))", strings.Replace(left, "->", "->>", -1))
}
func floorToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("FLOOR(CAST(%s as double precision))", strings.Replace(left, "->", "->>", -1))
}
func ceilingToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("CEILING(CAST(%s as double precision))", strings.Replace(left, "->", "->>", -1))
}
func yearToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createExtractDateQuery(pn, et, "YEAR")
}
func monthToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createExtractDateQuery(pn, et, "MONTH")
}
func dayToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createExtractDateQuery(pn, et, "DAY")
}
func hourToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createExtractDateQuery(pn, et, "HOUR")
}
func minuteToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createExtractDateQuery(pn, et, "MINUTE")
}
func secondToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createExtractDateQuery(pn, et, "SECOND")
}
func fractionalsecondsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("EXTRACT(MICROSECONDS FROM to_timestamp(%s,'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"')) / 1000000", left)
}
func dateToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("(%s)::date", left)
}
func timeToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
if strings.Contains(strings.ToLower(left), "time") {
return fmt.Sprintf("((%s)::timestamp)::time", left)
}
return fmt.Sprintf("(%s)::time", left)
}
func totaloffsetminutesToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("EXTRACT(TIMEZONE_MINUTE FROM to_timestamp(%s,'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'))", left)
}
func nowToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return fmt.Sprint("to_char(now()::timestamp at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"')")
}
func maxdatetimeToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return fmt.Sprint("'9999-12-31T23:59:59.999Z'")
}
func mindatetimeToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return fmt.Sprint("'0001-01-01T00:00:00.000Z'")
}
func totalsecondsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
left := qb.createFilter(et, pn.Children[0], true)
return fmt.Sprintf("SELECT extract(epoch from (%s)::timestamp)", left)
}
func geodistanceToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_DISTANCE(%s, %s)", 2)
}
func geolengthToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_LENGTH(%s)", 1)
}
func stequalsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_EQUALS(%s, %s)", 2)
}
func sttouchesToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_TOUCHES(%s, %s)", 2)
}
func stoverlapsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_OVERLAPS(%s, %s)", 2)
}
func stcrossesToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_CROSSES(%s, %s)", 2)
}
func stcontainsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_CONTAINS(%s, %s)", 2)
}
func stdisjointToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_DISJOINT(%s, %s)", 2)
}
func strelateToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_RELATE(%s, %s, %s)", 3)
}
func stwithinToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_WITHIN(%s, %s)", 2)
}
func stintersectsToString(qb QueryBuilder, pn *godata.ParseNode, et entities.EntityType) string {
return qb.createSpatialQuery(pn, et, "ST_INTERSECTS(%s, %s)", 2)
}