forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH2Provider.java
More file actions
111 lines (94 loc) · 3.7 KB
/
H2Provider.java
File metadata and controls
111 lines (94 loc) · 3.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
package sqlancer.h2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import sqlancer.AbstractAction;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.SQLGlobalState;
import sqlancer.SQLProviderAdapter;
import sqlancer.StatementExecutor;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLQueryProvider;
import sqlancer.h2.H2Provider.H2GlobalState;
public class H2Provider extends SQLProviderAdapter<H2GlobalState, H2Options> {
public H2Provider() {
super(H2GlobalState.class, H2Options.class);
}
public enum Action implements AbstractAction<H2GlobalState> {
INSERT(H2InsertGenerator::getQuery), //
INDEX(H2IndexGenerator::getQuery), //
ANALYZE((g) -> new SQLQueryAdapter("ANALYZE")), //
CREATE_VIEW(H2ViewGenerator::getQuery), //
UPDATE(H2UpdateGenerator::getQuery), //
DELETE(H2DeleteGenerator::getQuery), //
SET(H2SetGenerator::getQuery);
private final SQLQueryProvider<H2GlobalState> sqlQueryProvider;
Action(SQLQueryProvider<H2GlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public SQLQueryAdapter getQuery(H2GlobalState state) throws Exception {
return sqlQueryProvider.getQuery(state);
}
}
private static int mapActions(H2GlobalState globalState, Action a) {
Randomly r = globalState.getRandomly();
switch (a) {
case INSERT:
return r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
case ANALYZE:
return r.getInteger(0, 5);
case INDEX:
case SET:
return r.getInteger(0, 5);
case CREATE_VIEW:
return r.getInteger(0, 2);
case UPDATE:
case DELETE:
return r.getInteger(0, 10);
default:
throw new AssertionError(a);
}
}
public static class H2GlobalState extends SQLGlobalState<H2Options, H2Schema> {
@Override
protected H2Schema readSchema() throws SQLException {
return H2Schema.fromConnection(getConnection(), getDatabaseName());
}
}
@Override
public void generateDatabase(H2GlobalState globalState) throws Exception {
if (Randomly.getBoolean()) {
H2SetGenerator.getQuery(globalState).execute(globalState);
}
boolean success;
for (int i = 0; i < Randomly.fromOptions(1, 2, 3); i++) {
do {
SQLQueryAdapter qt = new H2TableGenerator().getQuery(globalState);
success = globalState.executeStatement(qt);
} while (!success);
}
StatementExecutor<H2GlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
H2Provider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
se.executeStatements();
}
@Override
public SQLConnection createDatabase(H2GlobalState globalState) throws SQLException {
String connectionString = "jdbc:h2:~/" + globalState.getDatabaseName() + ";DB_CLOSE_ON_EXIT=FALSE";
Connection connection = DriverManager.getConnection(connectionString, "sa", "");
connection.createStatement().execute("DROP ALL OBJECTS DELETE FILES");
connection.close();
connection = DriverManager.getConnection(connectionString, "sa", "");
return new SQLConnection(connection);
}
@Override
public String getDBMSName() {
return "h2";
}
}