Skip to content

Commit 5c3c3c3

Browse files
committed
Refactor Exceptions to remove SQLDependency while keeping the same functionality
Multiple high-level classes such as DatabaseProvider carry already the semantics for non-SQL DBMS except for the fact that a lot of methods still throw SQLExceptions from the java.sql package. This commit exchanges SQLException with Exception in signatures to solve this. Also changes the configuration of pmd to exclude the rule "SignatureDeclareThrowsException"
1 parent 68ff94d commit 5c3c3c3

25 files changed

Lines changed: 67 additions & 87 deletions

configs/pmd-rules.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<exclude name="SingularField" />
7272
<exclude name="ExcessiveMethodLength" />
7373
<exclude name="TooManyFields" />
74+
<exclude name="SignatureDeclareThrowsException" />
7475
</rule>
7576
<rule ref="category/java/performance.xml">
7677
<priority>2</priority>

src/sqlancer/AbstractAction.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package sqlancer;
22

3-
import java.sql.SQLException;
4-
53
import sqlancer.common.query.Query;
64

75
public interface AbstractAction<G> {
86

9-
Query getQuery(G globalState) throws SQLException;
7+
Query getQuery(G globalState) throws Exception;
108

119
/**
1210
* Specifies whether it makes sense to request a {@link Query}, when the previous call to {@link #getQuery(Object)}

src/sqlancer/DatabaseProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sqlancer;
22

33
import java.sql.Connection;
4-
import java.sql.SQLException;
54

65
import sqlancer.common.log.LoggableFactory;
76

@@ -28,9 +27,9 @@ public interface DatabaseProvider<G extends GlobalState<O, ?>, O extends DBMSSpe
2827
* the state created and is valid for this method call.
2928
*
3029
*/
31-
void generateAndTestDatabase(G globalState) throws SQLException;
30+
void generateAndTestDatabase(G globalState) throws Exception;
3231

33-
Connection createDatabase(G globalState) throws SQLException;
32+
Connection createDatabase(G globalState) throws Exception;
3433

3534
/**
3635
* The DBMS name is used to name the log directory and command to test the respective DBMS.

src/sqlancer/GlobalState.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sqlancer;
22

33
import java.sql.Connection;
4-
import java.sql.SQLException;
54

65
import sqlancer.Main.QueryManager;
76
import sqlancer.Main.StateLogger;
@@ -95,7 +94,7 @@ public void setDatabaseName(String databaseName) {
9594
this.databaseName = databaseName;
9695
}
9796

98-
private ExecutionTimer executePrologue(Query q) throws SQLException {
97+
private ExecutionTimer executePrologue(Query q) throws Exception {
9998
boolean logExecutionTime = getOptions().logExecutionTime();
10099
ExecutionTimer timer = null;
101100
if (logExecutionTime) {
@@ -114,7 +113,7 @@ private ExecutionTimer executePrologue(Query q) throws SQLException {
114113
return timer;
115114
}
116115

117-
private void executeEpilogue(Query q, boolean success, ExecutionTimer timer) throws SQLException {
116+
private void executeEpilogue(Query q, boolean success, ExecutionTimer timer) throws Exception {
118117
boolean logExecutionTime = getOptions().logExecutionTime();
119118
if (success && getOptions().printSucceedingStatements()) {
120119
System.out.println(q.getQueryString());
@@ -127,22 +126,22 @@ private void executeEpilogue(Query q, boolean success, ExecutionTimer timer) thr
127126
}
128127
}
129128

130-
public boolean executeStatement(Query q, String... fills) throws SQLException {
129+
public boolean executeStatement(Query q, String... fills) throws Exception {
131130
ExecutionTimer timer = executePrologue(q);
132131
boolean success = manager.execute(q, fills);
133132
executeEpilogue(q, success, timer);
134133
return success;
135134
}
136135

137-
public SQLancerResultSet executeStatementAndGet(Query q, String... fills) throws SQLException {
136+
public SQLancerResultSet executeStatementAndGet(Query q, String... fills) throws Exception {
138137
ExecutionTimer timer = executePrologue(q);
139138
SQLancerResultSet result = manager.executeAndGet(q, fills);
140139
boolean success = result != null;
141140
if (success) {
142141
result.registerEpilogue(() -> {
143142
try {
144143
executeEpilogue(q, success, timer);
145-
} catch (SQLException e) {
144+
} catch (Exception e) {
146145
throw new AssertionError(e);
147146
}
148147
});
@@ -154,7 +153,7 @@ public S getSchema() {
154153
if (schema == null) {
155154
try {
156155
updateSchema();
157-
} catch (SQLException e) {
156+
} catch (Exception e) {
158157
throw new AssertionError();
159158
}
160159
}
@@ -165,13 +164,13 @@ protected void setSchema(S schema) {
165164
this.schema = schema;
166165
}
167166

168-
public void updateSchema() throws SQLException {
167+
public void updateSchema() throws Exception {
169168
setSchema(readSchema());
170169
for (AbstractTable<?, ?> table : schema.getDatabaseTables()) {
171170
table.recomputeCount();
172171
}
173172
}
174173

175-
protected abstract S readSchema() throws SQLException;
174+
protected abstract S readSchema() throws Exception;
176175

177176
}

src/sqlancer/Main.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.io.Writer;
77
import java.nio.file.Files;
88
import java.sql.Connection;
9-
import java.sql.SQLException;
109
import java.sql.SQLFeatureNotSupportedException;
1110
import java.text.DateFormat;
1211
import java.text.SimpleDateFormat;
@@ -232,15 +231,15 @@ public static class QueryManager {
232231
this.globalState = globalState;
233232
}
234233

235-
public boolean execute(Query q, String... fills) throws SQLException {
234+
public boolean execute(Query q, String... fills) throws Exception {
236235
globalState.getState().logStatement(q);
237236
boolean success;
238237
success = q.execute(globalState, fills);
239238
Main.nrSuccessfulActions.addAndGet(1);
240239
return success;
241240
}
242241

243-
public SQLancerResultSet executeAndGet(Query q, String... fills) throws SQLException {
242+
public SQLancerResultSet executeAndGet(Query q, String... fills) throws Exception {
244243
globalState.getState().logStatement(q);
245244
SQLancerResultSet result;
246245
result = q.executeAndGet(globalState, fills);
@@ -293,14 +292,14 @@ public O getCommand() {
293292
return command;
294293
}
295294

296-
public void testConnection() throws SQLException {
295+
public void testConnection() throws Exception {
297296
G state = getInitializedGlobalState(options.getRandomSeed());
298297
try (Connection con = provider.createDatabase(state)) {
299298
return;
300299
}
301300
}
302301

303-
public void run() throws SQLException {
302+
public void run() throws Exception {
304303
G state = createGlobalState();
305304
stateToRepro = provider.getStateToReproduce(databaseName);
306305
stateToRepro.seedValue = r.getSeed();
@@ -455,7 +454,7 @@ private String formatInteger(long intValue) {
455454
try {
456455
executorFactory.getDBMSExecutor(options.getDatabasePrefix() + "connectiontest", new Randomly())
457456
.testConnection();
458-
} catch (SQLException e) {
457+
} catch (Exception e) {
459458
System.err.println(
460459
"SQLancer failed creating a test database, indicating that SQLancer might have failed connecting to the DBMS. In order to change the username and password, you can use the --username and --password options. Currently, SQLancer does not yet support passing a host and port (see https://github.com/sqlancer/sqlancer/issues/95).\n\n");
461460
e.printStackTrace();

src/sqlancer/OracleFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package sqlancer;
22

3-
import java.sql.SQLException;
4-
53
import sqlancer.common.oracle.TestOracle;
64

75
public interface OracleFactory<G extends GlobalState<?, ?>> {
86

9-
TestOracle create(G globalState) throws SQLException;
7+
TestOracle create(G globalState) throws Exception;
108

119
/**
1210
* Indicates whether the test oracle requires that all tables (including views) contain at least one row.

src/sqlancer/ProviderAdapter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sqlancer;
22

3-
import java.sql.SQLException;
43
import java.util.List;
54
import java.util.stream.Collectors;
65

@@ -37,7 +36,7 @@ public Class<O> getOptionClass() {
3736
}
3837

3938
@Override
40-
public void generateAndTestDatabase(G globalState) throws SQLException {
39+
public void generateAndTestDatabase(G globalState) throws Exception {
4140
try {
4241
generateDatabase(globalState);
4342
checkViewsAreValid(globalState);
@@ -74,7 +73,7 @@ private void checkViewsAreValid(G globalState) {
7473
}
7574
}
7675

77-
protected TestOracle getTestOracle(G globalState) throws SQLException {
76+
protected TestOracle getTestOracle(G globalState) throws Exception {
7877
List<? extends OracleFactory<G>> testOracleFactory = globalState.getDmbsSpecificOptions()
7978
.getTestOracleFactory();
8079
boolean testOracleRequiresMoreThanZeroRows = testOracleFactory.stream()
@@ -90,13 +89,13 @@ protected TestOracle getTestOracle(G globalState) throws SQLException {
9089
return new CompositeTestOracle(testOracleFactory.stream().map(o -> {
9190
try {
9291
return o.create(globalState);
93-
} catch (SQLException e1) {
92+
} catch (Exception e1) {
9493
throw new AssertionError(e1);
9594
}
9695
}).collect(Collectors.toList()), globalState);
9796
}
9897
}
9998

100-
public abstract void generateDatabase(G globalState) throws SQLException;
99+
public abstract void generateDatabase(G globalState) throws Exception;
101100

102101
}

src/sqlancer/StatementExecutor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sqlancer;
22

3-
import java.sql.SQLException;
43
import java.util.ArrayList;
54
import java.util.List;
65

@@ -15,7 +14,7 @@ public class StatementExecutor<G extends GlobalState<?, ?>, A extends AbstractAc
1514

1615
@FunctionalInterface
1716
public interface AfterQueryAction {
18-
void notify(Query q) throws SQLException;
17+
void notify(Query q) throws Exception;
1918
}
2019

2120
@FunctionalInterface
@@ -30,7 +29,7 @@ public StatementExecutor(G globalState, A[] actions, ActionMapper<G, A> mapping,
3029
this.queryConsumer = queryConsumer;
3130
}
3231

33-
public void executeStatements() throws SQLException {
32+
public void executeStatements() throws Exception {
3433
Randomly r = globalState.getRandomly();
3534
int[] nrRemaining = new int[actions.length];
3635
List<A> availableActions = new ArrayList<>();

src/sqlancer/citus/CitusProvider.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public enum Action implements AbstractAction<PostgresGlobalState> {
113113
}
114114

115115
@Override
116-
public Query getQuery(PostgresGlobalState state) throws SQLException {
116+
public Query getQuery(PostgresGlobalState state) throws Exception {
117117
return queryProvider.getQuery(state);
118118
}
119119
}
@@ -200,7 +200,7 @@ public int getPort() {
200200
}
201201

202202
private static void distributeTable(List<PostgresColumn> columns, String tableName, CitusGlobalState globalState)
203-
throws SQLException {
203+
throws Exception {
204204
if (!columns.isEmpty()) {
205205
PostgresColumn columnToDistribute = Randomly.fromList(columns);
206206
String queryString = "SELECT create_distributed_table('" + tableName + "', '" + columnToDistribute.getName()
@@ -227,7 +227,7 @@ private static List<String> getTableConstraints(String tableName, CitusGlobalSta
227227
return constraints;
228228
}
229229

230-
private static void createDistributedTable(String tableName, CitusGlobalState globalState) throws SQLException {
230+
private static void createDistributedTable(String tableName, CitusGlobalState globalState) throws Exception {
231231
List<PostgresColumn> columns = new ArrayList<>();
232232
List<String> tableConstraints = getTableConstraints(tableName, globalState);
233233
if (tableConstraints.isEmpty()) {
@@ -276,7 +276,7 @@ private static void createDistributedTable(String tableName, CitusGlobalState gl
276276
}
277277

278278
@Override
279-
public void generateDatabase(PostgresGlobalState globalState) throws SQLException {
279+
public void generateDatabase(PostgresGlobalState globalState) throws Exception {
280280
readFunctions(globalState);
281281
createTables(globalState, Randomly.fromOptions(4, 5, 6));
282282
for (PostgresTable table : globalState.getSchema().getDatabaseTables()) {
@@ -307,7 +307,7 @@ protected TestOracle getTestOracle(PostgresGlobalState globalState) throws SQLEx
307307
List<TestOracle> oracles = ((CitusOptions) globalState.getDmbsSpecificOptions()).citusOracle.stream().map(o -> {
308308
try {
309309
return o.create(globalState);
310-
} catch (SQLException e1) {
310+
} catch (Exception e1) {
311311
throw new AssertionError(e1);
312312
}
313313
}).collect(Collectors.toList());
@@ -424,7 +424,7 @@ public Connection createDatabase(PostgresGlobalState globalState) throws SQLExce
424424
}
425425

426426
@Override
427-
protected void prepareTables(PostgresGlobalState globalState) throws SQLException {
427+
protected void prepareTables(PostgresGlobalState globalState) throws Exception {
428428
StatementExecutor<PostgresGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
429429
CitusProvider::mapActions, (q) -> {
430430
if (globalState.getSchema().getDatabaseTables().isEmpty()) {

src/sqlancer/clickhouse/ClickHouseProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public enum Action implements AbstractAction<ClickHouseGlobalState> {
3636
}
3737

3838
@Override
39-
public Query getQuery(ClickHouseGlobalState state) throws SQLException {
39+
public Query getQuery(ClickHouseGlobalState state) throws Exception {
4040
return queryProvider.getQuery(state);
4141
}
4242
}
@@ -80,7 +80,7 @@ protected ClickHouseSchema readSchema() throws SQLException {
8080
}
8181

8282
@Override
83-
public void generateDatabase(ClickHouseGlobalState globalState) throws SQLException {
83+
public void generateDatabase(ClickHouseGlobalState globalState) throws Exception {
8484
for (int i = 0; i < Randomly.fromOptions(1); i++) {
8585
boolean success;
8686
do {

0 commit comments

Comments
 (0)