Skip to content

Commit c4af603

Browse files
committed
[SQLite] Use the abstract table classes
1 parent a2c700a commit c4af603

9 files changed

Lines changed: 36 additions & 132 deletions

src/sqlancer/schema/AbstractTableColumn.java

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

3-
import sqlancer.cockroachdb.CockroachDBSchema.CockroachDBColumn;
4-
53
public class AbstractTableColumn<T extends AbstractTable<?, ?>, U> implements Comparable<AbstractTableColumn<T, U>> {
64

75
private final String name;
@@ -44,7 +42,8 @@ public boolean equals(Object obj) {
4442
if (!(obj instanceof AbstractTableColumn)) {
4543
return false;
4644
} else {
47-
CockroachDBColumn c = (CockroachDBColumn) obj;
45+
@SuppressWarnings("unchecked")
46+
AbstractTableColumn<T, U> c = (AbstractTableColumn<T, U>) obj;
4847
return table.getName().contentEquals(getName()) && getName().equals(c.getName());
4948
}
5049
}

src/sqlancer/sqlite3/SQLite3ExpectedValueVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sqlancer.sqlite3.ast.SQLite3Expression.Subquery;
2727
import sqlancer.sqlite3.ast.SQLite3Expression.TypeLiteral;
2828
import sqlancer.sqlite3.ast.SQLite3Function;
29-
import sqlancer.sqlite3.ast.SQLite3RowValue;
29+
import sqlancer.sqlite3.ast.SQLite3RowValueExpression;
3030
import sqlancer.sqlite3.ast.SQLite3SelectStatement;
3131
import sqlancer.sqlite3.ast.SQLite3SetClause;
3232
import sqlancer.sqlite3.ast.SQLite3UnaryOperation;
@@ -261,7 +261,7 @@ public void visit(MatchOperation match) {
261261
}
262262

263263
@Override
264-
public void visit(SQLite3RowValue rw) {
264+
public void visit(SQLite3RowValueExpression rw) {
265265
print(rw);
266266
for (SQLite3Expression expr : rw.getExpressions()) {
267267
visit(expr);

src/sqlancer/sqlite3/SQLite3ToStringVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sqlancer.sqlite3.ast.SQLite3Expression.Subquery;
2727
import sqlancer.sqlite3.ast.SQLite3Expression.TypeLiteral;
2828
import sqlancer.sqlite3.ast.SQLite3Function;
29-
import sqlancer.sqlite3.ast.SQLite3RowValue;
29+
import sqlancer.sqlite3.ast.SQLite3RowValueExpression;
3030
import sqlancer.sqlite3.ast.SQLite3SelectStatement;
3131
import sqlancer.sqlite3.ast.SQLite3SetClause;
3232
import sqlancer.sqlite3.ast.SQLite3WindowFunction;
@@ -376,7 +376,7 @@ public void visit(MatchOperation match) {
376376
}
377377

378378
@Override
379-
public void visit(SQLite3RowValue rw) {
379+
public void visit(SQLite3RowValueExpression rw) {
380380
sb.append("(");
381381
visit(rw.getExpressions());
382382
sb.append(")");

src/sqlancer/sqlite3/SQLite3Visitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import sqlancer.sqlite3.ast.SQLite3Expression.Subquery;
2626
import sqlancer.sqlite3.ast.SQLite3Expression.TypeLiteral;
2727
import sqlancer.sqlite3.ast.SQLite3Function;
28-
import sqlancer.sqlite3.ast.SQLite3RowValue;
28+
import sqlancer.sqlite3.ast.SQLite3RowValueExpression;
2929
import sqlancer.sqlite3.ast.SQLite3SelectStatement;
3030
import sqlancer.sqlite3.ast.SQLite3SetClause;
3131
import sqlancer.sqlite3.ast.SQLite3UnaryOperation;
@@ -124,7 +124,7 @@ public default void visit(SQLite3PostfixUnaryOperation exp) {
124124

125125
public abstract void visit(SQLite3WindowFunction func);
126126

127-
public abstract void visit(SQLite3RowValue rw);
127+
public abstract void visit(SQLite3RowValueExpression rw);
128128

129129
public abstract void visit(SQLite3WindowFunctionExpression windowFunction);
130130

@@ -181,8 +181,8 @@ public default void visit(SQLite3Expression expr) {
181181
visit((SQLite3WindowFunction) expr);
182182
} else if (expr instanceof MatchOperation) {
183183
visit((MatchOperation) expr);
184-
} else if (expr instanceof SQLite3RowValue) {
185-
visit((SQLite3RowValue) expr);
184+
} else if (expr instanceof SQLite3RowValueExpression) {
185+
visit((SQLite3RowValueExpression) expr);
186186
} else if (expr instanceof SQLite3Text) {
187187
visit((SQLite3Text) expr);
188188
} else if (expr instanceof SQLite3WindowFunctionExpression) {

src/sqlancer/sqlite3/ast/SQLite3Expression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ public SQLite3Constant getExpectedValue() {
15771577
*/
15781578
@Override
15791579
public TypeAffinity getAffinity() {
1580-
switch (column.getColumnType()) {
1580+
switch (column.getType()) {
15811581
case BINARY:
15821582
return TypeAffinity.BLOB;
15831583
case INT:

src/sqlancer/sqlite3/ast/SQLite3RowValue.java renamed to src/sqlancer/sqlite3/ast/SQLite3RowValueExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
66

7-
public class SQLite3RowValue extends SQLite3Expression {
7+
public class SQLite3RowValueExpression extends SQLite3Expression {
88

99
private final List<SQLite3Expression> expressions;
1010

11-
public SQLite3RowValue(List<SQLite3Expression> expressions) {
11+
public SQLite3RowValueExpression(List<SQLite3Expression> expressions) {
1212
this.expressions = expressions;
1313
}
1414

src/sqlancer/sqlite3/gen/SQLite3ExpressionGenerator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
import sqlancer.sqlite3.ast.SQLite3Expression.TypeLiteral;
3838
import sqlancer.sqlite3.ast.SQLite3Function;
3939
import sqlancer.sqlite3.ast.SQLite3Function.ComputableFunction;
40-
import sqlancer.sqlite3.ast.SQLite3RowValue;
40+
import sqlancer.sqlite3.ast.SQLite3RowValueExpression;
4141
import sqlancer.sqlite3.ast.SQLite3UnaryOperation;
4242
import sqlancer.sqlite3.ast.SQLite3UnaryOperation.UnaryOperator;
4343
import sqlancer.sqlite3.queries.SQLite3RandomQuerySynthesizer;
44-
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3RowValue;
4544
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
4645
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
46+
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3RowValue;
4747
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Table;
4848

4949
public class SQLite3ExpressionGenerator {
@@ -337,11 +337,11 @@ private SQLite3Expression getRowValueComparison(int depth) {
337337
switch (randomOption) {
338338
// TODO case
339339
case STANDARD_COMPARISON:
340-
return new BinaryComparisonOperation(new SQLite3RowValue(left), new SQLite3RowValue(right),
340+
return new BinaryComparisonOperation(new SQLite3RowValueExpression(left), new SQLite3RowValueExpression(right),
341341
BinaryComparisonOperator.getRandomRowValueOperator());
342342
case BETWEEN:
343343
return new BetweenOperation(getRandomRowValue(depth + 1, size), Randomly.getBoolean(),
344-
new SQLite3RowValue(left), new SQLite3RowValue(right));
344+
new SQLite3RowValueExpression(left), new SQLite3RowValueExpression(right));
345345
// case IN:
346346
// return new SQLite3Expression.InOperation(new SQLite3RowValue(left),
347347
// SQLite3RandomQuerySynthesizer.generate(globalState, size));
@@ -350,8 +350,8 @@ private SQLite3Expression getRowValueComparison(int depth) {
350350
}
351351
}
352352

353-
private SQLite3RowValue getRandomRowValue(int depth, int size) {
354-
return new SQLite3RowValue(getRandomExpressions(size, depth + 1));
353+
private SQLite3RowValueExpression getRandomRowValue(int depth, int size) {
354+
return new SQLite3RowValueExpression(getRandomExpressions(size, depth + 1));
355355
}
356356

357357
private SQLite3Expression getMatchClause(int depth) {

src/sqlancer/sqlite3/queries/SQLite3PivotedQuerySynthesizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import sqlancer.sqlite3.gen.SQLite3Common;
4242
import sqlancer.sqlite3.gen.SQLite3ExpressionGenerator;
4343
import sqlancer.sqlite3.schema.SQLite3Schema;
44-
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3RowValue;
4544
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
45+
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3RowValue;
4646
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Table;
4747
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Tables;
4848

@@ -150,7 +150,7 @@ public SQLite3SelectStatement getQuery(SQLite3GlobalState globalState) throws SQ
150150
if (generateDistinct) {
151151
colName = new SQLite3Distinct(colName);
152152
}
153-
SQLite3AggregateFunction aggFunc = SQLite3AggregateFunction.getRandom(c.getColumnType());
153+
SQLite3AggregateFunction aggFunc = SQLite3AggregateFunction.getRandom(c.getType());
154154
colName = new SQLite3Aggregate(Arrays.asList(colName), aggFunc);
155155
if (Randomly.getBoolean() && !generateDistinct) {
156156
colName = generateWindowFunction(columns, colName, true);

src/sqlancer/sqlite3/schema/SQLite3Schema.java

Lines changed: 15 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import sqlancer.QueryAdapter;
1919
import sqlancer.Randomly;
2020
import sqlancer.StateToReproduce.SQLite3StateToReproduce;
21+
import sqlancer.schema.AbstractTable;
22+
import sqlancer.schema.AbstractTableColumn;
23+
import sqlancer.schema.TableIndex;
2124
import sqlancer.sqlite3.SQLite3Errors;
2225
import sqlancer.sqlite3.SQLite3ToStringVisitor;
2326
import sqlancer.sqlite3.ast.SQLite3Constant;
@@ -49,15 +52,12 @@ public SQLite3Table getRandomTableOrBailout() {
4952
}
5053
}
5154

52-
public static class SQLite3Column implements Comparable<SQLite3Column> {
55+
public static class SQLite3Column extends AbstractTableColumn<SQLite3Table, SQLite3DataType> {
5356

54-
private final String name;
55-
private final SQLite3DataType columnType;
56-
private final boolean isPrimaryKey;
5757
private final boolean isInteger; // "INTEGER" type, not "INT"
58-
private SQLite3Table table;
5958
private final SQLite3CollateSequence collate;
6059
private boolean generated;
60+
private boolean isPrimaryKey;
6161

6262
public enum SQLite3CollateSequence {
6363
NOCASE, RTRIM, BINARY;
@@ -69,8 +69,7 @@ public static SQLite3CollateSequence random() {
6969

7070
public SQLite3Column(String name, SQLite3DataType columnType, boolean isInteger, boolean isPrimaryKey,
7171
SQLite3CollateSequence collate) {
72-
this.name = name;
73-
this.columnType = columnType;
72+
super(name, null, columnType);
7473
this.isInteger = isInteger;
7574
this.isPrimaryKey = isPrimaryKey;
7675
this.collate = collate;
@@ -84,44 +83,12 @@ public SQLite3Column(String rowId, SQLite3DataType columnType2, boolean contains
8483
this.generated = generated;
8584
}
8685

87-
@Override
88-
public String toString() {
89-
return String.format("%s.%s: %s", table.getName(), name, columnType);
90-
}
91-
92-
@Override
93-
public int hashCode() {
94-
return name.hashCode() + 11 * columnType.hashCode();
95-
}
96-
97-
@Override
98-
public boolean equals(Object obj) {
99-
if (!(obj instanceof SQLite3Column)) {
100-
return false;
101-
} else {
102-
SQLite3Column c = (SQLite3Column) obj;
103-
return table.getName().contentEquals(getName()) && name.equals(c.name);
104-
}
105-
}
106-
107-
public String getName() {
108-
return name;
109-
}
110-
111-
public String getFullQualifiedName() {
112-
return table.getName() + "." + getName();
113-
}
114-
115-
public SQLite3DataType getColumnType() {
116-
return columnType;
117-
}
118-
11986
public boolean isPrimaryKey() {
12087
return isPrimaryKey;
12188
}
12289

12390
public boolean isOnlyPrimaryKey() {
124-
return isPrimaryKey && table.getColumns().stream().filter(c -> c.isPrimaryKey()).count() == 1;
91+
return isPrimaryKey && getTable().getColumns().stream().filter(c -> c.isPrimaryKey()).count() == 1;
12592
}
12693

12794
// see https://www.sqlite.org/lang_createtable.html#rowid
@@ -131,24 +98,7 @@ public boolean isOnlyPrimaryKey() {
13198
* column is known as an INTEGER PRIMARY KEY.
13299
*/
133100
public boolean isIntegerPrimaryKey() {
134-
return isInteger && isOnlyPrimaryKey() && !table.hasWithoutRowid();
135-
}
136-
137-
public void setTable(SQLite3Table table) {
138-
this.table = table;
139-
}
140-
141-
public SQLite3Table getTable() {
142-
return table;
143-
}
144-
145-
@Override
146-
public int compareTo(SQLite3Column o) {
147-
if (o.getTable().equals(this.getTable())) {
148-
return name.compareTo(o.getName());
149-
} else {
150-
return o.getTable().compareTo(table);
151-
}
101+
return isInteger && isOnlyPrimaryKey() && !getTable().hasWithoutRowid();
152102
}
153103

154104
public SQLite3CollateSequence getCollateSequence() {
@@ -265,72 +215,35 @@ public SQLite3RowValue getRandomRowValue(Connection con, SQLite3StateToReproduce
265215

266216
}
267217

268-
public static class SQLite3Table implements Comparable<SQLite3Table> {
218+
219+
public static class SQLite3Table extends AbstractTable<SQLite3Column, TableIndex> {
220+
// TODO: why does the SQLite implementation have no table indexes?
269221

270222
public static enum TableKind {
271223
MAIN, TEMP;
272224
}
273225

274-
private final String tableName;
275-
private final List<SQLite3Column> columns;
276226
private final TableKind tableType;
277227
private SQLite3Column rowid;
278228
private boolean withoutRowid;
279229
private int nrRows;
280-
private boolean isView;
281230
private boolean isVirtual;
282231
private boolean isReadOnly;
283232

284233
public SQLite3Table(String tableName, List<SQLite3Column> columns, TableKind tableType, boolean withoutRowid,
285234
int nrRows, boolean isView, boolean isVirtual, boolean isReadOnly) {
286-
this.tableName = tableName;
235+
super(tableName, columns, Collections.emptyList(), isView);
287236
this.tableType = tableType;
288237
this.withoutRowid = withoutRowid;
289-
this.isView = isView;
290238
this.isVirtual = isVirtual;
291239
this.isReadOnly = isReadOnly;
292-
this.columns = Collections.unmodifiableList(columns);
293240
this.nrRows = nrRows;
294241
}
295242

296243
public boolean hasWithoutRowid() {
297244
return withoutRowid;
298245
}
299246

300-
@Override
301-
public String toString() {
302-
StringBuffer sb = new StringBuffer();
303-
sb.append(tableName + "\n");
304-
for (SQLite3Column c : columns) {
305-
sb.append("\t" + c + "\n");
306-
}
307-
return sb.toString();
308-
}
309-
310-
public String getName() {
311-
return tableName;
312-
}
313-
314-
public List<SQLite3Column> getColumns() {
315-
return columns;
316-
}
317-
318-
public String getColumnsAsString() {
319-
return columns.stream().map(c -> c.getName()).collect(Collectors.joining(", "));
320-
}
321-
322-
public String getColumnsAsString(Function<SQLite3Column, String> function) {
323-
return columns.stream().map(function).collect(Collectors.joining(", "));
324-
}
325-
326-
public SQLite3Column getRandomColumn() {
327-
return Randomly.fromList(columns);
328-
}
329-
330-
@Override
331-
public int compareTo(SQLite3Table o) {
332-
return o.getName().compareTo(tableName);
333-
}
334247

335248
public void addRowid(SQLite3Column rowid) {
336249
this.rowid = rowid;
@@ -348,10 +261,6 @@ public boolean isVirtual() {
348261
return isVirtual;
349262
}
350263

351-
public List<SQLite3Column> getRandomNonEmptyColumnSubset() {
352-
return Randomly.nonEmptySubset(getColumns());
353-
}
354-
355264
public boolean isSystemTable() {
356265
return getName().startsWith("sqlit");
357266
}
@@ -360,10 +269,6 @@ public int getNrRows() {
360269
return nrRows;
361270
}
362271

363-
public boolean isView() {
364-
return isView;
365-
}
366-
367272
public boolean isTemp() {
368273
return tableType == TableKind.TEMP;
369274
}
@@ -693,11 +598,11 @@ public SQLite3Table getRandomTableNoViewNoVirtualTable() {
693598
}
694599

695600
public List<SQLite3Table> getDatabaseTablesWithoutViews() {
696-
return databaseTables.stream().filter(t -> !t.isView).collect(Collectors.toList());
601+
return databaseTables.stream().filter(t -> !t.isView()).collect(Collectors.toList());
697602
}
698603

699604
public List<SQLite3Table> getViews() {
700-
return databaseTables.stream().filter(t -> t.isView).collect(Collectors.toList());
605+
return databaseTables.stream().filter(t -> t.isView()).collect(Collectors.toList());
701606
}
702607

703608
public SQLite3Table getRandomViewOrBailout() {
@@ -709,7 +614,7 @@ public SQLite3Table getRandomViewOrBailout() {
709614
}
710615

711616
public List<SQLite3Table> getDatabaseTablesWithoutViewsWithoutVirtualTables() {
712-
return databaseTables.stream().filter(t -> !t.isView && !t.isVirtual).collect(Collectors.toList());
617+
return databaseTables.stream().filter(t -> !t.isView() && !t.isVirtual).collect(Collectors.toList());
713618
}
714619

715620
}

0 commit comments

Comments
 (0)