forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCockroachDBConstant.java
More file actions
260 lines (192 loc) · 7.45 KB
/
CockroachDBConstant.java
File metadata and controls
260 lines (192 loc) · 7.45 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
package sqlancer.cockroachdb.ast;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.cockroachdb.CockroachDBVisitor;
public class CockroachDBConstant implements CockroachDBExpression {
private CockroachDBConstant() {
}
public static class CockroachDBNullConstant extends CockroachDBConstant {
@Override
public String toString() {
return "NULL";
}
}
public static class CockroachDBIntConstant extends CockroachDBConstant {
private final long value;
public CockroachDBIntConstant(long value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public long getValue() {
return value;
}
}
public static class CockroachDBDoubleConstant extends CockroachDBConstant {
private final double value;
public CockroachDBDoubleConstant(double value) {
this.value = value;
}
public double getValue() {
return value;
}
@Override
public String toString() {
if (value == Double.POSITIVE_INFINITY) {
return "FLOAT '+Inf'";
} else if (value == Double.NEGATIVE_INFINITY) {
return "FLOAT '-Inf'";
}
return String.valueOf(value);
}
}
public static class CockroachDBTextConstant extends CockroachDBConstant {
private final String value;
public CockroachDBTextConstant(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "'" + value.replace("'", "''") + "'";
}
}
public static class CockroachDBBitConstant extends CockroachDBConstant {
private final String value;
public CockroachDBBitConstant(long value) {
this.value = Long.toBinaryString(value);
}
public CockroachDBBitConstant(String binaryString) {
this.value = binaryString;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "B'" + value + "'";
}
}
public static class CockroachDBBooleanConstant extends CockroachDBConstant {
private final boolean value;
public CockroachDBBooleanConstant(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
}
public static class CockroachDBArrayConstant extends CockroachDBConstant {
private final List<CockroachDBExpression> elements;
public CockroachDBArrayConstant(List<CockroachDBExpression> elements) {
this.elements = elements;
}
public List<CockroachDBExpression> getElements() {
return elements;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("ARRAY[");
for (int i = 0; i < elements.size(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(CockroachDBVisitor.asString(elements.get(i)));
}
sb.append("]");
return sb.toString();
}
}
public static class CockroachDBIntervalConstant extends CockroachDBConstant {
private final long year;
private final long month;
private final long day;
private final long hour;
private final long minute;
private final long second;
public CockroachDBIntervalConstant(long year, long month, long day, long hour, long minute, long second) {
this.year = year;
this.month = month;
this.day = day;
this.hour = hour;
this.minute = minute;
this.second = second;
}
@Override
public String toString() {
return String.valueOf(String.format("(INTERVAL '%d year %d months %d days %d hours %d minutes %d seconds')",
year, month, day, hour, minute, second));
}
}
public static class CockroachDBTimeRelatedConstant extends CockroachDBConstant {
private final String textRepr;
private final String typeRepresentation;
public CockroachDBTimeRelatedConstant(String typeRepresentation, long val, String format) {
this.typeRepresentation = typeRepresentation;
Timestamp timestamp = new Timestamp(val);
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
textRepr = dateFormat.format(timestamp);
}
@Override
public String toString() {
return String.format("%s '%s'", typeRepresentation, textRepr);
}
}
public static CockroachDBTextConstant createStringConstant(String text) {
return new CockroachDBTextConstant(text);
}
public static CockroachDBDoubleConstant createFloatConstant(double val) {
return new CockroachDBDoubleConstant(val);
}
public static CockroachDBIntConstant createIntConstant(long val) {
return new CockroachDBIntConstant(val);
}
public static CockroachDBNullConstant createNullConstant() {
return new CockroachDBNullConstant();
}
public static CockroachDBConstant createBooleanConstant(boolean val) {
return new CockroachDBBooleanConstant(val);
}
public static CockroachDBExpression createBitConstant(long integer) {
return new CockroachDBBitConstant(integer);
}
public static CockroachDBExpression createBitConstantWithSize(int size) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append(Randomly.getBoolean() ? 0 : 1);
}
return new CockroachDBBitConstant(sb.toString());
}
public static CockroachDBExpression createTimestampConstant(long integer) {
return new CockroachDBTimeRelatedConstant("TIMESTAMP", integer, "yyyy-MM-dd'T'HH:mm:ss");
}
public static CockroachDBExpression createTimeConstant(long integer) {
return new CockroachDBTimeRelatedConstant("TIME", integer, "yyyy-MM-dd'T'HH:mm:ss");
}
public static CockroachDBExpression createTimetz(long integer) {
return new CockroachDBTimeRelatedConstant("TIMETZ", integer, "yyyy-MM-dd'T'HH:mm:ss"); // TODO: support the
// complete format
}
public static CockroachDBExpression createTimestamptzConstant(long integer) {
return new CockroachDBTimeRelatedConstant("TIMESTAMPTZ", integer, "yyyy-MM-dd'T'HH:mm:ss"); // TODO: support the
// complete
// format
}
public static CockroachDBExpression createIntervalConstant(long year, long month, long day, long hour, long minute,
long second) {
return new CockroachDBIntervalConstant(year, month, day, hour, minute, second);
}
public static CockroachDBExpression createArrayConstant(List<CockroachDBExpression> elements) {
return new CockroachDBArrayConstant(elements);
}
}