-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresConstant.java
More file actions
597 lines (480 loc) · 16.5 KB
/
PostgresConstant.java
File metadata and controls
597 lines (480 loc) · 16.5 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
package sqlancer.postgres.ast;
import java.math.BigDecimal;
import sqlancer.IgnoreMeException;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
public abstract class PostgresConstant implements PostgresExpression {
public abstract String getTextRepresentation();
public abstract String getUnquotedTextRepresentation();
public static class BooleanConstant extends PostgresConstant {
private final boolean value;
public BooleanConstant(boolean value) {
this.value = value;
}
@Override
public String getTextRepresentation() {
return value ? "TRUE" : "FALSE";
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.BOOLEAN;
}
@Override
public boolean asBoolean() {
return value;
}
@Override
public boolean isBoolean() {
return true;
}
@Override
public PostgresConstant isEquals(PostgresConstant rightVal) {
if (rightVal.isNull()) {
return PostgresConstant.createNullConstant();
} else if (rightVal.isBoolean()) {
return PostgresConstant.createBooleanConstant(value == rightVal.asBoolean());
} else if (rightVal.isString()) {
return PostgresConstant
.createBooleanConstant(value == rightVal.cast(PostgresDataType.BOOLEAN).asBoolean());
} else {
throw new AssertionError(rightVal);
}
}
@Override
protected PostgresConstant isLessThan(PostgresConstant rightVal) {
if (rightVal.isNull()) {
return PostgresConstant.createNullConstant();
} else if (rightVal.isString()) {
return isLessThan(rightVal.cast(PostgresDataType.BOOLEAN));
} else {
assert rightVal.isBoolean();
return PostgresConstant.createBooleanConstant((value ? 1 : 0) < (rightVal.asBoolean() ? 1 : 0));
}
}
@Override
public PostgresConstant cast(PostgresDataType type) {
switch (type) {
case BOOLEAN:
return this;
case INT:
return PostgresConstant.createIntConstant(value ? 1 : 0);
case TEXT:
return PostgresConstant.createTextConstant(value ? "true" : "false");
default:
return null;
}
}
@Override
public String getUnquotedTextRepresentation() {
return getTextRepresentation();
}
}
public static class PostgresNullConstant extends PostgresConstant {
@Override
public String getTextRepresentation() {
return "NULL";
}
@Override
public PostgresDataType getExpressionType() {
return null;
}
@Override
public boolean isNull() {
return true;
}
@Override
public PostgresConstant isEquals(PostgresConstant rightVal) {
return PostgresConstant.createNullConstant();
}
@Override
protected PostgresConstant isLessThan(PostgresConstant rightVal) {
return PostgresConstant.createNullConstant();
}
@Override
public PostgresConstant cast(PostgresDataType type) {
return PostgresConstant.createNullConstant();
}
@Override
public String getUnquotedTextRepresentation() {
return getTextRepresentation();
}
}
public static class StringConstant extends PostgresConstant {
private final String value;
public StringConstant(String value) {
this.value = value;
}
@Override
public String getTextRepresentation() {
return String.format("'%s'", value.replace("'", "''"));
}
@Override
public PostgresConstant isEquals(PostgresConstant rightVal) {
if (rightVal.isNull()) {
return PostgresConstant.createNullConstant();
} else if (rightVal.isInt()) {
return cast(PostgresDataType.INT).isEquals(rightVal.cast(PostgresDataType.INT));
} else if (rightVal.isBoolean()) {
return cast(PostgresDataType.BOOLEAN).isEquals(rightVal.cast(PostgresDataType.BOOLEAN));
} else if (rightVal.isString()) {
return PostgresConstant.createBooleanConstant(value.contentEquals(rightVal.asString()));
} else {
throw new AssertionError(rightVal);
}
}
@Override
protected PostgresConstant isLessThan(PostgresConstant rightVal) {
if (rightVal.isNull()) {
return PostgresConstant.createNullConstant();
} else if (rightVal.isInt()) {
return cast(PostgresDataType.INT).isLessThan(rightVal.cast(PostgresDataType.INT));
} else if (rightVal.isBoolean()) {
return cast(PostgresDataType.BOOLEAN).isLessThan(rightVal.cast(PostgresDataType.BOOLEAN));
} else if (rightVal.isString()) {
return PostgresConstant.createBooleanConstant(value.compareTo(rightVal.asString()) < 0);
} else {
throw new AssertionError(rightVal);
}
}
@Override
public PostgresConstant cast(PostgresDataType type) {
if (type == PostgresDataType.TEXT) {
return this;
}
String s = value.trim();
switch (type) {
case BOOLEAN:
try {
return PostgresConstant.createBooleanConstant(Long.parseLong(s) != 0);
} catch (NumberFormatException e) {
}
switch (s.toUpperCase()) {
case "T":
case "TR":
case "TRU":
case "TRUE":
case "1":
case "YES":
case "YE":
case "Y":
case "ON":
return PostgresConstant.createTrue();
case "F":
case "FA":
case "FAL":
case "FALS":
case "FALSE":
case "N":
case "NO":
case "OF":
case "OFF":
default:
return PostgresConstant.createFalse();
}
case INT:
try {
return PostgresConstant.createIntConstant(Long.parseLong(s));
} catch (NumberFormatException e) {
return PostgresConstant.createIntConstant(-1);
}
case TEXT:
return this;
default:
return null;
}
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.TEXT;
}
@Override
public boolean isString() {
return true;
}
@Override
public String asString() {
return value;
}
@Override
public String getUnquotedTextRepresentation() {
return value;
}
}
public static class IntConstant extends PostgresConstant {
private final long val;
public IntConstant(long val) {
this.val = val;
}
@Override
public String getTextRepresentation() {
return String.valueOf(val);
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.INT;
}
@Override
public long asInt() {
return val;
}
@Override
public boolean isInt() {
return true;
}
@Override
public PostgresConstant isEquals(PostgresConstant rightVal) {
if (rightVal.isNull()) {
return PostgresConstant.createNullConstant();
} else if (rightVal.isBoolean()) {
return cast(PostgresDataType.BOOLEAN).isEquals(rightVal);
} else if (rightVal.isInt()) {
return PostgresConstant.createBooleanConstant(val == rightVal.asInt());
} else if (rightVal.isString()) {
return PostgresConstant.createBooleanConstant(val == rightVal.cast(PostgresDataType.INT).asInt());
} else {
throw new AssertionError(rightVal);
}
}
@Override
protected PostgresConstant isLessThan(PostgresConstant rightVal) {
if (rightVal.isNull()) {
return PostgresConstant.createNullConstant();
} else if (rightVal.isInt()) {
return PostgresConstant.createBooleanConstant(val < rightVal.asInt());
} else if (rightVal.isBoolean()) {
throw new AssertionError(rightVal);
} else if (rightVal.isString()) {
return PostgresConstant.createBooleanConstant(val < rightVal.cast(PostgresDataType.INT).asInt());
} else {
throw new IgnoreMeException();
}
}
@Override
public PostgresConstant cast(PostgresDataType type) {
switch (type) {
case BOOLEAN:
return PostgresConstant.createBooleanConstant(val != 0);
case INT:
return this;
case TEXT:
return PostgresConstant.createTextConstant(String.valueOf(val));
default:
return null;
}
}
@Override
public String getUnquotedTextRepresentation() {
return getTextRepresentation();
}
}
public static PostgresConstant createNullConstant() {
return new PostgresNullConstant();
}
public String asString() {
throw new UnsupportedOperationException(this.toString());
}
public boolean isString() {
return false;
}
public static PostgresConstant createIntConstant(long val) {
return new IntConstant(val);
}
public static PostgresConstant createBooleanConstant(boolean val) {
return new BooleanConstant(val);
}
@Override
public PostgresConstant getExpectedValue() {
return this;
}
public boolean isNull() {
return false;
}
public boolean asBoolean() {
throw new UnsupportedOperationException(this.toString());
}
public static PostgresConstant createFalse() {
return createBooleanConstant(false);
}
public static PostgresConstant createTrue() {
return createBooleanConstant(true);
}
public long asInt() {
throw new UnsupportedOperationException(this.toString());
}
public boolean isBoolean() {
return false;
}
public abstract PostgresConstant isEquals(PostgresConstant rightVal);
public boolean isInt() {
return false;
}
protected abstract PostgresConstant isLessThan(PostgresConstant rightVal);
@Override
public String toString() {
return getTextRepresentation();
}
public abstract PostgresConstant cast(PostgresDataType type);
public static PostgresConstant createTextConstant(String string) {
return new StringConstant(string);
}
public abstract static class PostgresConstantBase extends PostgresConstant {
@Override
public String getUnquotedTextRepresentation() {
return null;
}
@Override
public PostgresConstant isEquals(PostgresConstant rightVal) {
return null;
}
@Override
protected PostgresConstant isLessThan(PostgresConstant rightVal) {
return null;
}
@Override
public PostgresConstant cast(PostgresDataType type) {
return null;
}
}
public static class DecimalConstant extends PostgresConstantBase {
private final BigDecimal val;
public DecimalConstant(BigDecimal val) {
this.val = val;
}
@Override
public String getTextRepresentation() {
return String.valueOf(val);
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.DECIMAL;
}
}
public static class InetConstant extends PostgresConstantBase {
private final String val;
public InetConstant(String val) {
this.val = "'" + val + "'";
}
@Override
public String getTextRepresentation() {
return String.valueOf(val);
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.INET;
}
}
public static class FloatConstant extends PostgresConstantBase {
private final float val;
public FloatConstant(float val) {
this.val = val;
}
@Override
public String getTextRepresentation() {
if (Double.isFinite(val)) {
return String.valueOf(val);
} else {
return "'" + val + "'";
}
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.FLOAT;
}
}
public static class DoubleConstant extends PostgresConstantBase {
private final double val;
public DoubleConstant(double val) {
this.val = val;
}
@Override
public String getTextRepresentation() {
if (Double.isFinite(val)) {
return String.valueOf(val);
} else {
return "'" + val + "'";
}
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.FLOAT;
}
}
public static class BitConstant extends PostgresConstantBase {
private final long val;
public BitConstant(long val) {
this.val = val;
}
@Override
public String getTextRepresentation() {
return String.format("B'%s'", Long.toBinaryString(val));
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.BIT;
}
}
public static class RangeConstant extends PostgresConstantBase {
private final long left;
private final boolean leftIsInclusive;
private final long right;
private final boolean rightIsInclusive;
public RangeConstant(long left, boolean leftIsInclusive, long right, boolean rightIsInclusive) {
this.left = left;
this.leftIsInclusive = leftIsInclusive;
this.right = right;
this.rightIsInclusive = rightIsInclusive;
}
@Override
public String getTextRepresentation() {
StringBuilder sb = new StringBuilder();
sb.append("'");
if (leftIsInclusive) {
sb.append("[");
} else {
sb.append("(");
}
sb.append(left);
sb.append(",");
sb.append(right);
if (rightIsInclusive) {
sb.append("]");
} else {
sb.append(")");
}
sb.append("'");
sb.append("::int4range");
return sb.toString();
}
@Override
public PostgresDataType getExpressionType() {
return PostgresDataType.RANGE;
}
}
public static PostgresConstant createDecimalConstant(BigDecimal bigDecimal) {
return new DecimalConstant(bigDecimal);
}
public static PostgresConstant createFloatConstant(float val) {
return new FloatConstant(val);
}
public static PostgresConstant createDoubleConstant(double val) {
return new DoubleConstant(val);
}
public static PostgresConstant createRange(long left, boolean leftIsInclusive, long right,
boolean rightIsInclusive) {
long realLeft;
long realRight;
if (left > right) {
realRight = left;
realLeft = right;
} else {
realLeft = left;
realRight = right;
}
return new RangeConstant(realLeft, leftIsInclusive, realRight, rightIsInclusive);
}
public static PostgresExpression createBitConstant(long integer) {
return new BitConstant(integer);
}
public static PostgresExpression createInetConstant(String val) {
return new InetConstant(val);
}
}