Skip to content

Commit 1ff7497

Browse files
committed
Make the print statement options accessible to all DBMS
1 parent d5a33be commit 1ff7497

File tree

6 files changed

+41
-39
lines changed

6 files changed

+41
-39
lines changed

src/sqlancer/GlobalState.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public boolean executeStatement(Query q) throws SQLException {
9595
if (logExecutionTime) {
9696
timer = new ExecutionTimer().start();
9797
}
98+
if (getOptions().printAllStatements()) {
99+
System.out.println(q.getQueryString());
100+
}
98101
if (getOptions().logEachSelect()) {
99102
if (logExecutionTime) {
100103
getLogger().writeCurrentNoLineBreak(q.getQueryString());
@@ -103,6 +106,9 @@ public boolean executeStatement(Query q) throws SQLException {
103106
}
104107
}
105108
boolean success = manager.execute(q);
109+
if (success && getOptions().printSucceedingStatements()) {
110+
System.out.println(q.getQueryString());
111+
}
106112
if (logExecutionTime) {
107113
getLogger().writeCurrent(" -- " + timer.end().asString());
108114
}

src/sqlancer/MainOptions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public class MainOptions {
5454
@Parameter(names = "--exit-code-error", description = "The exit code that should be returned when an error is encountered (or a bug is found)")
5555
private int errorExitCode = -1; // NOPMD
5656

57+
@Parameter(names = "--print-statements", description = "Print all statements to stdout, before they are sent to the DBMS (not yet implemented for all oracles)", arity = 1)
58+
private boolean printStatements; // NOPMD
59+
60+
@Parameter(names = "--print-succeeding-statements", description = "Print statements that are successfully processed by the DBMS to stdout (not yet implemented for all oracles)", arity = 1)
61+
private boolean printSucceedingStatements; // NOPMD
62+
5763
public int getMaxExpressionDepth() {
5864
return maxExpressionDepth;
5965
}
@@ -70,6 +76,20 @@ public boolean logEachSelect() {
7076
return logEachSelect;
7177
}
7278

79+
public boolean printAllStatements() {
80+
if (printSucceedingStatements && printStatements) {
81+
throw new AssertionError();
82+
}
83+
return printStatements;
84+
}
85+
86+
public boolean printSucceedingStatements() {
87+
if (printStatements && printSucceedingStatements) {
88+
throw new AssertionError();
89+
}
90+
return printSucceedingStatements;
91+
}
92+
7393
public boolean logExecutionTime() {
7494
if (!logEachSelect) {
7595
throw new AssertionError();

src/sqlancer/QueryAdapter.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,30 @@ public QueryAdapter(String query, boolean couldAffectSchema) {
2121
}
2222

2323
public QueryAdapter(String query, Collection<String> expectedErrors) {
24-
this.query = query;
24+
this.query = canonicalizeString(query);
2525
this.expectedErrors = expectedErrors;
2626
this.couldAffectSchema = false;
2727
checkQueryString();
2828
}
2929

3030
public QueryAdapter(String query, Collection<String> expectedErrors, boolean couldAffectSchema) {
31-
this.query = query;
31+
this.query = canonicalizeString(query);
3232
this.expectedErrors = expectedErrors;
3333
this.couldAffectSchema = couldAffectSchema;
3434
checkQueryString();
3535
}
3636

37+
private String canonicalizeString(String s) {
38+
if (s.endsWith(";")) {
39+
return s;
40+
} else if (!s.contains("--")) {
41+
return s + ";";
42+
} else {
43+
// query contains a comment
44+
return s;
45+
}
46+
}
47+
3748
private void checkQueryString() {
3849
if (query.contains("CREATE TABLE") && !couldAffectSchema) {
3950
throw new AssertionError("CREATE TABLE statements should set couldAffectSchema to true");

src/sqlancer/sqlite3/SQLite3Options.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ public class SQLite3Options {
8181
"--generate-new-database" }, description = "Specifies whether new databases should be generated", arity = 1)
8282
public boolean generateDatabase = true;
8383

84-
@Parameter(names = {
85-
"--print-statements" }, description = "Specifies whether to print SQL statements to stdout", arity = 1)
86-
public boolean printStatements;
87-
8884
@Parameter(names = {
8985
"--execute-queries" }, description = "Specifies whether the query in the fuzzer should be executed", arity = 1)
9086
public boolean executeQuery = true;

src/sqlancer/sqlite3/SQLite3Provider.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,13 @@ public void generateAndTestDatabase(SQLite3GlobalState globalState) throws SQLEx
208208
do {
209209
Query tableQuery = getTableQuery(r, i++);
210210
globalState.executeStatement(tableQuery);
211-
logStatement(globalState, tableQuery);
212211
} while (globalState.getSchema().getDatabaseTables().size() < nrTablesToCreate);
213212
assert globalState.getSchema().getTables().getTables().size() == nrTablesToCreate;
214213
checkTablesForGeneratedColumnLoops(globalState);
215214
if (globalState.getDmbsSpecificOptions().testDBStats && Randomly.getBooleanWithSmallProbability()) {
216215
QueryAdapter tableQuery = new QueryAdapter(
217216
"CREATE VIRTUAL TABLE IF NOT EXISTS stat USING dbstat(main)");
218217
globalState.executeStatement(tableQuery);
219-
logStatement(globalState, tableQuery);
220218
}
221219
int[] nrRemaining = new int[Action.values().length];
222220
List<Action> actions = new ArrayList<>();
@@ -296,7 +294,6 @@ public void generateAndTestDatabase(SQLite3GlobalState globalState) throws SQLEx
296294
try {
297295
query = nextAction.getQuery(globalState);
298296
globalState.executeStatement(query);
299-
logStatement(globalState, query);
300297
} catch (IgnoreMeException e) {
301298

302299
}
@@ -308,12 +305,10 @@ public void generateAndTestDatabase(SQLite3GlobalState globalState) throws SQLEx
308305
}
309306
Query query = SQLite3TransactionGenerator.generateCommit(globalState);
310307
globalState.executeStatement(query);
311-
logStatement(globalState, query);
312308

313309
// also do an abort for DEFERRABLE INITIALLY DEFERRED
314310
query = SQLite3TransactionGenerator.generateRollbackTransaction(globalState);
315311
globalState.executeStatement(query);
316-
logStatement(globalState, query);
317312
manager.incrementCreateDatabase();
318313
}
319314
TestOracle oracle = globalState.getSqliteOptions().oracle.create(globalState);
@@ -338,16 +333,6 @@ public void generateAndTestDatabase(SQLite3GlobalState globalState) throws SQLEx
338333
}
339334
}
340335

341-
private void logStatement(SQLite3GlobalState globalState, Query tableQuery) {
342-
if (globalState.getDmbsSpecificOptions().printStatements) {
343-
String s = tableQuery.getQueryString();
344-
if (!s.endsWith(";")) {
345-
s = s + ";";
346-
}
347-
System.out.println(s);
348-
}
349-
}
350-
351336
private void checkTablesForGeneratedColumnLoops(SQLite3GlobalState globalState) throws SQLException {
352337
for (SQLite3Table table : globalState.getSchema().getDatabaseTables()) {
353338
Query q = new QueryAdapter("SELECT * FROM " + table.getName(),
@@ -401,12 +386,7 @@ private void addSensiblePragmaDefaults(SQLite3GlobalState globalState) throws SQ
401386
Randomly.fromOptions("UTF-8", "UTF-16", "UTF-16le", "UTF-16be")));
402387
}
403388
for (String s : pragmasToExecute) {
404-
if (globalState.getDmbsSpecificOptions().printStatements) {
405-
System.out.println(s);
406-
}
407-
Query q = new QueryAdapter(s);
408-
state.statements.add(q);
409-
q.execute(globalState);
389+
globalState.executeStatement(new QueryAdapter(s));
410390
}
411391
}
412392

src/sqlancer/sqlite3/oracle/SQLite3Fuzzer.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.sql.SQLException;
44

5-
import sqlancer.MainOptions;
65
import sqlancer.QueryAdapter;
76
import sqlancer.Randomly;
87
import sqlancer.TestOracle;
@@ -22,19 +21,9 @@ public SQLite3Fuzzer(SQLite3GlobalState globalState) {
2221
public void check() throws SQLException {
2322
String s = SQLite3Visitor
2423
.asString(SQLite3RandomQuerySynthesizer.generate(globalState, Randomly.smallNumber() + 1)) + ";";
25-
MainOptions options = globalState.getOptions();
2624
try {
27-
if (options.logEachSelect()) {
28-
globalState.getLogger().writeCurrent(s);
29-
}
30-
if (globalState.getDmbsSpecificOptions().printStatements) {
31-
System.out.println(s);
32-
}
3325
if (globalState.getDmbsSpecificOptions().executeQuery) {
34-
globalState.getManager().execute(new QueryAdapter(s));
35-
if (globalState.getDmbsSpecificOptions().executeStatementsAndPrintSuccessfulOnes) {
36-
System.out.println(s);
37-
}
26+
globalState.executeStatement(new QueryAdapter(s));
3827
globalState.getManager().incrementSelectQueryCount();
3928
}
4029
} catch (Error e) {

0 commit comments

Comments
 (0)