-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresOrderByTerm.java
More file actions
79 lines (64 loc) · 2.09 KB
/
PostgresOrderByTerm.java
File metadata and controls
79 lines (64 loc) · 2.09 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
package sqlancer.postgres.ast;
import sqlancer.Randomly;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
public class PostgresOrderByTerm implements PostgresExpression {
private final PostgresExpression expr;
private final PostgresOrder order;
private final int limit;
private final boolean ties;
public enum PostgresOrder {
ASC, DESC;
public static PostgresOrder getRandomOrder() {
return Randomly.fromOptions(PostgresOrder.values());
}
}
public PostgresOrderByTerm(PostgresExpression expr, PostgresOrder order) {
if (expr == null) {
throw new IllegalArgumentException("Expression cannot be null");
}
this.expr = expr;
this.order = order;
if (Randomly.getBooleanWithRatherLowProbability()) {
this.limit = (int) Randomly.getPositiveOrZeroNonCachedInteger();
this.ties = true;
} else {
this.limit = 0;
this.ties = false;
}
}
// Constructor for window functions, might be removed in the future to have only one constructor
public PostgresOrderByTerm(PostgresExpression expr, boolean ascending) {
if (expr == null) {
throw new IllegalArgumentException("Expression cannot be null");
}
this.expr = expr;
this.order = ascending ? PostgresOrder.ASC : PostgresOrder.DESC;
this.limit = 0;
this.ties = false;
}
public PostgresExpression getExpr() {
return expr;
}
public PostgresOrder getOrder() {
return order;
}
public boolean isAscending() {
return order == PostgresOrder.ASC;
}
@Override
public PostgresConstant getExpectedValue() {
throw new AssertionError(this);
}
@Override
public PostgresDataType getExpressionType() {
return null;
}
@Override
public String toString() {
if (ties) {
return String.format("%s %s FETCH FIRST %d WITH TIES", expr, order, limit);
} else {
return String.format("%s %s", expr, order);
}
}
}