-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcqlConverter.go
524 lines (486 loc) · 14 KB
/
cqlConverter.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package main
import (
"bytes"
"errors"
"regexp"
"strconv"
"strings"
)
/**
* A cql converter that converts CQL query to SQL query
* Baishan 15/4/2016
*/
const (
DATE_PATTERN = "(\\d{4})-(\\d{1,2})-(\\d{1,2})"
DATE_TIME_PATTERN = "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})"
//token types
TT_EOF = -1
TT_WORD = -3
TT_STRING = 999 // quoted string
TT_SORTBY = 1008 // The "sortby" operator
TT_PARENTHESIS = 1100 //()
TT_LOGIC_OPS = 1101 //and or
TT_RELATION_OPS = 1102 //>, <, =, >=, <=, !=, ==
TT_TIMESTRING = 1103 //2016-4-12T22:00:00
TT_BBOX = 1104 //BBOX(origin_geom,174,-41,175,-42)
TT_WITHIN = 1105 //WITHIN(origin_geom,POLYGON((172.951 -41.767, 172.001 -42.832, 169.564 -44.341,+172.312 -45.412, 175.748 -42.908, 172.951 -41.767)))
TT_DWITHIN = 1106 //DWITHIN(origin_geom,Point+(175+-41),0.5,feet)
)
type CqlConverter struct {
cqlCharArray []rune //char array of the CQL string
cqlLen int //length of the CQL string
currentIndex int //current index in the cqlCharArray
currentTokenType int //current token type in the CQL string
currentToken string //current token in the CQL
BBOX string
}
//search string contains another string
func SearchStringInString(s string, ch string) bool {
return (strings.Index(s, ch)) >= 0
}
/**
* convenient pattern match (treat error as false)
*/
func PatternMatch(pattern string, str string) bool {
match, err := regexp.MatchString(pattern, str)
if err != nil {
return false
}
return match
}
// NewCqlConverterL returns a pointer to a CqlConverter struct.
func NewCqlConverter(cql string) *CqlConverter {
cqlConverter := CqlConverter{}
cqlConverter.cqlCharArray = []rune(cql)
cqlConverter.cqlLen = len(cqlConverter.cqlCharArray)
return &cqlConverter
}
func (cql *CqlConverter) Render() string {
var ret string
switch cql.currentTokenType {
case TT_BBOX:
ret = "bboc"
case TT_WITHIN:
ret = "within"
case TT_LOGIC_OPS:
ret = "logic"
case TT_PARENTHESIS:
ret = "paranthesis"
case TT_RELATION_OPS:
ret = "relation"
case TT_DWITHIN:
ret = "dwithin"
case TT_TIMESTRING:
ret = "time"
case TT_WORD:
ret = "word"
case TT_STRING:
ret = "string"
case TT_EOF:
ret = "END"
default:
ret = ""
}
ret += ">>" + cql.currentToken
return ret
}
/**
* move to next token, update token and type
* token separators: "()/<>= \t\r\n"
*/
func (cql *CqlConverter) NextToken() {
//eat whitespace
for cql.currentIndex < cql.cqlLen && SearchStringInString(" \t\r\n", string(cql.cqlCharArray[cql.currentIndex])) {
cql.currentIndex++
}
//eof
if cql.currentIndex == cql.cqlLen {
cql.currentTokenType = TT_EOF
cql.currentToken = ""
return
}
//get current char
c := string(cql.cqlCharArray[cql.currentIndex])
var buf bytes.Buffer
var lval string
//1. separators
if SearchStringInString("()", c) {
cql.currentTokenType = TT_PARENTHESIS
cql.currentToken = c
cql.currentIndex++
//2. comparitor
} else if SearchStringInString("<>=!", c) {
cql.currentIndex++
//two-char comparitor
twoCharOps := false
//log.Println("2. relation c " + c)
if cql.currentIndex < cql.cqlLen {
d := string(cql.cqlCharArray[cql.currentIndex])
//log.Println("2. relation d " + d)
if SearchStringInString("<>=", d) {
cql.currentToken = c + d
cql.currentTokenType = TT_RELATION_OPS
twoCharOps = true
cql.currentIndex++
}
}
if !twoCharOps { //single char ops
if SearchStringInString("<>=", c) {
cql.currentToken = c
cql.currentTokenType = TT_RELATION_OPS
} else { //only the ! char
cql.currentIndex--
}
}
//3. quoted string
} else if SearchStringInString("\"", c) { //no single-quotes
cql.currentTokenType = TT_STRING
//remember quote char
mark := c
cql.currentIndex++
escaped := false
buf.Reset() //reset buffer
for cql.currentIndex < cql.cqlLen {
if !escaped && (string(cql.cqlCharArray[cql.currentIndex]) == mark) { //terminator
break
}
if escaped && SearchStringInString("*?^\\", string(cql.cqlCharArray[cql.currentIndex])) { //no escaping for d-quote
buf.WriteString("\\")
}
if !escaped && string(cql.cqlCharArray[cql.currentIndex]) == "\\" { //escape-char
escaped = true
cql.currentIndex++
continue
}
escaped = false //reset escape
buf.WriteString(string(cql.cqlCharArray[cql.currentIndex]))
cql.currentIndex++
}
cql.currentToken = buf.String()
lval = strings.ToLower(cql.currentToken)
if cql.currentIndex < cql.cqlLen {
cql.currentIndex++
} else { //unterminated
cql.currentTokenType = TT_EOF //notify error
}
} else { //4. WORD, including: logic ops, bbox, within, dwithin, timestring
cql.currentTokenType = TT_WORD
buf.Reset() //reset buffer
for cql.currentIndex < cql.cqlLen && !SearchStringInString("()/<>= \t\r\n", string(cql.cqlCharArray[cql.currentIndex])) {
buf.WriteString(string(cql.cqlCharArray[cql.currentIndex]))
cql.currentIndex++
}
cql.currentToken = buf.String()
lval = strings.ToLower(cql.currentToken)
//strip leading and tailing single quotes
stripVal := strings.TrimLeft(cql.currentToken, "'")
stripVal = strings.TrimRight(stripVal, "'")
if lval == "or" || lval == "and" || lval == "not" {
cql.currentTokenType = TT_LOGIC_OPS
} else if lval == "bbox" {
cql.currentTokenType = TT_BBOX
} else if lval == "within" {
cql.currentTokenType = TT_WITHIN
} else if lval == "dwithin" {
cql.currentTokenType = TT_DWITHIN
} else if lval == "sortby" {
cql.currentTokenType = TT_SORTBY
} else if PatternMatch(DATE_PATTERN, stripVal) || PatternMatch(DATE_TIME_PATTERN, stripVal) {
cql.currentTokenType = TT_TIMESTRING
}
}
}
/**
*sql:
* BBOX(origin_geom,174,-41,175,-42)
sql:
ST_Contains(ST_SetSRID(ST_Envelope('LINESTRING(174 -41,175 -42)'::geometry),4326), origin_geom)
* @param sql
*/
func (cql *CqlConverter) ToBBoxSql(sql *bytes.Buffer) error {
token := cql.currentToken //bbox
//log.Println("token1 " + token)
cql.NextToken()
if cql.currentTokenType != TT_PARENTHESIS {
//log.Fatalln("Error, not valid bbox!!")
return errors.New("not valid bbox!!")
}
tokens := make([]string, 0)
for ")" != cql.currentToken { //move until the close parenthesis
cql.NextToken()
token = cql.currentToken // origin_geom,174,-41,175,-42
//log.Println("token2 " + token)
if ")" != token {
for _, value := range strings.Split(token, ",") {
if value != "" {
tokens = append(tokens, value)
}
}
}
}
//log.Println("## tokens", len(tokens))
if cql.currentTokenType != TT_PARENTHESIS {
//log.Fatal("Error, not valid bbox!!")
return errors.New("not valid bbox!!")
}
//String[] bboxContents = token3.split("[, ]+");
if len(tokens) == 5 {
sql.WriteString("ST_Contains(ST_SetSRID(ST_Envelope('LINESTRING(")
sql.WriteString(tokens[1])
sql.WriteString(" ")
sql.WriteString(tokens[2])
sql.WriteString(",")
sql.WriteString(tokens[3])
sql.WriteString(" ")
sql.WriteString(tokens[4])
sql.WriteString(")'::geometry),4326),")
sql.WriteString(tokens[0])
sql.WriteString(")")
//keep the bbox string for later user
cql.BBOX = tokens[1] + "," + tokens[2] + "," + tokens[3] + "," + tokens[4]
}
return nil
}
/**
* get comma separated tokens between parenthesis
* (origin_geom,POLYGON((172.951 -41.767,172.001 -42.832,169.564 -44.341,172.312 -45.412,175.748 -42.908,172.951 -41.767)))
* (origin_geom,Point(175 -41),500,meters)
*/
func (cql *CqlConverter) GetParenthesisedTokens() []string {
//get all tokens for the WITHIN claus, parse all comma separated tokens
var token string
openParenthesis := 1 //already at open parenthesis
closeParenthesis := 0
tokens := make([]string, 0)
for true {
cql.NextToken()
token = cql.currentToken // origin_geom,174,-41,175,-42
//log.Println("token2 " + token)
for _, value := range strings.Split(token, ",") {
if value != "" {
tokens = append(tokens, value)
}
}
if "(" == token {
openParenthesis++
}
if ")" == token {
closeParenthesis++
}
//fmt.Printf("openParenthesis %d closeParenthesis %d", openParenthesis, closeParenthesis)
if closeParenthesis > 0 && openParenthesis == closeParenthesis {
break
}
}
return tokens
}
/**
* cql:
WITHIN(origin_geom,POLYGON((172.951 -41.767,172.001 -42.832,169.564 -44.341,172.312 -45.412,175.748 -42.908,172.951 -41.767)))
sql:
ST_Within(origin_geom, ST_GeomFromText('POLYGON((172.951 -41.767,172.001 -42.832,169.564 -44.341,172.312 -45.412,175.748 -42.908,172.951 -41.767))',4326))
* @param sql
*/
func (cql *CqlConverter) ToWithinSql(sql *bytes.Buffer) error {
token := cql.currentToken //within
//log.Println("token1 " + token)
cql.NextToken()
if "(" != cql.currentToken {
//log.Fatalln("Error, not valid WITHIN!!")
return errors.New("not valid WITHIN!!")
}
//get all tokens for the WITHIN claus, parse all comma separated tokens
tokens := cql.GetParenthesisedTokens()
//log.Println((tokens))
sql.WriteString("ST_Within(")
sql.WriteString(tokens[0])
sql.WriteString(", ST_GeomFromText('")
openParenthesis := 0
closeParenthesis := 0
i := 1 //start from 1
numeric := 0
//get the polygon contents
for i < len(tokens) {
token = tokens[i]
i++
if "(" == token {
openParenthesis++
}
if ")" == token {
closeParenthesis++
}
if "POLYGON" == strings.ToUpper(token) {
sql.WriteString("POLYGON((")
numeric = 0
}
//get the coordinates
if openParenthesis == 2 && closeParenthesis == 0 {
if PatternMatch("-?[.\\d]+", token) { //numeric
numeric++
if numeric > 1 {
if numeric%2 == 0 {
sql.WriteString(" ")
} else {
sql.WriteString(",")
}
}
sql.WriteString(token)
}
}
//fmt.Printf("openParenthesis %d closeParenthesis %d", openParenthesis, closeParenthesis)
if openParenthesis == closeParenthesis && closeParenthesis > 0 {
break
}
}
sql.WriteString("))', 4326))")
return nil
}
/**
* cql:
* DWITHIN(origin_geom,Point(175 -41),500,meters)
* sql:
* ST_DWithin(origin_geom::Geography, ST_GeomFromText('POINT(172.951 -41.767)', 4326)::Geography, 5000)
*
* !!!WE ONLY DO METERS!!
* @param sql
*/
func (cql *CqlConverter) ToDWithinSql(sql *bytes.Buffer) error {
token := cql.currentToken //within
//log.Println("token1 " + token)
cql.NextToken()
if "(" != cql.currentToken {
//log.Fatalln("Error, not valid DWITHIN!!")
return errors.New("not valid DWITHIN!!")
}
//get all tokens for the WITHIN claus, parse all comma separated tokens
tokens := cql.GetParenthesisedTokens()
//log.Println((tokens))
sql.WriteString("ST_DWithin(")
sql.WriteString(tokens[0])
sql.WriteString("::Geography, ST_GeomFromText('")
openParenthesis := 0
closeParenthesis := 0
i := 1
numeric := 0
numParenthesis := 0
geom := ""
//get the geometry contents
for i < len(tokens) {
token = tokens[i]
//log.Println(token)
i++
if "(" == token {
openParenthesis++
}
if ")" == token {
closeParenthesis++
}
//log.Println("token3 " + token)
if "POLYGON" == strings.ToUpper(token) || "POLYLINE" == strings.ToUpper(token) || "POINT" == strings.ToUpper(token) {
geom = token
if "POLYGON" == strings.ToUpper(token) || "POLYLINE" == strings.ToUpper(token) {
sql.WriteString(token)
sql.WriteString("((")
numParenthesis = 2
} else if "POINT" == strings.ToUpper(token) {
sql.WriteString("POINT(")
numParenthesis = 1
}
numeric = 0
}
//get the coordinates
if openParenthesis == numParenthesis && closeParenthesis == 0 {
if PatternMatch("-?[.\\d]+", token) { //numeric
numeric++
if numeric > 1 {
if numeric%2 == 0 {
sql.WriteString(" ")
} else {
sql.WriteString(",")
}
}
sql.WriteString(token)
}
}
//fmt.Printf("openParenthesis %d closeParenthesis %d", openParenthesis, closeParenthesis)
if openParenthesis == closeParenthesis && closeParenthesis > 0 {
break
}
}
if "POLYGON" == strings.ToUpper(geom) || "POLYLINE" == strings.ToUpper(geom) {
sql.WriteString("))', 4326)::Geography")
} else if "POINT" == strings.ToUpper(geom) {
sql.WriteString(")', 4326)::Geography")
} else {
return errors.New("No valid geometry for function DWITHIN!!")
}
//get the distance
if i < len(tokens) {
token = tokens[i]
//log.Println("## distance " + token)
if PatternMatch("[.\\d]+", token) {
distance, err := strconv.ParseFloat(token, 64)
if err != nil {
//log.Fatalln("error parsing distance!!")
distance = -1
return errors.New("error parsing distance!!")
}
i++
if i < len(tokens) { //get unit
token = strings.ToLower(tokens[i])
//log.Println("## unit " + token)
if "meters" == token {
} else if "feet" == token {
//log.Println("## change feets to meeters " + token)
distance = 0.3048 * distance
} else {
//log.Fatalln("wrong unit for distance, should be in [meters, feet]!!")
return errors.New("wrong unit for distance, should be in [meters, feet]!!")
}
}
sql.WriteString(", ")
sql.WriteString(strconv.FormatFloat(distance, 'f', -1, 64))
}
}
sql.WriteString(")")
return nil
}
/**
* convert cql to sql
*/
func (cql *CqlConverter) ToSQL() (string, error) {
var (
sql bytes.Buffer
err error
)
err = nil
//go through the CQL string and convert
for cql.currentTokenType != TT_EOF {
cql.NextToken()
//1. leading space
if cql.currentTokenType == TT_RELATION_OPS || cql.currentTokenType == TT_LOGIC_OPS {
sql.WriteString(" ")
}
//2. do content
if cql.currentTokenType == TT_TIMESTRING {
sql.WriteString(cql.currentToken)
sql.WriteString("::timestamptz") //DB column type
} else if cql.currentTokenType == TT_RELATION_OPS && cql.currentToken == "==" { //convert to =
sql.WriteString("=")
} else if cql.currentTokenType == TT_BBOX {
err = cql.ToBBoxSql(&sql)
} else if cql.currentTokenType == TT_WITHIN {
err = cql.ToWithinSql(&sql)
} else if cql.currentTokenType == TT_DWITHIN {
err = cql.ToDWithinSql(&sql)
} else { //just the token
sql.WriteString(cql.currentToken)
}
//3. end space
if cql.currentTokenType == TT_RELATION_OPS || cql.currentTokenType == TT_LOGIC_OPS {
sql.WriteString(" ")
}
//log.Println(cql.Render())
}
return sql.String(), err
}