forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryAdapter.java
More file actions
102 lines (88 loc) · 2.79 KB
/
QueryAdapter.java
File metadata and controls
102 lines (88 loc) · 2.79 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
package sqlancer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
public class QueryAdapter extends Query {
private final String query;
private final Collection<String> expectedErrors;
private final boolean couldAffectSchema;
public QueryAdapter(String query) {
this(query, new ArrayList<>());
}
public QueryAdapter(String query, boolean couldAffectSchema) {
this(query, new ArrayList<>(), couldAffectSchema);
}
public QueryAdapter(String query, Collection<String> expectedErrors) {
this.query = query;
this.expectedErrors = expectedErrors;
this.couldAffectSchema = false;
}
public QueryAdapter(String query, Collection<String> expectedErrors, boolean couldAffectSchema) {
this.query = query;
this.expectedErrors = expectedErrors;
this.couldAffectSchema = couldAffectSchema;
}
@Override
public String getQueryString() {
return query;
}
@Override
public boolean execute(Connection con) throws SQLException {
try (Statement s = con.createStatement()) {
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 {
boolean isExcluded = false;
for (String expectedError : expectedErrors) {
if (e.getMessage().contains(expectedError)) {
isExcluded = true;
break;
}
}
if (!isExcluded) {
throw new AssertionError(query, e);
}
}
@Override
public ResultSet executeAndGet(Connection con) throws SQLException {
Statement s = con.createStatement();
ResultSet result = null;
try {
result = s.executeQuery(query);
Main.nrSuccessfulActions.addAndGet(1);
return result;
} catch (Exception e) {
s.close();
boolean isExcluded = false;
Main.nrUnsuccessfulActions.addAndGet(1);
for (String expectedError : expectedErrors) {
if (e.getMessage().contains(expectedError)) {
isExcluded = true;
break;
}
}
if (!isExcluded) {
throw e;
}
}
return null;
}
@Override
public boolean couldAffectSchema() {
return couldAffectSchema;
}
@Override
public Collection<String> getExpectedErrors() {
return expectedErrors;
}
}