forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite3Expression.java
More file actions
1556 lines (1294 loc) · 53.9 KB
/
SQLite3Expression.java
File metadata and controls
1556 lines (1294 loc) · 53.9 KB
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
package sqlancer.sqlite3.ast;
import java.util.List;
import java.util.Optional;
import sqlancer.IgnoreMeException;
import sqlancer.LikeImplementationHelper;
import sqlancer.Randomly;
import sqlancer.common.ast.newast.Expression;
import sqlancer.common.visitor.BinaryOperation;
import sqlancer.common.visitor.UnaryOperation;
import sqlancer.sqlite3.SQLite3CollateHelper;
import sqlancer.sqlite3.SQLite3Provider;
import sqlancer.sqlite3.ast.SQLite3Expression.BinaryComparisonOperation.BinaryComparisonOperator;
import sqlancer.sqlite3.ast.SQLite3Expression.Sqlite3BinaryOperation.BinaryOperator;
import sqlancer.sqlite3.ast.SQLite3UnaryOperation.UnaryOperator;
import sqlancer.sqlite3.schema.SQLite3DataType;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Table;
public abstract class SQLite3Expression implements Expression<SQLite3Column> {
public static class SQLite3TableReference extends SQLite3Expression {
private final String indexedBy;
private final SQLite3Table table;
public SQLite3TableReference(String indexedBy, SQLite3Table table) {
this.indexedBy = indexedBy;
this.table = table;
}
public SQLite3TableReference(SQLite3Table table) {
this.indexedBy = null;
this.table = table;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
public SQLite3Table getTable() {
return table;
}
public String getIndexedBy() {
return indexedBy;
}
}
public static class SQLite3Distinct extends SQLite3Expression {
private final SQLite3Expression expr;
public SQLite3Distinct(SQLite3Expression expr) {
this.expr = expr;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return expr.getExplicitCollateSequence();
}
@Override
public SQLite3Constant getExpectedValue() {
return expr.getExpectedValue();
}
public SQLite3Expression getExpression() {
return expr;
}
@Override
public SQLite3CollateSequence getImplicitCollateSequence() {
// https://www.sqlite.org/src/tktview/18ab5da2c05ad57d7f9d79c41d3138b141378543
return expr.getImplicitCollateSequence();
}
}
public SQLite3Constant getExpectedValue() {
return null;
}
public enum TypeAffinity {
INTEGER, TEXT, BLOB, REAL, NUMERIC, NONE;
public boolean isNumeric() {
return this == INTEGER || this == REAL || this == NUMERIC;
}
}
/*
* See https://www.sqlite.org/datatype3.html 3.2
*/
public TypeAffinity getAffinity() {
return TypeAffinity.NONE;
}
/*
* See https://www.sqlite.org/datatype3.html#assigning_collating_sequences_from_sql 7.1
*
*/
public abstract SQLite3CollateSequence getExplicitCollateSequence();
public SQLite3CollateSequence getImplicitCollateSequence() {
return null;
}
public static class SQLite3Exist extends SQLite3Expression {
private final SQLite3Expression select;
public SQLite3Exist(SQLite3Expression select) {
this.select = select;
}
public SQLite3Expression getExpression() {
return select;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
}
public static class Join extends SQLite3Expression
implements sqlancer.common.ast.newast.Join<SQLite3Expression, SQLite3Table, SQLite3Column> {
public enum JoinType {
INNER, CROSS, OUTER, NATURAL, RIGHT, FULL;
}
private final SQLite3Table table;
private SQLite3Expression onClause;
private JoinType type;
public Join(Join other) {
this.table = other.table;
this.onClause = other.onClause;
this.type = other.type;
}
public Join(SQLite3Table table, SQLite3Expression onClause, JoinType type) {
this.table = table;
this.onClause = onClause;
this.type = type;
}
public Join(SQLite3Table table, JoinType type) {
this.table = table;
if (type != JoinType.NATURAL) {
throw new AssertionError();
}
this.onClause = null;
this.type = type;
}
public SQLite3Table getTable() {
return table;
}
@Override
public SQLite3Expression getOnClause() {
return onClause;
}
public JoinType getType() {
return type;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
@Override
public void setOnClause(SQLite3Expression onClause) {
this.onClause = onClause;
}
public void setType(JoinType type) {
this.type = type;
}
}
public static class Subquery extends SQLite3Expression {
private final String query;
public Subquery(String query) {
this.query = query;
}
public static SQLite3Expression create(String query) {
return new Subquery(query);
}
public String getQuery() {
return query;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
}
public static class TypeLiteral {
private final Type type;
public enum Type {
TEXT {
@Override
public SQLite3Constant apply(SQLite3Constant cons) {
return SQLite3Cast.castToText(cons);
}
},
REAL {
@Override
public SQLite3Constant apply(SQLite3Constant cons) {
return SQLite3Cast.castToReal(cons);
}
},
INTEGER {
@Override
public SQLite3Constant apply(SQLite3Constant cons) {
return SQLite3Cast.castToInt(cons);
}
},
NUMERIC {
@Override
public SQLite3Constant apply(SQLite3Constant cons) {
return SQLite3Cast.castToNumeric(cons);
}
},
BLOB {
@Override
public SQLite3Constant apply(SQLite3Constant cons) {
return SQLite3Cast.castToBlob(cons);
}
};
public abstract SQLite3Constant apply(SQLite3Constant cons);
}
public TypeLiteral(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
}
public static class Cast extends SQLite3Expression {
private final TypeLiteral type;
private final SQLite3Expression expression;
public Cast(TypeLiteral typeofExpr, SQLite3Expression expression) {
this.type = typeofExpr;
this.expression = expression;
}
public SQLite3Expression getExpression() {
return expression;
}
public TypeLiteral getType() {
return type;
}
@Override
public SQLite3Constant getExpectedValue() {
if (expression.getExpectedValue() == null) {
return null;
} else {
return type.type.apply(expression.getExpectedValue());
}
}
/**
* An expression of the form "CAST(expr AS type)" has an affinity that is the same as a column with a declared
* type of "type".
*/
@Override
public TypeAffinity getAffinity() {
switch (type.type) {
case BLOB:
return TypeAffinity.BLOB;
case INTEGER:
return TypeAffinity.INTEGER;
case NUMERIC:
return TypeAffinity.NUMERIC;
case REAL:
return TypeAffinity.REAL;
case TEXT:
return TypeAffinity.TEXT;
default:
throw new AssertionError();
}
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return expression.getExplicitCollateSequence();
}
@Override
public SQLite3CollateSequence getImplicitCollateSequence() {
if (SQLite3CollateHelper.shouldGetSubexpressionAffinity(expression)) {
return expression.getImplicitCollateSequence();
} else {
return null;
}
}
}
public static class BetweenOperation extends SQLite3Expression {
private final SQLite3Expression expr;
private final boolean negated;
private final SQLite3Expression left;
private final SQLite3Expression right;
public BetweenOperation(SQLite3Expression expr, boolean negated, SQLite3Expression left,
SQLite3Expression right) {
this.expr = expr;
this.negated = negated;
this.left = left;
this.right = right;
}
public SQLite3Expression getExpression() {
return expr;
}
public boolean isNegated() {
return negated;
}
public SQLite3Expression getLeft() {
return left;
}
public SQLite3Expression getRight() {
return right;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
if (expr.getExplicitCollateSequence() != null) {
return expr.getExplicitCollateSequence();
} else if (left.getExplicitCollateSequence() != null) {
return left.getExplicitCollateSequence();
} else {
return right.getExplicitCollateSequence();
}
}
@Override
public SQLite3Constant getExpectedValue() {
return getTopNode().getExpectedValue();
}
public SQLite3Expression getTopNode() {
BinaryComparisonOperation leftOp = new BinaryComparisonOperation(expr, left,
BinaryComparisonOperator.GREATER_EQUALS);
BinaryComparisonOperation rightOp = new BinaryComparisonOperation(expr, right,
BinaryComparisonOperator.SMALLER_EQUALS);
Sqlite3BinaryOperation and = new Sqlite3BinaryOperation(leftOp, rightOp, BinaryOperator.AND);
if (negated) {
return new SQLite3UnaryOperation(UnaryOperator.NOT, and);
} else {
return and;
}
}
}
public static class Function extends SQLite3Expression {
private final SQLite3Expression[] arguments;
private final String name;
public Function(String name, SQLite3Expression... arguments) {
this.name = name;
this.arguments = arguments.clone();
}
public SQLite3Expression[] getArguments() {
return arguments.clone();
}
public String getName() {
return name;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
for (SQLite3Expression arg : arguments) {
if (arg.getExplicitCollateSequence() != null) {
return arg.getExplicitCollateSequence();
}
}
return null;
}
}
public static class SQLite3OrderingTerm extends SQLite3Expression {
private final SQLite3Expression expression;
private final Ordering ordering;
public enum Ordering {
ASC, DESC;
public static Ordering getRandomValue() {
return Randomly.fromOptions(Ordering.values());
}
}
public SQLite3OrderingTerm(SQLite3Expression expression, Ordering ordering) {
this.expression = expression;
this.ordering = ordering;
}
public SQLite3Expression getExpression() {
return expression;
}
public Ordering getOrdering() {
return ordering;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return expression.getExplicitCollateSequence();
}
}
public static class CollateOperation extends SQLite3Expression {
private final SQLite3Expression expression;
private final SQLite3CollateSequence collate;
public CollateOperation(SQLite3Expression expression, SQLite3CollateSequence collate) {
this.expression = expression;
this.collate = collate;
}
public SQLite3CollateSequence getCollate() {
return collate;
}
public SQLite3Expression getExpression() {
return expression;
}
// If either operand has an explicit collating function assignment using the
// postfix COLLATE operator, then the explicit collating function is used for
// comparison, with precedence to the collating function of the left operand.
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return collate;
}
@Override
public SQLite3Constant getExpectedValue() {
return expression.getExpectedValue();
}
@Override
public TypeAffinity getAffinity() {
return expression.getAffinity();
}
}
public static class SQLite3PostfixUnaryOperation extends SQLite3Expression
implements UnaryOperation<SQLite3Expression> {
public enum PostfixUnaryOperator {
ISNULL("ISNULL") {
@Override
public SQLite3Constant apply(SQLite3Constant expectedValue) {
if (expectedValue.isNull()) {
return SQLite3Constant.createTrue();
} else {
return SQLite3Constant.createFalse();
}
}
},
NOT_NULL("NOT NULL") {
@Override
public SQLite3Constant apply(SQLite3Constant expectedValue) {
if (expectedValue.isNull()) {
return SQLite3Constant.createFalse();
} else {
return SQLite3Constant.createTrue();
}
}
},
NOTNULL("NOTNULL") {
@Override
public SQLite3Constant apply(SQLite3Constant expectedValue) {
if (expectedValue.isNull()) {
return SQLite3Constant.createFalse();
} else {
return SQLite3Constant.createTrue();
}
}
},
IS_TRUE("IS TRUE") {
@Override
public SQLite3Constant apply(SQLite3Constant expectedValue) {
if (expectedValue.isNull()) {
return SQLite3Constant.createIntConstant(0);
}
return SQLite3Cast.asBoolean(expectedValue);
}
},
IS_FALSE("IS FALSE") {
@Override
public SQLite3Constant apply(SQLite3Constant expectedValue) {
if (expectedValue.isNull()) {
return SQLite3Constant.createIntConstant(0);
}
return SQLite3UnaryOperation.UnaryOperator.NOT.apply(SQLite3Cast.asBoolean(expectedValue));
}
};
private final String textRepresentation;
PostfixUnaryOperator(String textRepresentation) {
this.textRepresentation = textRepresentation;
}
@Override
public String toString() {
return getTextRepresentation();
}
public String getTextRepresentation() {
return textRepresentation;
}
public static PostfixUnaryOperator getRandomOperator() {
return Randomly.fromOptions(values());
}
public abstract SQLite3Constant apply(SQLite3Constant expectedValue);
}
private final PostfixUnaryOperator operation;
private final SQLite3Expression expression;
public SQLite3PostfixUnaryOperation(PostfixUnaryOperator operation, SQLite3Expression expression) {
this.operation = operation;
this.expression = expression;
}
public PostfixUnaryOperator getOperation() {
return operation;
}
@Override
public SQLite3Expression getExpression() {
return expression;
}
@Override
public SQLite3Constant getExpectedValue() {
if (expression.getExpectedValue() == null) {
return null;
}
return operation.apply(expression.getExpectedValue());
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return expression.getExplicitCollateSequence();
}
@Override
public String getOperatorRepresentation() {
return operation.getTextRepresentation();
}
@Override
public OperatorKind getOperatorKind() {
return OperatorKind.POSTFIX;
}
}
public static class InOperation extends SQLite3Expression {
private final SQLite3Expression left;
private List<SQLite3Expression> rightExpressionList;
private SQLite3Expression rightSelect;
public InOperation(SQLite3Expression left, List<SQLite3Expression> right) {
this.left = left;
this.rightExpressionList = right;
}
public InOperation(SQLite3Expression left, SQLite3Expression select) {
this.left = left;
this.rightSelect = select;
}
public SQLite3Expression getLeft() {
return left;
}
public List<SQLite3Expression> getRightExpressionList() {
return rightExpressionList;
}
public SQLite3Expression getRightSelect() {
return rightSelect;
}
@Override
// The collating sequence used for expressions of the form "x IN (y, z, ...)" is
// the collating sequence of x.
public SQLite3CollateSequence getExplicitCollateSequence() {
if (left.getExplicitCollateSequence() != null) {
return left.getExplicitCollateSequence();
} else {
return null;
}
}
@Override
public SQLite3Constant getExpectedValue() {
// TODO query as right hand side is not implemented
if (left.getExpectedValue() == null) {
return null;
}
if (rightExpressionList.isEmpty()) {
return SQLite3Constant.createFalse();
} else if (left.getExpectedValue().isNull()) {
return SQLite3Constant.createNullConstant();
} else {
boolean containsNull = false;
for (SQLite3Expression expr : getRightExpressionList()) {
if (expr.getExpectedValue() == null) {
return null; // TODO: we can still compute something if the value is already contained
}
SQLite3CollateSequence collate = getExplicitCollateSequence();
if (collate == null) {
collate = left.getImplicitCollateSequence();
}
if (collate == null) {
collate = SQLite3CollateSequence.BINARY;
}
ConstantTuple convertedConstants = applyAffinities(left.getAffinity(), TypeAffinity.NONE,
left.getExpectedValue(), expr.getExpectedValue());
SQLite3Constant equals = left.getExpectedValue().applyEquals(convertedConstants.right, collate);
Optional<Boolean> isEquals = SQLite3Cast.isTrue(equals);
if (isEquals.isPresent() && isEquals.get()) {
return SQLite3Constant.createTrue();
} else if (!isEquals.isPresent()) {
containsNull = true;
}
}
if (containsNull) {
return SQLite3Constant.createNullConstant();
} else {
return SQLite3Constant.createFalse();
}
}
}
}
public static class MatchOperation extends SQLite3Expression {
private final SQLite3Expression left;
private final SQLite3Expression right;
public MatchOperation(SQLite3Expression left, SQLite3Expression right) {
this.left = left;
this.right = right;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
public SQLite3Expression getLeft() {
return left;
}
public SQLite3Expression getRight() {
return right;
}
}
public static class BinaryComparisonOperation extends SQLite3Expression
implements BinaryOperation<SQLite3Expression> {
private final BinaryComparisonOperator operation;
private final SQLite3Expression left;
private final SQLite3Expression right;
public BinaryComparisonOperation(SQLite3Expression left, SQLite3Expression right,
BinaryComparisonOperator operation) {
this.left = left;
this.right = right;
this.operation = operation;
}
public BinaryComparisonOperator getOperator() {
return operation;
}
@Override
public SQLite3Expression getLeft() {
return left;
}
@Override
public SQLite3Expression getRight() {
return right;
}
@Override
public SQLite3Constant getExpectedValue() {
SQLite3Constant leftExpected = left.getExpectedValue();
SQLite3Constant rightExpected = right.getExpectedValue();
if (leftExpected == null || rightExpected == null) {
return null;
}
TypeAffinity leftAffinity = left.getAffinity();
TypeAffinity rightAffinity = right.getAffinity();
return operation.applyOperand(leftExpected, leftAffinity, rightExpected, rightAffinity, left, right,
operation.shouldApplyAffinity());
}
public static BinaryComparisonOperation create(SQLite3Expression leftVal, SQLite3Expression rightVal,
BinaryComparisonOperator op) {
return new BinaryComparisonOperation(leftVal, rightVal, op);
}
public enum BinaryComparisonOperator {
SMALLER("<") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
return left.applyLess(right, collate);
}
},
SMALLER_EQUALS("<=") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
SQLite3Constant lessThan = left.applyLess(right, collate);
if (lessThan == null) {
return null;
}
if (lessThan.getDataType() == SQLite3DataType.INT && lessThan.asInt() == 0) {
return left.applyEquals(right, collate);
} else {
return lessThan;
}
}
},
GREATER(">") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
SQLite3Constant equals = left.applyEquals(right, collate);
if (equals == null) {
return null;
}
if (equals.getDataType() == SQLite3DataType.INT && equals.asInt() == 1) {
return SQLite3Constant.createFalse();
} else {
SQLite3Constant applyLess = left.applyLess(right, collate);
if (applyLess == null) {
return null;
}
return UnaryOperator.NOT.apply(applyLess);
}
}
},
GREATER_EQUALS(">=") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
SQLite3Constant equals = left.applyEquals(right, collate);
if (equals == null) {
return null;
}
if (equals.getDataType() == SQLite3DataType.INT && equals.asInt() == 1) {
return SQLite3Constant.createTrue();
} else {
SQLite3Constant applyLess = left.applyLess(right, collate);
if (applyLess == null) {
return null;
}
return UnaryOperator.NOT.apply(applyLess);
}
}
},
EQUALS("=", "==") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
return left.applyEquals(right, collate);
}
},
NOT_EQUALS("!=", "<>") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
if (left == null || right == null) {
return null;
}
if (left.isNull() || right.isNull()) {
return SQLite3Constant.createNullConstant();
} else {
SQLite3Constant applyEquals = left.applyEquals(right, collate);
if (applyEquals == null) {
return null;
}
boolean equals = applyEquals.asInt() == 1;
return SQLite3Constant.createBoolean(!equals);
}
}
},
IS("IS") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
if (left == null || right == null) {
return null;
} else if (left.isNull()) {
return SQLite3Constant.createBoolean(right.isNull());
} else if (right.isNull()) {
return SQLite3Constant.createFalse();
} else {
return left.applyEquals(right, collate);
}
}
},
IS_NOT("IS NOT") {
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
if (left == null || right == null) {
return null;
} else if (left.isNull()) {
return SQLite3Constant.createBoolean(!right.isNull());
} else if (right.isNull()) {
return SQLite3Constant.createTrue();
} else {
SQLite3Constant applyEquals = left.applyEquals(right, collate);
if (applyEquals == null) {
return null;
}
boolean equals = applyEquals.asInt() == 1;
return SQLite3Constant.createBoolean(!equals);
}
}
},
LIKE("LIKE") {
@Override
public boolean shouldApplyAffinity() {
return false;
}
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
if (left == null || right == null) {
return null;
}
if (left.isNull() || right.isNull()) {
return SQLite3Constant.createNullConstant();
}
SQLite3Constant leftStr = SQLite3Cast.castToText(left);
SQLite3Constant rightStr = SQLite3Cast.castToText(right);
if (leftStr == null || rightStr == null) {
return null;
}
boolean val = LikeImplementationHelper.match(leftStr.asString(), rightStr.asString(), 0, 0, false);
return SQLite3Constant.createBoolean(val);
}
},
GLOB("GLOB") {
@Override
public boolean shouldApplyAffinity() {
return false;
}
@Override
SQLite3Constant apply(SQLite3Constant left, SQLite3Constant right, SQLite3CollateSequence collate) {
if (left == null || right == null) {
return null;
}
if (left.isNull() || right.isNull()) {
return SQLite3Constant.createNullConstant();
}
SQLite3Constant leftStr = SQLite3Cast.castToText(left);
SQLite3Constant rightStr = SQLite3Cast.castToText(right);
if (leftStr == null || rightStr == null) {
return null;
}
boolean val = match(leftStr.asString(), rightStr.asString(), 0, 0);
return SQLite3Constant.createBoolean(val);
}
private boolean match(String str, String regex, int regexPosition, int strPosition) {
if (strPosition == str.length() && regexPosition == regex.length()) {
return true;
}
if (regexPosition >= regex.length()) {
return false;
}
char cur = regex.charAt(regexPosition);
if (strPosition >= str.length()) {
if (cur == '*') {
return match(str, regex, regexPosition + 1, strPosition);
} else {
return false;
}
}
switch (cur) {
case '[':
int endingBrackets = regexPosition;
do {
endingBrackets++;
if (endingBrackets >= regex.length()) {
return false;
}
} while (regex.charAt(endingBrackets) != ']');
StringBuilder patternInBrackets = new StringBuilder(
regex.substring(regexPosition + 1, endingBrackets));
boolean inverted;
if (patternInBrackets.toString().startsWith("^")) {
if (patternInBrackets.length() > 1) {
inverted = true;
patternInBrackets = new StringBuilder(patternInBrackets.substring(1));
} else {
return false;
}
} else {
inverted = false;
}
int currentSearchIndex = 0;
boolean found = false;
do {
int minusPosition = patternInBrackets.toString().indexOf('-', currentSearchIndex);
boolean minusAtBoundaries = minusPosition == 0
|| minusPosition == patternInBrackets.length() - 1;
if (minusPosition == -1 || minusAtBoundaries) {
break;
}
found = true;
StringBuilder expandedPattern = new StringBuilder();
for (char start = patternInBrackets.charAt(minusPosition - 1); start < patternInBrackets
.charAt(minusPosition + 1); start += 1) {
expandedPattern.append(start);
}
patternInBrackets.replace(minusPosition, minusPosition + 1, expandedPattern.toString());
currentSearchIndex = minusPosition + expandedPattern.length();
} while (found);
if (patternInBrackets.length() > 0) {
char textChar = str.charAt(strPosition);
boolean contains = patternInBrackets.toString().contains(Character.toString(textChar));
if (contains && !inverted || !contains && inverted) {