forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHSQLDBProvider.java
More file actions
110 lines (93 loc) · 4.02 KB
/
HSQLDBProvider.java
File metadata and controls
110 lines (93 loc) · 4.02 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
package sqlancer.hsqldb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.google.auto.service.AutoService;
import sqlancer.AbstractAction;
import sqlancer.DatabaseProvider;
import sqlancer.IgnoreMeException;
import sqlancer.MainOptions;
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.hsqldb.gen.HSQLDBInsertGenerator;
import sqlancer.hsqldb.gen.HSQLDBTableGenerator;
import sqlancer.hsqldb.gen.HSQLDBUpdateGenerator;
@AutoService(DatabaseProvider.class)
public class HSQLDBProvider extends SQLProviderAdapter<HSQLDBProvider.HSQLDBGlobalState, HSQLDBOptions> {
private static final String HSQLDB = "hsqldb";
public HSQLDBProvider() {
super(HSQLDBGlobalState.class, HSQLDBOptions.class);
}
public enum Action implements AbstractAction<HSQLDBGlobalState> {
INSERT(HSQLDBInsertGenerator::getQuery), UPDATE(HSQLDBUpdateGenerator::getQuery);
private final SQLQueryProvider<HSQLDBProvider.HSQLDBGlobalState> sqlQueryProvider;
Action(SQLQueryProvider<HSQLDBProvider.HSQLDBGlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public SQLQueryAdapter getQuery(HSQLDBProvider.HSQLDBGlobalState state) throws Exception {
return sqlQueryProvider.getQuery(state);
}
}
@Override
public SQLConnection createDatabase(HSQLDBGlobalState globalState) throws Exception {
String databaseName = globalState.getDatabaseName();
String url = "jdbc:hsqldb:file:" + databaseName;
MainOptions options = globalState.getOptions();
Connection connection = DriverManager.getConnection(url, options.getUserName(), options.getPassword());
// When a server instance is started, or when a connection is made to an in-process database,
// a new, empty database is created if no database exists at the given path.
try (Statement s = connection.createStatement()) {
s.execute("DROP SCHEMA PUBLIC CASCADE");
s.execute("SET DATABASE SQL DOUBLE NAN FALSE");
}
return new SQLConnection(connection);
}
@Override
public String getDBMSName() {
return HSQLDB;
}
@Override
public void generateDatabase(HSQLDBGlobalState globalState) throws Exception {
for (int i = 0; i < Randomly.fromOptions(1, 2); i++) {
boolean success;
do {
SQLQueryAdapter qt = new HSQLDBTableGenerator().getQuery(globalState, null);
success = globalState.executeStatement(qt);
} while (!success);
}
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
StatementExecutor<HSQLDBGlobalState, Action> se = new StatementExecutor<>(globalState,
HSQLDBProvider.Action.values(), HSQLDBProvider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
se.executeStatements();
}
private static int mapActions(HSQLDBProvider.HSQLDBGlobalState globalState, HSQLDBProvider.Action a) {
Randomly r = globalState.getRandomly();
switch (a) {
case INSERT:
return r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
case UPDATE:
return r.getInteger(0, 10);
default:
throw new AssertionError(a);
}
}
public static class HSQLDBGlobalState extends SQLGlobalState<HSQLDBOptions, HSQLDBSchema> {
@Override
protected HSQLDBSchema readSchema() throws SQLException {
return HSQLDBSchema.fromConnection(getConnection(), getDatabaseName());
}
}
}