-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLite3Constant.java
More file actions
625 lines (517 loc) · 19.7 KB
/
SQLite3Constant.java
File metadata and controls
625 lines (517 loc) · 19.7 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
package sqlancer.sqlite3.ast;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sqlancer.Randomly;
import sqlancer.sqlite3.SQLite3Visitor;
import sqlancer.sqlite3.schema.SQLite3DataType;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
public abstract class SQLite3Constant extends SQLite3Expression {
public static class SQLite3NullConstant extends SQLite3Constant {
@Override
public boolean isNull() {
return true;
}
@Override
public Object getValue() {
return null;
}
@Override
public SQLite3DataType getDataType() {
return SQLite3DataType.NULL;
}
@Override
public SQLite3Constant applyEquals(SQLite3Constant right, SQLite3CollateSequence collate) {
return SQLite3Constant.createNullConstant();
}
@Override
public SQLite3Constant applyNumericAffinity() {
return this;
}
@Override
public SQLite3Constant applyTextAffinity() {
return this;
}
@Override
String getStringRepresentation() {
return "NULL";
}
@Override
public SQLite3Constant castToBoolean() {
return SQLite3Cast.asBoolean(this);
}
@Override
public SQLite3Constant applyLess(SQLite3Constant right, SQLite3CollateSequence collate) {
return SQLite3Constant.createNullConstant();
}
}
public static class SQLite3IntConstant extends SQLite3Constant {
private final long value;
private final boolean isHex;
public SQLite3IntConstant(long value, boolean isHex) {
this.value = value;
this.isHex = isHex;
}
public SQLite3IntConstant(long value) {
this.value = value;
this.isHex = false;
}
@Override
public boolean isHex() {
return isHex;
}
@Override
public boolean isNull() {
return false;
}
@Override
public long asInt() {
return value;
}
@Override
public Object getValue() {
return value;
}
@Override
public SQLite3DataType getDataType() {
return SQLite3DataType.INT;
}
@Override
public SQLite3Constant applyEquals(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right instanceof SQLite3RealConstant) {
if (Double.isInfinite(right.asDouble())) {
return SQLite3Constant.createFalse();
}
BigDecimal otherColumnValue = BigDecimal.valueOf(right.asDouble());
BigDecimal thisColumnValue = BigDecimal.valueOf(value);
return SQLite3Constant.createBoolean(thisColumnValue.compareTo(otherColumnValue) == 0);
} else if (right instanceof SQLite3IntConstant) {
return SQLite3Constant.createBoolean(value == right.asInt());
} else if (right instanceof SQLite3NullConstant) {
return SQLite3Constant.createNullConstant();
} else {
return SQLite3Constant.createFalse();
}
}
@Override
public SQLite3Constant applyNumericAffinity() {
return this;
}
@Override
public SQLite3Constant applyTextAffinity() {
return SQLite3Constant.createTextConstant(String.valueOf(value));
}
@Override
String getStringRepresentation() {
return String.valueOf(value);
}
@Override
public SQLite3Constant castToBoolean() {
return SQLite3Cast.asBoolean(this);
}
@Override
public SQLite3Constant applyLess(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right.isNull()) {
return right;
} else if (right.getDataType() == SQLite3DataType.TEXT || right.getDataType() == SQLite3DataType.BINARY) {
return SQLite3Constant.createTrue();
} else if (right.getDataType() == SQLite3DataType.INT) {
long rightValue = right.asInt();
return SQLite3Constant.createBoolean(value < rightValue);
} else {
if (Double.POSITIVE_INFINITY == right.asDouble()) {
return SQLite3Constant.createTrue();
} else if (Double.NEGATIVE_INFINITY == right.asDouble()) {
return SQLite3Constant.createFalse();
}
assert right.getDataType() == SQLite3DataType.REAL;
BigDecimal otherColumnValue = BigDecimal.valueOf(right.asDouble());
BigDecimal thisColumnValue = BigDecimal.valueOf(value);
return SQLite3Constant.createBoolean(thisColumnValue.compareTo(otherColumnValue) < 0);
}
}
}
public static class SQLite3RealConstant extends SQLite3Constant {
private final double value;
public SQLite3RealConstant(double value) {
this.value = value;
}
@Override
public boolean isNull() {
return false;
}
@Override
public double asDouble() {
return value;
}
@Override
public Object getValue() {
return value;
}
@Override
public SQLite3DataType getDataType() {
return SQLite3DataType.REAL;
}
@Override
public SQLite3Constant applyEquals(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right instanceof SQLite3RealConstant) {
return SQLite3Constant.createBoolean(value == right.asDouble());
} else if (right instanceof SQLite3IntConstant) {
if (Double.isInfinite(value)) {
return SQLite3Constant.createFalse();
}
BigDecimal thisColumnValue = BigDecimal.valueOf(value);
BigDecimal otherColumnValue = BigDecimal.valueOf(right.asInt());
return SQLite3Constant.createBoolean(thisColumnValue.compareTo(otherColumnValue) == 0);
} else if (right instanceof SQLite3NullConstant) {
return SQLite3Constant.createNullConstant();
} else {
return SQLite3Constant.createFalse();
}
}
@Override
public SQLite3Constant applyNumericAffinity() {
return this;
}
@Override
public SQLite3Constant applyTextAffinity() {
return SQLite3Cast.castToText(this);
}
@Override
String getStringRepresentation() {
return String.valueOf(value);
}
@Override
public SQLite3Constant castToBoolean() {
return SQLite3Cast.asBoolean(this);
}
@Override
public SQLite3Constant applyLess(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right.isNull()) {
return right;
} else if (right.getDataType() == SQLite3DataType.TEXT || right.getDataType() == SQLite3DataType.BINARY) {
return SQLite3Constant.createTrue();
} else if (right.getDataType() == SQLite3DataType.REAL) {
double rightValue = right.asDouble();
return SQLite3Constant.createBoolean(value < rightValue);
} else {
if (Double.POSITIVE_INFINITY == value) {
return SQLite3Constant.createFalse();
} else if (Double.NEGATIVE_INFINITY == value) {
return SQLite3Constant.createTrue();
}
assert right.getDataType() == SQLite3DataType.INT;
BigDecimal otherColumnValue = BigDecimal.valueOf(right.asInt());
BigDecimal thisColumnValue = BigDecimal.valueOf(value);
return SQLite3Constant.createBoolean(thisColumnValue.compareTo(otherColumnValue) < 0);
}
}
}
public static class SQLite3TextConstant extends SQLite3Constant {
private final String text;
public SQLite3TextConstant(String text) {
this.text = text;
}
@Override
public boolean isNull() {
return false;
}
@Override
public String asString() {
return text;
}
@Override
public Object getValue() {
return text;
}
@Override
public SQLite3DataType getDataType() {
return SQLite3DataType.TEXT;
}
@Override
public SQLite3Constant applyEquals(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right.isNull()) {
return SQLite3Constant.createNullConstant();
} else if (right instanceof SQLite3TextConstant) {
String other = right.asString();
boolean equals;
switch (collate) {
case BINARY:
equals = text.equals(other);
break;
case NOCASE:
equals = toLower(text).equals(toLower(other));
break;
case RTRIM:
equals = trimTrailing(text).equals(trimTrailing(other));
break;
default:
throw new AssertionError(collate);
}
return SQLite3Constant.createBoolean(equals);
} else {
return SQLite3Constant.createFalse();
}
}
public static String toLower(String t) {
StringBuilder text = new StringBuilder(t);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 'A' && c <= 'Z') {
text.setCharAt(i, Character.toLowerCase(c));
}
}
return text.toString();
}
public static String toUpper(String t) {
StringBuilder text = new StringBuilder(t);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 'a' && c <= 'z') {
text.setCharAt(i, Character.toUpperCase(c));
}
}
return text.toString();
}
public static String trim(String str) {
return trimLeading(trimTrailing(str));
}
public static String trimLeading(String str) {
if (str != null) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {
return str.substring(i);
}
}
}
return "";
}
public static String trimTrailing(String str) {
if (str != null) {
for (int i = str.length() - 1; i >= 0; --i) {
if (str.charAt(i) != ' ') {
return str.substring(0, i + 1);
}
}
}
return "";
}
@Override
public SQLite3Constant applyNumericAffinity() {
Pattern leadingDigitPattern = Pattern
.compile("[-+]?((\\d(\\d)*(\\.(\\d)*)?)|\\.(\\d)(\\d)*)([Ee][+-]?(\\d)(\\d)*)?");
String trimmedString = text.trim();
if (trimmedString.isEmpty()) {
return this;
}
Matcher matcher = leadingDigitPattern.matcher(trimmedString);
if (matcher.matches()) {
return SQLite3Cast.castToNumeric(this);
} else {
return this;
}
}
@Override
public SQLite3Constant applyTextAffinity() {
return this;
}
@Override
String getStringRepresentation() {
return text;
}
@Override
public SQLite3Constant castToBoolean() {
return SQLite3Cast.asBoolean(this);
}
@Override
public SQLite3Constant applyLess(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right.isNull()) {
return right;
} else if (right.getDataType() == SQLite3DataType.BINARY) {
return SQLite3Constant.createTrue();
} else if (right.getDataType() == SQLite3DataType.TEXT) {
String other = right.asString();
boolean lessThan;
switch (collate) {
case BINARY:
lessThan = text.compareTo(other) < 0;
break;
case NOCASE:
lessThan = toLower(text).compareTo(toLower(other)) < 0;
break;
case RTRIM:
lessThan = trimTrailing(text).compareTo(trimTrailing(other)) < 0;
break;
default:
throw new AssertionError(collate);
}
return SQLite3Constant.createBoolean(lessThan);
} else {
assert right.getDataType() == SQLite3DataType.REAL || right.getDataType() == SQLite3DataType.INT;
return SQLite3Constant.createFalse();
}
}
}
public static class SQLite3BinaryConstant extends SQLite3Constant {
private final byte[] bytes;
public SQLite3BinaryConstant(byte[] bytes) {
this.bytes = bytes.clone();
}
@Override
public boolean isNull() {
return false;
}
@Override
public SQLite3DataType getDataType() {
return SQLite3DataType.BINARY;
}
@Override
public Object getValue() {
return bytes;
}
@Override
public byte[] asBinary() {
return bytes.clone();
}
@Override
public SQLite3Constant applyNumericAffinity() {
return this;
}
@Override
public SQLite3Constant applyTextAffinity() {
return this;
/*
* if (bytes.length == 0) { return this; } else { StringBuilder sb = new StringBuilder(); for (byte b :
* bytes) { if (isPrintableChar(b)) { sb.append((char) b); } } return
* SQLite3Constant.createTextConstant(sb.toString()); }
*/
}
public boolean isPrintableChar(byte b) {
return Math.abs(b) >= 32;
}
@Override
String getStringRepresentation() {
String hexRepr = SQLite3Visitor.byteArrayToHex(bytes);
return String.format("x'%s'", hexRepr);
}
@Override
public SQLite3Constant applyEquals(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right.isNull()) {
return SQLite3Constant.createNullConstant();
} else if (right.getDataType() == SQLite3DataType.BINARY) {
byte[] otherArr = right.asBinary();
if (bytes.length == otherArr.length) {
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] != otherArr[i]) {
return SQLite3Constant.createFalse();
}
}
return SQLite3Constant.createTrue();
} else {
return SQLite3Constant.createFalse();
}
} else {
return SQLite3Constant.createFalse();
}
}
@Override
public SQLite3Constant castToBoolean() {
return SQLite3Cast.asBoolean(this);
}
@Override
public SQLite3Constant applyLess(SQLite3Constant right, SQLite3CollateSequence collate) {
if (right.isNull()) {
return right;
} else if (right.getDataType() == SQLite3DataType.TEXT || right.getDataType() == SQLite3DataType.INT
|| right.getDataType() == SQLite3DataType.REAL) {
return SQLite3Constant.createFalse();
} else {
byte[] otherArr = right.asBinary();
int minLength = Math.min(bytes.length, otherArr.length);
for (int i = 0; i < minLength; i++) {
if (bytes[i] != otherArr[i]) {
return SQLite3Constant.createBoolean((bytes[i] & 0xff) < (otherArr[i] & 0xff));
} else if (bytes[i] > otherArr[i]) {
return SQLite3Constant.createFalse();
}
}
return SQLite3Constant.createBoolean(bytes.length < otherArr.length);
}
}
}
abstract String getStringRepresentation();
public abstract boolean isNull();
public abstract Object getValue();
public boolean isHex() {
throw new UnsupportedOperationException(this.getDataType().toString());
}
public long asInt() {
throw new UnsupportedOperationException(this.getDataType().toString());
}
public double asDouble() {
throw new UnsupportedOperationException(this.getDataType().toString());
}
public byte[] asBinary() {
throw new UnsupportedOperationException(this.getDataType().toString());
}
public String asString() {
throw new UnsupportedOperationException(this.getDataType().toString());
}
public abstract SQLite3DataType getDataType();
public static SQLite3Constant createIntConstant(long val) {
return new SQLite3IntConstant(val);
}
public static SQLite3Constant createIntConstant(long val, boolean isHex) {
return new SQLite3IntConstant(val, isHex);
}
public static SQLite3Constant createBinaryConstant(byte[] val) {
return new SQLite3BinaryConstant(val);
}
public static SQLite3Constant createBinaryConstant(String val) {
return new SQLite3BinaryConstant(SQLite3Visitor.hexStringToByteArray(val));
}
public static SQLite3Constant createRealConstant(double real) {
return new SQLite3RealConstant(real);
}
public static SQLite3Constant createTextConstant(String text) {
return new SQLite3TextConstant(text);
}
public static SQLite3Constant createNullConstant() {
return new SQLite3NullConstant();
}
public static SQLite3Constant getRandomBinaryConstant(Randomly r) {
return new SQLite3BinaryConstant(r.getBytes());
}
@Override
public SQLite3Constant getExpectedValue() {
return this;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
@Override
public String toString() {
return String.format("(%s) %s", getDataType(), getStringRepresentation());
}
public abstract SQLite3Constant applyNumericAffinity();
public abstract SQLite3Constant applyTextAffinity();
public static SQLite3Constant createTrue() {
return new SQLite3Constant.SQLite3IntConstant(1);
}
public static SQLite3Constant createFalse() {
return new SQLite3Constant.SQLite3IntConstant(0);
}
public static SQLite3Constant createBoolean(boolean tr) {
return new SQLite3Constant.SQLite3IntConstant(tr ? 1 : 0);
}
public abstract SQLite3Constant applyEquals(SQLite3Constant right, SQLite3CollateSequence collate);
public abstract SQLite3Constant applyLess(SQLite3Constant right, SQLite3CollateSequence collate);
public abstract SQLite3Constant castToBoolean();
public SQLite3Constant applyEquals(SQLite3Constant right) {
return applyEquals(right, SQLite3CollateSequence.BINARY);
}
public boolean isReal() {
return getDataType() == SQLite3DataType.REAL;
}
}