forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseProvider.java
More file actions
103 lines (89 loc) · 3.21 KB
/
DatabaseProvider.java
File metadata and controls
103 lines (89 loc) · 3.21 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
package sqlancer;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public interface DatabaseProvider<G extends GlobalState<O>, O> {
void generateAndTestDatabase(G globalState) throws SQLException;
G generateGlobalState();
// TODO: should be type G
Connection createDatabase(GlobalState<?> globalState) throws SQLException;
/**
* The DBMS name is used to name the log directory and command to test the respective DBMS.
*/
String getDBMSName();
// TODO: remove this
default void printDatabaseSpecificState(FileWriter writer, StateToReproduce state) {
}
StateToReproduce getStateToReproduce(String databaseName);
O getCommand();
static boolean isEqualDouble(String first, String second) {
try {
double val = Double.parseDouble(first);
double secVal = Double.parseDouble(second);
return equals(val, secVal);
} catch (Exception e) {
return false;
}
}
static boolean equals(double a, double b) {
if (a == b) {
return true;
}
// If the difference is less than epsilon, treat as equal.
return Math.abs(a - b) < 0.0001 * Math.max(Math.abs(a), Math.abs(b));
}
static List<String> getResultSetFirstColumnAsString(String queryString, Set<String> errors, Connection con,
GlobalState<?> state) throws SQLException {
if (state.getOptions().logEachSelect()) {
// TODO: refactor me
state.getLogger().writeCurrent(queryString);
try {
state.getLogger().getCurrentFileWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
QueryAdapter q = new QueryAdapter(queryString, errors);
List<String> resultSet = new ArrayList<>();
ResultSet result = null;
try {
result = q.executeAndGet(con);
if (result == null) {
throw new IgnoreMeException();
}
while (result.next()) {
resultSet.add(result.getString(1));
}
result.getStatement().close();
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw e;
}
if (e instanceof NumberFormatException) {
// https://github.com/tidb-challenge-program/bug-hunting-issue/issues/57
throw new IgnoreMeException();
}
if (e.getMessage() == null) {
throw new AssertionError(queryString, e);
}
for (String error : errors) {
if (e.getMessage().contains(error)) {
throw new IgnoreMeException();
}
}
throw new AssertionError(queryString, e);
} finally {
if (result != null && !result.isClosed()) {
result.getStatement().close();
result.close();
}
}
return resultSet;
}
}