-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathCnosDBProvider.java
More file actions
123 lines (99 loc) · 4.36 KB
/
CnosDBProvider.java
File metadata and controls
123 lines (99 loc) · 4.36 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
package sqlancer.cnosdb;
import java.util.Objects;
import com.google.auto.service.AutoService;
import sqlancer.AbstractAction;
import sqlancer.DatabaseProvider;
import sqlancer.IgnoreMeException;
import sqlancer.ProviderAdapter;
import sqlancer.Randomly;
import sqlancer.StatementExecutor;
import sqlancer.cnosdb.client.CnosDBClient;
import sqlancer.cnosdb.client.CnosDBConnection;
import sqlancer.cnosdb.gen.CnosDBInsertGenerator;
import sqlancer.cnosdb.gen.CnosDBTableGenerator;
import sqlancer.cnosdb.query.CnosDBOtherQuery;
import sqlancer.cnosdb.query.CnosDBQueryProvider;
import sqlancer.common.log.LoggableFactory;
@AutoService(DatabaseProvider.class)
public class CnosDBProvider extends ProviderAdapter<CnosDBGlobalState, CnosDBOptions, CnosDBConnection> {
protected String username;
protected String password;
protected String host;
protected int port;
protected String databaseName;
public CnosDBProvider() {
super(CnosDBGlobalState.class, CnosDBOptions.class);
}
protected CnosDBProvider(Class<CnosDBGlobalState> globalClass, Class<CnosDBOptions> optionClass) {
super(globalClass, optionClass);
}
protected static int mapActions(CnosDBGlobalState globalState, Action a) {
Randomly r = globalState.getRandomly();
int nrPerformed;
if (Objects.requireNonNull(a) == Action.INSERT) {
nrPerformed = r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
} else {
throw new AssertionError(a);
}
return nrPerformed;
}
@Override
protected void checkViewsAreValid(CnosDBGlobalState globalState) {
}
@Override
public void generateDatabase(CnosDBGlobalState globalState) throws Exception {
createTables(globalState, Randomly.fromOptions(4, 5, 6));
prepareTables(globalState);
}
@Override
public CnosDBConnection createDatabase(CnosDBGlobalState globalState) throws Exception {
username = globalState.getOptions().getUserName();
password = globalState.getOptions().getPassword();
host = globalState.getOptions().getHost();
port = globalState.getOptions().getPort();
databaseName = globalState.getDatabaseName();
CnosDBClient client = new CnosDBClient(host, port, username, password, databaseName);
CnosDBConnection connection = new CnosDBConnection(client);
client.execute("DROP DATABASE IF EXISTS " + databaseName);
globalState.getState().logStatement("DROP DATABASE IF EXISTS " + databaseName);
client.execute("CREATE DATABASE " + databaseName);
globalState.getState().logStatement("CREATE DATABASE " + databaseName);
return connection;
}
protected void createTables(CnosDBGlobalState globalState, int numTables) throws Exception {
while (globalState.getSchema().getDatabaseTables().size() < numTables) {
String tableName = String.format("m%d", globalState.getSchema().getDatabaseTables().size());
CnosDBOtherQuery createTable = CnosDBTableGenerator.generate(tableName);
globalState.executeStatement(createTable);
}
}
protected void prepareTables(CnosDBGlobalState globalState) throws Exception {
StatementExecutor<CnosDBGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
CnosDBProvider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
se.executeStatements();
}
@Override
public String getDBMSName() {
return "CnosDB".toLowerCase();
}
@Override
public LoggableFactory getLoggableFactory() {
return new CnosDBLoggableFactory();
}
public enum Action implements AbstractAction<CnosDBGlobalState> {
INSERT(CnosDBInsertGenerator::insert);
private final CnosDBQueryProvider<CnosDBGlobalState> sqlQueryProvider;
Action(CnosDBQueryProvider<CnosDBGlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public CnosDBOtherQuery getQuery(CnosDBGlobalState state) throws Exception {
return new CnosDBOtherQuery(sqlQueryProvider.getQuery(state).getQueryString(),
CnosDBExpectedError.expectedErrors());
}
}
}