forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH2IndexGenerator.java
More file actions
56 lines (49 loc) · 1.78 KB
/
H2IndexGenerator.java
File metadata and controls
56 lines (49 loc) · 1.78 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
package sqlancer.h2;
import sqlancer.Randomly;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.h2.H2Provider.H2GlobalState;
import sqlancer.h2.H2Schema.H2Table;
public class H2IndexGenerator {
private final H2GlobalState globalState;
public H2IndexGenerator(H2GlobalState globalState) {
this.globalState = globalState;
}
public static SQLQueryAdapter getQuery(H2GlobalState globalState) {
return new H2IndexGenerator(globalState).generate();
}
private SQLQueryAdapter generate() {
ExpectedErrors errors = new ExpectedErrors();
StringBuilder sb = new StringBuilder();
sb.append("CREATE ");
if (Randomly.getBoolean()) {
sb.append("UNIQUE ");
errors.add("Unique index or primary key violation");
}
if (Randomly.getBoolean()) {
sb.append("HASH ");
}
sb.append("INDEX IF NOT EXISTS ");
sb.append(globalState.getSchema().getFreeIndexName());
sb.append(" ON ");
H2Table table = globalState.getSchema().getRandomTable(t -> !t.isView());
sb.append(table.getName());
sb.append('(');
for (int i = 0; i < Randomly.smallNumber() + 1; i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(table.getRandomColumn().getName());
if (Randomly.getBoolean()) {
sb.append(' ');
sb.append(Randomly.fromOptions("ASC", "DESC"));
}
if (Randomly.getBoolean()) {
sb.append(" NULLS ");
sb.append(Randomly.fromOptions("FIRST", "LAST"));
}
}
sb.append(')');
return new SQLQueryAdapter(sb.toString(), errors);
}
}