forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalState.java
More file actions
153 lines (125 loc) · 4.11 KB
/
GlobalState.java
File metadata and controls
153 lines (125 loc) · 4.11 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
package sqlancer;
import sqlancer.common.query.Query;
import sqlancer.common.query.SQLancerResultSet;
import sqlancer.common.schema.AbstractSchema;
import sqlancer.common.schema.AbstractTable;
public abstract class GlobalState<O extends DBMSSpecificOptions<?>, S extends AbstractSchema<?, ?>, C extends SQLancerDBConnection> {
protected C databaseConnection;
private Randomly r;
private MainOptions options;
private O dmbsSpecificOptions;
private S schema;
private Main.StateLogger logger;
private StateToReproduce state;
private Main.QueryManager<C> manager;
private String databaseName;
public void setConnection(C con) {
this.databaseConnection = con;
}
public C getConnection() {
return databaseConnection;
}
@SuppressWarnings("unchecked")
public void setDmbsSpecificOptions(Object dmbsSpecificOptions) {
this.dmbsSpecificOptions = (O) dmbsSpecificOptions;
}
public O getDmbsSpecificOptions() {
return dmbsSpecificOptions;
}
public void setRandomly(Randomly r) {
this.r = r;
}
public Randomly getRandomly() {
return r;
}
public MainOptions getOptions() {
return options;
}
public void setMainOptions(MainOptions options) {
this.options = options;
}
public void setStateLogger(Main.StateLogger logger) {
this.logger = logger;
}
public Main.StateLogger getLogger() {
return logger;
}
public void setState(StateToReproduce state) {
this.state = state;
}
public StateToReproduce getState() {
return state;
}
public Main.QueryManager<C> getManager() {
return manager;
}
public void setManager(Main.QueryManager<C> manager) {
this.manager = manager;
}
public String getDatabaseName() {
return databaseName;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
private ExecutionTimer executePrologue(Query<?> q) throws Exception {
boolean logExecutionTime = getOptions().logExecutionTime();
ExecutionTimer timer = null;
if (logExecutionTime) {
timer = new ExecutionTimer().start();
}
if (getOptions().printAllStatements()) {
System.out.println(q.getLogString());
}
if (getOptions().logEachSelect()) {
if (logExecutionTime) {
getLogger().writeCurrentNoLineBreak(q.getLogString());
} else {
getLogger().writeCurrent(q.getLogString());
}
}
return timer;
}
protected abstract void executeEpilogue(Query<?> q, boolean success, ExecutionTimer timer) throws Exception;
public boolean executeStatement(Query<C> q, String... fills) throws Exception {
ExecutionTimer timer = executePrologue(q);
boolean success = manager.execute(q, fills);
executeEpilogue(q, success, timer);
return success;
}
public SQLancerResultSet executeStatementAndGet(Query<C> q, String... fills) throws Exception {
ExecutionTimer timer = executePrologue(q);
SQLancerResultSet result = manager.executeAndGet(q, fills);
boolean success = result != null;
if (success) {
result.registerEpilogue(() -> {
try {
executeEpilogue(q, success, timer);
} catch (Exception e) {
throw new AssertionError(e);
}
});
}
return result;
}
public S getSchema() {
if (schema == null) {
try {
updateSchema();
} catch (Exception e) {
throw new AssertionError();
}
}
return schema;
}
protected void setSchema(S schema) {
this.schema = schema;
}
public void updateSchema() throws Exception {
setSchema(readSchema());
for (AbstractTable<?, ?, ?> table : schema.getDatabaseTables()) {
table.recomputeCount();
}
}
protected abstract S readSchema() throws Exception;
}