-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplsql.go
1925 lines (1850 loc) · 44.5 KB
/
plsql.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 Pouya Vedadiyan
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package genql
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"sync"
"github.com/vedadiyan/genql/compare"
"github.com/vedadiyan/sqlparser/pkg/sqlparser"
)
type (
Map = map[string]any
GroupDefinition = map[string]bool
WhereDefinition = *sqlparser.Where
HavingDefinition = *sqlparser.Where
SelectDefinition = sqlparser.SelectExprs
Statement = sqlparser.Statement
QueryOption func(query *Query)
CteEvaluation = func() (any, error)
Function func(*Query, Map, *FunctionOptions, []any) (any, error)
NeutalString string
ColumnName string
Ommit bool
Fuse map[string]any
FunctionOptions struct {
}
ExpressionReaderOptions struct {
}
OrderByDefinition []struct {
Key string
Value bool
}
Options struct {
wrapped bool
postgresEscapingDialect bool
idomaticArrays bool
completed func()
errors func(err error)
constants map[string]any
vars map[string]any
varsMut sync.RWMutex
}
Query struct {
data Map
from []any
//processed []any
distinct bool
selectDefinition SelectDefinition
whereDefinition WhereDefinition
groupDefinition GroupDefinition
offsetDefinition int
limitDefinition int
havingDefinition HavingDefinition
orderByDefinition OrderByDefinition
wg sync.WaitGroup
singletonExecutions map[string]any
postProcessors []func() error
dual bool
options *Options
}
)
var (
functions map[string]Function
immediateFunctions []string
)
func Wrapped() QueryOption {
return func(query *Query) {
query.options.wrapped = true
}
}
func PostgresEscapingDialect() QueryOption {
return func(query *Query) {
query.options.postgresEscapingDialect = true
}
}
func IdomaticArrays() QueryOption {
return func(query *Query) {
query.options.idomaticArrays = true
}
}
func CompletedCallback(callback func()) QueryOption {
return func(query *Query) {
query.options.completed = callback
}
}
func UnReportedErrors(handler func(error)) QueryOption {
return func(query *Query) {
query.options.errors = handler
}
}
func WithConstants(constants map[string]any) QueryOption {
return func(query *Query) {
query.options.constants = constants
}
}
func WithVars(vars map[string]any) QueryOption {
return func(query *Query) {
query.options.vars = vars
}
}
func New(data Map, query string, options ...QueryOption) (*Query, error) {
q := &Query{
offsetDefinition: -1,
limitDefinition: -1,
groupDefinition: make(GroupDefinition),
orderByDefinition: make(OrderByDefinition, 0),
singletonExecutions: make(map[string]any),
postProcessors: make([]func() error, 0),
options: &Options{},
}
for _, option := range options {
option(q)
}
switch q.options.wrapped {
case true:
{
q.data = Map{"root": data}
}
default:
{
q.data = data
}
}
if q.options.postgresEscapingDialect {
rs, err := DoubleQuotesToBackTick(query)
if err != nil {
return nil, err
}
query = rs
}
if q.options.idomaticArrays {
rs, err := FixIdiomaticArray(query)
if err != nil {
return nil, err
}
query = rs
}
statement, err := Parse(query)
if err != nil {
return nil, err
}
err = Build(q, statement)
if err != nil {
return nil, err
}
return q, nil
}
func Prepare(data Map, statement sqlparser.Statement, options *Options) (*Query, error) {
q := &Query{
offsetDefinition: -1,
limitDefinition: -1,
groupDefinition: make(GroupDefinition),
orderByDefinition: make(OrderByDefinition, 0),
singletonExecutions: map[string]any{},
postProcessors: make([]func() error, 0),
options: options,
}
q.data = data
err := Build(q, statement)
if err != nil {
return nil, err
}
return q, nil
}
func Parse(query string) (Statement, error) {
return sqlparser.Parse(query)
}
func Build(query *Query, statement Statement) error {
switch statement := statement.(type) {
case *sqlparser.Select:
{
return BuildSelect(query, statement)
}
case *sqlparser.Union:
{
return BuildUnion(query, statement)
}
default:
{
return UNSUPPORTED_CASE.Extend(fmt.Sprintf("%T is not supported", statement))
}
}
}
func BuildSelect(query *Query, slct *sqlparser.Select) error {
if len(slct.From) > 1 {
return EXPECTATION_FAILED.Extend("this version of gql does not support multiple table selection")
}
err := BuildCte(query, slct.With)
if err != nil {
return err
}
err = BuildFrom(query, &slct.From[0])
if err != nil {
return err
}
err = BuildLimit(query, slct.Limit)
if err != nil {
return err
}
err = BuildGroup(query, &slct.GroupBy)
if err != nil {
return err
}
err = BuildOrder(query, &slct.OrderBy)
if err != nil {
return err
}
query.havingDefinition = slct.Having
query.selectDefinition = slct.SelectExprs
query.whereDefinition = slct.Where
query.distinct = slct.Distinct
return nil
}
func BuildUnion(query *Query, expr *sqlparser.Union) error {
leftStatement := expr.Left.(*sqlparser.Select)
leftStatement.With = expr.With
rightStatement := expr.Right.(*sqlparser.Select)
rightStatement.With = expr.With
left, err := Prepare(query.data, leftStatement, query.options)
if err != nil {
return err
}
leftData, err := left.execAndPostProcess()
if err != nil {
return err
}
right, err := Prepare(query.data, rightStatement, query.options)
if err != nil {
return err
}
rightData, err := right.execAndPostProcess()
if err != nil {
return err
}
leftDataArray, err := AsArray(leftData)
if err != nil {
return err
}
rightDataArray, err := AsArray(rightData)
if err != nil {
return err
}
slice := make([]any, 0)
slice = append(slice, leftDataArray...)
slice = append(slice, rightDataArray...)
query.from = slice
query.selectDefinition = sqlparser.SelectExprs{
&sqlparser.StarExpr{},
}
err = BuildLimit(query, expr.Limit)
if err != nil {
return err
}
return nil
}
func BuildCte(query *Query, expr *sqlparser.With) error {
if expr == nil {
return nil
}
for _, cte := range expr.Ctes {
copy := *cte
query.data[copy.ID.String()] = CteEvaluation(func() (any, error) {
query, err := Prepare(query.data, copy.Subquery.Select, query.options)
if err != nil {
return nil, err
}
rs, err := query.execAndPostProcess()
if err != nil {
return nil, err
}
query.data[copy.ID.String()] = rs
return rs, nil
})
}
return nil
}
func BuildLimit(query *Query, limit *sqlparser.Limit) error {
if limit == nil {
return nil
}
if limit.Offset != nil {
_, offsetLiteral, err := BuildLiteral(limit.Offset)
if err != nil {
return err
}
offsetNumeric, err := strconv.Atoi(offsetLiteral)
if err != nil {
return err
}
query.offsetDefinition = offsetNumeric
}
_, limitLiteral, err := BuildLiteral(limit.Rowcount)
if err != nil {
return err
}
limitNumeric, err := strconv.Atoi(limitLiteral)
if err != nil {
return err
}
query.limitDefinition = limitNumeric
return nil
}
func BuildGroup(query *Query, group *sqlparser.GroupBy) error {
if group == nil {
return nil
}
for _, i := range *group {
qualifier, name, err := BuildColumnName(i)
if err != nil {
return nil
}
if len(qualifier) == 0 {
query.groupDefinition[name] = true
continue
}
query.groupDefinition[fmt.Sprintf("%s.%s", qualifier, name)] = true
}
return nil
}
func BuildOrder(query *Query, orderBy *sqlparser.OrderBy) error {
if orderBy == nil {
return nil
}
for _, ordeorderBy := range *orderBy {
qualifier, columnName, err := BuildColumnName(ordeorderBy.Expr)
if err != nil {
return err
}
if len(qualifier) == 0 {
query.orderByDefinition = append(query.orderByDefinition, struct {
Key string
Value bool
}{
Key: columnName,
Value: ordeorderBy.Direction == sqlparser.AscOrder,
})
continue
}
query.orderByDefinition = append(query.orderByDefinition, struct {
Key string
Value bool
}{
Key: fmt.Sprintf("%s.%s", qualifier, columnName),
Value: ordeorderBy.Direction == sqlparser.AscOrder,
})
}
return nil
}
func BuildFrom(query *Query, tableExpr *sqlparser.TableExpr) error {
switch tableExpr := (*tableExpr).(type) {
case *sqlparser.AliasedTableExpr:
{
return BuilFromAliasedTable(query, tableExpr.As.String(), tableExpr.Expr)
}
case *sqlparser.JoinTableExpr:
{
return BuildJoin(query, tableExpr)
}
default:
{
return EXPECTATION_FAILED.Extend("invalid from clause")
}
}
}
func BuildJoin(query *Query, joinExpr *sqlparser.JoinTableExpr) error {
left := CopyQuery(query)
err := BuildFrom(left, &joinExpr.LeftExpr)
if err != nil {
return err
}
right := CopyQuery(query)
err = BuildFrom(right, &joinExpr.RightExpr)
if err != nil {
return err
}
rs, err := ExecJoin(query, left.from, right.from, joinExpr.Condition.On, joinExpr.Join)
if err != nil {
return nil
}
query.from = rs
return nil
}
func ExecJoin(query *Query, left []any, right []any, joinExpr sqlparser.Expr, joinType sqlparser.JoinType) ([]any, error) {
if joinType == sqlparser.RightJoinType {
left, right = right, left
}
slice := make([]any, 0)
for _, left := range left {
left, ok := left.(Map)
if !ok {
return nil, INVALID_TYPE.Extend(fmt.Sprintf("failed to build `JOIN` expression, expected object but found %T", left))
}
joined := false
for _, right := range right {
current := make(Map)
for key, value := range left {
current[key] = value
}
right, ok := right.(Map)
if !ok {
return nil, INVALID_TYPE.Extend(fmt.Sprintf("failed to build `JOIN` expression, expected object but found %T", left))
}
for key, value := range right {
current[key] = value
}
rs, err := Expr(query, current, joinExpr, nil)
if err != nil {
return nil, err
}
rsValue, ok := rs.(bool)
if !ok {
return nil, INVALID_TYPE.Extend(fmt.Sprintf("failed to build `JOIN` expression, expected boolean but found %T", left))
}
if rsValue {
slice = append(slice, current)
joined = true
}
}
if !joined {
current := make(Map)
for key, value := range left {
current[key] = value
}
if joinType != sqlparser.NormalJoinType {
slice = append(slice, current)
}
}
}
return slice, nil
}
func BuildLiteral(expr sqlparser.Expr) (sqlparser.ValType, string, error) {
literal, ok := expr.(*sqlparser.Literal)
if !ok {
return 0, "", INVALID_TYPE.Extend(fmt.Sprintf("failed to build `LITERAL` expression, expected Literal but found %T", expr))
}
return literal.Type, literal.Val, nil
}
func BuildColumnName(expr sqlparser.Expr) (string, string, error) {
columnName, ok := expr.(*sqlparser.ColName)
if !ok {
return "", "", INVALID_TYPE.Extend(fmt.Sprintf("failed to build `COLUMN` name. expected ColName but found %T", expr))
}
return columnName.Qualifier.Name.String(), columnName.Name.String(), nil
}
func BuilFromAliasedTable(query *Query, as string, expr sqlparser.SimpleTableExpr) error {
switch expr := expr.(type) {
case sqlparser.TableName:
{
var tableName string
qualifier := expr.Qualifier.String()
name := expr.Name.String()
if len(qualifier) == 0 {
tableName = name
} else {
tableName = fmt.Sprintf("%s.%s", qualifier, name)
}
data, err := ExecReader(query.data, tableName)
if err != nil {
return err
}
switch data := data.(type) {
case CteEvaluation:
{
data, err := data()
if err != nil {
return err
}
array, err := AsArray(data)
if err != nil {
return err
}
alias := ProcessAlias(array, as)
query.from = alias
return nil
}
case nil:
{
if qualifier == "" && tableName == "dual" {
query.dual = true
array, err := AsArray(query.data)
if err != nil {
return err
}
query.from = array
return nil
}
return nil
}
default:
{
array, err := AsArray(data)
if err != nil {
return err
}
alias := ProcessAlias(array, as)
query.from = alias
return nil
}
}
}
case *sqlparser.DerivedTable:
{
subquery, err := Prepare(query.data, expr.Select, query.options)
if err != nil {
return err
}
data, err := subquery.exec()
if err != nil {
return err
}
query.postProcessors = append(query.postProcessors, subquery.postProcessors...)
query.wg.Add(1)
go func() {
subquery.wg.Wait()
query.wg.Done()
}()
array, err := AsArray(data)
if err != nil {
return err
}
alias := ProcessAlias(array, as)
query.from = alias
return nil
}
default:
{
return UNSUPPORTED_CASE.Extend("invalid from clause")
}
}
}
func ProcessAlias(data []any, as string) []any {
if len(as) == 0 {
return data
}
slice := make([]any, len(data))
for i, j := range data {
slice[i] = Map{
as: j,
}
}
return slice
}
func Expr(query *Query, current Map, expr sqlparser.Expr, options *ExpressionReaderOptions) (any, error) {
switch expr := expr.(type) {
case *sqlparser.AndExpr:
{
return AndExpr(query, current, expr)
}
case *sqlparser.OrExpr:
{
return OrExpr(query, current, expr)
}
case *sqlparser.ComparisonExpr:
{
return ComparisonExpr(query, current, expr)
}
case *sqlparser.BetweenExpr:
{
return BetweenExpr(query, current, expr)
}
case *sqlparser.BinaryExpr:
{
return BinaryExpr(query, current, expr)
}
case *sqlparser.Literal:
{
return LiteralExpr(query, current, expr)
}
case *sqlparser.NullVal:
{
return nil, nil
}
case *sqlparser.IsExpr:
{
return IsExpr(query, current, expr)
}
case *sqlparser.NotExpr:
{
return NotExpr(query, current, expr)
}
case *sqlparser.SubstrExpr:
{
return SubStrExpr(query, current, expr)
}
case *sqlparser.UnaryExpr:
{
return UnaryExpr(query, current, expr)
}
case sqlparser.ValTuple:
{
return ValueTupleExpr(query, current, &expr)
}
case sqlparser.BoolVal:
{
return bool(expr), nil
}
case *sqlparser.ColName:
{
qualifier, name, err := BuildColumnName(expr)
if err != nil {
return nil, err
}
columnName := name
if len(qualifier) > 0 {
columnName = fmt.Sprintf("%s.%s", qualifier, name)
}
return ColumnName(columnName), nil
}
case *sqlparser.Subquery:
{
return SubqueryExpr(query, current, expr)
}
case *sqlparser.CaseExpr:
{
return CaseExpr(query, current, expr)
}
case *sqlparser.ExistsExpr:
{
return ExistExpr(query, current, expr)
}
case *sqlparser.FuncExpr:
{
return FunExpr(query, current, expr)
}
case sqlparser.AggrFunc:
{
return AggrFunExpr(query, current, expr)
}
default:
{
return nil, UNSUPPORTED_CASE
}
}
}
func AndExpr(query *Query, current Map, expr *sqlparser.AndExpr) (bool, error) {
left, err := Expr(query, current, expr.Left, nil)
if err != nil {
return false, err
}
leftValueRaw, err := ValueOf(query, current, left)
if err != nil {
return false, err
}
if leftValueRaw == nil {
return false, EXPECTATION_FAILED.Extend("failed to build `AND` expreesion. left side value is nil")
}
leftValue, err := AsType[bool](leftValueRaw)
if err != nil {
return false, err
}
right, err := Expr(query, current, expr.Right, nil)
if err != nil {
return false, err
}
rightValueRaw, err := ValueOf(query, current, right)
if err != nil {
return false, err
}
if rightValueRaw == nil {
return false, EXPECTATION_FAILED.Extend("failed to build `AND` expreesion. right side value is nil")
}
rightValue, err := AsType[bool](rightValueRaw)
if err != nil {
return false, err
}
return *leftValue && *rightValue, nil
}
func OrExpr(query *Query, current Map, expr *sqlparser.OrExpr) (bool, error) {
left, err := Expr(query, current, expr.Left, nil)
if err != nil {
return false, err
}
leftValueRaw, err := ValueOf(query, current, left)
if err != nil {
return false, err
}
if leftValueRaw == nil {
return false, EXPECTATION_FAILED.Extend("failed to build `OR` expreesion. left side value is nil")
}
leftValue, err := AsType[bool](leftValueRaw)
if err != nil {
return false, err
}
right, err := Expr(query, current, expr.Right, nil)
if err != nil {
return false, err
}
rightValueRaw, err := ValueOf(query, current, right)
if err != nil {
return false, err
}
if rightValueRaw == nil {
return false, EXPECTATION_FAILED.Extend("failed to build `OR` expreesion. right side value is nil")
}
rightValue, err := AsType[bool](rightValueRaw)
if err != nil {
return false, err
}
return *leftValue || *rightValue, nil
}
func ComparisonExpr(query *Query, current Map, expr *sqlparser.ComparisonExpr) (bool, error) {
current["<-"] = query.data
defer delete(current, "<-")
left, err := Expr(query, current, expr.Left, nil)
if err != nil {
return false, err
}
leftValue, err := ValueOf(query, current, left)
if err != nil {
return false, err
}
right, err := Expr(query, current, expr.Right, nil)
if err != nil {
return false, err
}
rightValue, err := ValueOf(query, current, right)
if err != nil {
return false, err
}
switch expr.Operator {
case sqlparser.EqualOp:
{
return compare.Compare(leftValue, rightValue) == 0, nil
}
case sqlparser.NotEqualOp:
{
return compare.Compare(leftValue, rightValue) != 0, nil
}
case sqlparser.GreaterThanOp:
{
return compare.Compare(leftValue, rightValue) == 1, nil
}
case sqlparser.GreaterEqualOp:
{
return compare.Compare(leftValue, rightValue) >= 0, nil
}
case sqlparser.LessThanOp:
{
return compare.Compare(leftValue, rightValue) == -1, nil
}
case sqlparser.LessEqualOp:
{
return compare.Compare(leftValue, rightValue) <= 0, nil
}
case sqlparser.LikeOp:
{
return RegexComparison(fmt.Sprintf("%v", leftValue), fmt.Sprintf("%v", rightValue))
}
case sqlparser.NotLikeOp:
{
rs, err := RegexComparison(fmt.Sprintf("%v", leftValue), fmt.Sprintf("%v", rightValue))
if err != nil {
return false, err
}
return !rs, nil
}
case sqlparser.InOp:
{
if right == nil {
return false, EXPECTATION_FAILED.Extend("failed to build `IN` expreesion. right side value is nil")
}
rightArray, ok := (right).([]any)
if !ok {
return false, INVALID_TYPE.Extend(fmt.Sprintf("failed to build `IN` expression. expected an array but found %T", right))
}
for _, value := range rightArray {
switch value := value.(type) {
case Map:
{
for _, value := range value {
if v, ok := value.(*float64); ok {
value = *v
}
if compare.Compare(leftValue, value) == 0 {
return true, nil
}
break
}
}
default:
{
if v, ok := value.(*float64); ok {
value = *v
}
if compare.Compare(leftValue, value) == 0 {
return true, nil
}
}
}
}
return false, nil
}
case sqlparser.NotInOp:
{
if right == nil {
return false, EXPECTATION_FAILED.Extend("failed to build `NOT IN` expreesion. right side value is nil")
}
rightArray, ok := (right).([]any)
if !ok {
return false, INVALID_TYPE.Extend(fmt.Sprintf("failed to build `IN` expression. expected an array but found %T", right))
}
for _, value := range rightArray {
if leftValue == fmt.Sprintf("%v", value) {
return false, nil
}
}
return true, nil
}
default:
{
return false, UNSUPPORTED_CASE
}
}
}
func BetweenExpr(query *Query, current Map, expr *sqlparser.BetweenExpr) (bool, error) {
point, err := Expr(query, current, expr.Left, nil)
if err != nil {
return false, err
}
pointValueRaw, err := ValueOf(query, current, point)
if err != nil {
return false, err
}
// TO DO: could be either a number or a date
from, err := Expr(query, current, expr.From, nil)
if err != nil {
return false, err
}
// TO DO: could be either a number or a date
to, err := Expr(query, current, expr.To, nil)
if err != nil {
return false, err
}
pointValue := fmt.Sprintf("%v", pointValueRaw)
fromValue := fmt.Sprintf("%v", from)
toValue := fmt.Sprintf("%v", to)
switch expr.IsBetween {
case true:
{
return (pointValue > fromValue) && (pointValue < toValue), nil
}
default:
{
return !((pointValue > fromValue) && (pointValue < toValue)), nil
}
}
}
func BinaryExpr(query *Query, current Map, expr *sqlparser.BinaryExpr) (*float64, error) {
left, err := Expr(query, current, expr.Left, nil)
if err != nil {
return nil, err
}
leftValueRaw, err := ValueOf(query, current, left)
if err != nil {
return nil, err
}
if leftValueRaw == nil {
return nil, nil
}
leftValue, err := AsType[float64](leftValueRaw)
if err != nil {
return nil, err
}
right, err := Expr(query, current, expr.Right, nil)
if err != nil {
return nil, err
}
rightValueRaw, err := ValueOf(query, current, right)
if err != nil {
return nil, err
}
if rightValueRaw == nil {
return nil, nil
}
rightValue, err := AsType[float64](rightValueRaw)
if err != nil {
return nil, err
}
switch expr.Operator {
case sqlparser.PlusOp:
{
rs := *leftValue + *rightValue
return &rs, nil
}
case sqlparser.MinusOp:
{
rs := *leftValue - *rightValue
return &rs, nil
}
case sqlparser.MultOp:
{
rs := *leftValue * *rightValue
return &rs, nil
}
case sqlparser.DivOp:
{
rs := *leftValue / *rightValue
return &rs, nil
}
case sqlparser.IntDivOp:
{
rs := float64(int64(*leftValue) / int64(*rightValue))
return &rs, nil
}
case sqlparser.ModOp:
{
rs := math.Mod(*leftValue, *rightValue)
return &rs, nil
}
case sqlparser.BitAndOp:
{
rs := float64(int64(*leftValue) & int64(*rightValue))
return &rs, nil
}
case sqlparser.BitOrOp:
{
rs := float64(int64(*leftValue) | int64(*rightValue))
return &rs, nil
}
case sqlparser.BitXorOp:
{
rs := float64(int64(*leftValue) ^ int64(*rightValue))
return &rs, nil
}
case sqlparser.ShiftLeftOp:
{
rs := float64(int64(*leftValue) << int64(*rightValue))
return &rs, nil
}
case sqlparser.ShiftRightOp:
{
rs := float64(int64(*leftValue) >> int64(*rightValue))
return &rs, nil
}
default:
{
return nil, UNSUPPORTED_CASE
}
}
}
func LiteralExpr(query *Query, current Map, expr *sqlparser.Literal) (any, error) {
literalType, literalValue, err := BuildLiteral(expr)
if err != nil {
return nil, err