-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLQueryAdapter.java
More file actions
227 lines (201 loc) · 7.02 KB
/
SQLQueryAdapter.java
File metadata and controls
227 lines (201 loc) · 7.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package sqlancer.common.query;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import sqlancer.GlobalState;
import sqlancer.Main;
import sqlancer.SQLConnection;
public class SQLQueryAdapter extends Query<SQLConnection> implements Serializable {
private static final long serialVersionUID = 1L;
private final String query;
private final ExpectedErrors expectedErrors;
private final boolean couldAffectSchema;
public SQLQueryAdapter(String query) {
this(query, new ExpectedErrors());
}
public SQLQueryAdapter(String query, boolean couldAffectSchema) {
this(query, new ExpectedErrors(), couldAffectSchema);
}
public SQLQueryAdapter(String query, ExpectedErrors expectedErrors) {
this(query, expectedErrors, guessAffectSchemaFromQuery(query));
}
private static boolean guessAffectSchemaFromQuery(String query) {
return query.contains("CREATE TABLE") && !query.startsWith("EXPLAIN");
}
public SQLQueryAdapter(String query, ExpectedErrors expectedErrors, boolean couldAffectSchema) {
this(query, expectedErrors, couldAffectSchema, true);
}
public SQLQueryAdapter(String query, ExpectedErrors expectedErrors, boolean couldAffectSchema,
boolean canonicalizeString) {
if (canonicalizeString) {
this.query = canonicalizeString(query);
} else {
this.query = query;
}
this.expectedErrors = expectedErrors;
this.couldAffectSchema = couldAffectSchema;
checkQueryString();
}
private String canonicalizeString(String s) {
if (s.endsWith(";")) {
return s;
} else if (!s.contains("--")) {
return s + ";";
} else {
// query contains a comment
return s;
}
}
private void checkQueryString() {
if (!couldAffectSchema && guessAffectSchemaFromQuery(query)) {
throw new AssertionError("CREATE TABLE statements should set couldAffectSchema to true");
}
}
@Override
public String getQueryString() {
return query;
}
@Override
public String getUnterminatedQueryString() {
String result;
if (query.endsWith(";")) {
result = query.substring(0, query.length() - 1);
} else {
result = query;
}
assert !result.endsWith(";");
return result;
}
/**
* This method is used to mostly oracles, which need to report exceptions. We set the reportException parameter to
* true by default meaning that exceptions are reported.
*
* @param globalState
* @param fills
*
* @return whether the query was executed successfully
*
* @param <G>
*
* @throws SQLException
*/
@Override
public <G extends GlobalState<?, ?, SQLConnection>> boolean execute(G globalState, String... fills)
throws SQLException {
return execute(globalState, true, fills);
}
/**
* This method is used to DQE oracles, DQE does not check exception separately, while other testing methods may
* need. We use reportException to control this behavior. For a specific DBMS used DQE oracle, we call this method
* and pass a boolean value of false as an argument.
*
* @param globalState
* @param reportException
* @param fills
*
* @return whether the query was executed successfully
*
* @param <G>
*
* @throws SQLException
*/
public <G extends GlobalState<?, ?, SQLConnection>> boolean execute(G globalState, boolean reportException,
String... fills) throws SQLException {
return internalExecute(globalState.getConnection(), reportException, fills);
}
protected <G extends GlobalState<?, ?, SQLConnection>> boolean internalExecute(SQLConnection connection,
boolean reportException, String... fills) throws SQLException {
Statement s;
if (fills.length > 0) {
s = connection.prepareStatement(fills[0]);
for (int i = 1; i < fills.length; i++) {
((PreparedStatement) s).setString(i, fills[i]);
}
} else {
s = connection.createStatement();
}
try {
if (fills.length > 0) {
((PreparedStatement) s).execute();
} else {
s.execute(query);
}
Main.nrSuccessfulActions.addAndGet(1);
return true;
} catch (Exception e) {
Main.nrUnsuccessfulActions.addAndGet(1);
if (reportException) {
checkException(e);
}
return false;
} finally {
s.close();
}
}
public void checkException(Exception e) throws AssertionError {
Throwable ex = e;
while (ex != null) {
if (expectedErrors.errorIsExpected(ex.getMessage())) {
return;
} else {
ex = ex.getCause();
}
}
throw new AssertionError(query, e);
}
@Override
public <G extends GlobalState<?, ?, SQLConnection>> SQLancerResultSet executeAndGet(G globalState, String... fills)
throws SQLException {
return executeAndGet(globalState, true, fills);
}
public <G extends GlobalState<?, ?, SQLConnection>> SQLancerResultSet executeAndGet(G globalState,
boolean reportException, String... fills) throws SQLException {
return internalExecuteAndGet(globalState.getConnection(), reportException, fills);
}
protected <G extends GlobalState<?, ?, SQLConnection>> SQLancerResultSet internalExecuteAndGet(
SQLConnection connection, boolean reportException, String... fills) throws SQLException {
Statement s;
if (fills.length > 0) {
s = connection.prepareStatement(fills[0]);
for (int i = 1; i < fills.length; i++) {
((PreparedStatement) s).setString(i, fills[i]);
}
} else {
s = connection.createStatement();
}
ResultSet result;
try {
if (fills.length > 0) {
result = ((PreparedStatement) s).executeQuery();
} else {
result = s.executeQuery(query);
}
Main.nrSuccessfulActions.addAndGet(1);
if (result == null) {
return null;
}
return new SQLancerResultSet(result);
} catch (Exception e) {
s.close();
Main.nrUnsuccessfulActions.addAndGet(1);
if (reportException) {
checkException(e);
}
return null;
}
}
@Override
public boolean couldAffectSchema() {
return couldAffectSchema;
}
@Override
public ExpectedErrors getExpectedErrors() {
return expectedErrors;
}
@Override
public String getLogString() {
return getQueryString();
}
}