-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLQueryAdapter.java
More file actions
150 lines (132 loc) · 4.31 KB
/
SQLQueryAdapter.java
File metadata and controls
150 lines (132 loc) · 4.31 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
package sqlancer.common.query;
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> {
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, false);
}
public SQLQueryAdapter(String query, ExpectedErrors expectedErrors, boolean couldAffectSchema) {
this.query = canonicalizeString(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 (query.contains("CREATE TABLE") && !couldAffectSchema) {
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;
}
@Override
public <G extends GlobalState<?, ?, SQLConnection>> boolean execute(G globalState, String... fills)
throws SQLException {
Statement s;
if (fills.length > 0) {
s = globalState.getConnection().prepareStatement(fills[0]);
for (int i = 1; i < fills.length; i++) {
((PreparedStatement) s).setString(i, fills[i]);
}
} else {
s = globalState.getConnection().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);
checkException(e);
return false;
}
}
public void checkException(Exception e) throws AssertionError {
if (!expectedErrors.errorIsExpected(e.getMessage())) {
throw new AssertionError(query, e);
}
}
@Override
public <G extends GlobalState<?, ?, SQLConnection>> SQLancerResultSet executeAndGet(G globalState, String... fills)
throws SQLException {
Statement s;
if (fills.length > 0) {
s = globalState.getConnection().prepareStatement(fills[0]);
for (int i = 1; i < fills.length; i++) {
((PreparedStatement) s).setString(i, fills[i]);
}
} else {
s = globalState.getConnection().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);
checkException(e);
}
return null;
}
@Override
public boolean couldAffectSchema() {
return couldAffectSchema;
}
@Override
public ExpectedErrors getExpectedErrors() {
return expectedErrors;
}
@Override
public String getLogString() {
return getQueryString();
}
}