Skip to content

Commit 8f172a4

Browse files
committed
Create tableReference class for clean support for subqueries & tables as table reference options
1 parent 464fcd6 commit 8f172a4

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

src/sqlancer/postgres/PostgresExpectedValueVisitor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import sqlancer.postgres.ast.PostgresSelect.PostgresFromTable;
1919
import sqlancer.postgres.ast.PostgresSelect.PostgresCTE;
2020
import sqlancer.postgres.ast.PostgresSimilarTo;
21+
import sqlancer.postgres.ast.PostgresJoin.PostgresTableReference;
2122

2223
public final class PostgresExpectedValueVisitor implements PostgresVisitor {
2324

@@ -161,4 +162,9 @@ public void visit(PostgresCTE cte) {
161162
print(cte);
162163
}
163164

165+
@Override
166+
public void visit(PostgresTableReference ref) {
167+
print(ref);
168+
}
169+
164170
}

src/sqlancer/postgres/PostgresToStringVisitor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import sqlancer.postgres.ast.PostgresInOperation;
1616
import sqlancer.postgres.ast.PostgresJoin;
1717
import sqlancer.postgres.ast.PostgresJoin.PostgresJoinType;
18+
import sqlancer.postgres.ast.PostgresJoin.PostgresTableReference;
1819
import sqlancer.postgres.ast.PostgresOrderByTerm;
1920
import sqlancer.postgres.ast.PostgresPOSIXRegularExpression;
2021
import sqlancer.postgres.ast.PostgresPostfixOperation;
@@ -84,6 +85,11 @@ public void visit(PostgresCTE cte) {
8485
sb.append(cte.getName());
8586
}
8687

88+
@Override
89+
public void visit(PostgresTableReference ref) {
90+
visit(ref.getTableReference());
91+
}
92+
8793
@Override
8894
public void visit(PostgresSelect s) {
8995
sb.append("SELECT ");
@@ -135,11 +141,7 @@ public void visit(PostgresSelect s) {
135141
throw new AssertionError(j.getType());
136142
}
137143
sb.append(" ");
138-
if (j.joinCTE()) {
139-
visit(j.getCTE());
140-
} else {
141-
sb.append(j.getTable().getName());
142-
}
144+
visit(j.getTableReference());
143145
if (j.getType() != PostgresJoinType.CROSS) {
144146
sb.append(" ON ");
145147
visit(j.getOnClause());

src/sqlancer/postgres/PostgresVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import sqlancer.postgres.ast.PostgresSelect.PostgresCTE;
2424
import sqlancer.postgres.ast.PostgresSimilarTo;
2525
import sqlancer.postgres.gen.PostgresExpressionGenerator;
26+
import sqlancer.postgres.ast.PostgresJoin.PostgresTableReference;;
2627

2728
public interface PostgresVisitor {
2829

@@ -60,6 +61,8 @@ public interface PostgresVisitor {
6061

6162
void visit(PostgresCTE cte);
6263

64+
void visit(PostgresTableReference ref);
65+
6366
default void visit(PostgresExpression expression) {
6467
if (expression instanceof PostgresConstant) {
6568
visit((PostgresConstant) expression);
@@ -95,6 +98,8 @@ default void visit(PostgresExpression expression) {
9598
visit((PostgresFromTable) expression);
9699
} else if (expression instanceof PostgresCTE) {
97100
visit((PostgresCTE) expression);
101+
} else if (expression instanceof PostgresTableReference) {
102+
visit((PostgresTableReference) expression);
98103
} else {
99104
throw new AssertionError(expression);
100105
}

src/sqlancer/postgres/ast/PostgresJoin.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sqlancer.postgres.PostgresSchema.PostgresDataType;
55
import sqlancer.postgres.PostgresSchema.PostgresTable;
66
import sqlancer.postgres.ast.PostgresSelect.PostgresCTE;
7+
import sqlancer.postgres.ast.PostgresSelect.PostgresFromTable;
78

89
public class PostgresJoin implements PostgresExpression {
910

@@ -16,34 +17,36 @@ public static PostgresJoinType getRandom() {
1617

1718
}
1819

19-
private final PostgresTable table;
20+
private final PostgresTableReference tableReference;
2021
private final PostgresExpression onClause;
2122
private final PostgresJoinType type;
22-
private PostgresCTE CTE = null;
2323

24-
public PostgresJoin(PostgresTable table, PostgresExpression onClause, PostgresJoinType type) {
25-
this.table = table;
26-
this.onClause = onClause;
27-
this.type = type;
28-
}
24+
public static class PostgresTableReference implements PostgresExpression {
2925

30-
public PostgresJoin(PostgresCTE CTE, PostgresExpression onClause, PostgresJoinType type) {
31-
this.CTE = CTE;
32-
this.onClause = onClause;
33-
this.type = type;
34-
this.table = null;
35-
}
26+
private final PostgresExpression tableReference;
27+
28+
public PostgresTableReference(PostgresCTE cte) {
29+
this.tableReference = cte;
30+
}
31+
32+
public PostgresTableReference(PostgresTable table) {
33+
this.tableReference = new PostgresFromTable(table, Randomly.getBoolean());
34+
}
35+
36+
public PostgresExpression getTableReference() {
37+
return tableReference;
38+
}
3639

37-
public PostgresTable getTable() {
38-
return table;
3940
}
4041

41-
public PostgresCTE getCTE() {
42-
return CTE;
42+
public PostgresJoin(PostgresTableReference tableReference, PostgresExpression onClause, PostgresJoinType type) {
43+
this.tableReference = tableReference;
44+
this.onClause = onClause;
45+
this.type = type;
4346
}
4447

45-
public boolean joinCTE() {
46-
return (CTE != null);
48+
public PostgresTableReference getTableReference() {
49+
return tableReference;
4750
}
4851

4952
public PostgresExpression getOnClause() {

src/sqlancer/postgres/oracle/PostgresNoRECOracle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import sqlancer.postgres.ast.PostgresExpression;
2828
import sqlancer.postgres.ast.PostgresJoin;
2929
import sqlancer.postgres.ast.PostgresJoin.PostgresJoinType;
30+
import sqlancer.postgres.ast.PostgresJoin.PostgresTableReference;
3031
import sqlancer.postgres.ast.PostgresPostfixText;
3132
import sqlancer.postgres.ast.PostgresSelect;
3233
import sqlancer.postgres.ast.PostgresSelect.PostgresFromTable;
@@ -83,7 +84,7 @@ public static List<PostgresJoin> getJoinStatements(PostgresGlobalState globalSta
8384
PostgresTable table = Randomly.fromList(tables);
8485
tables.remove(table);
8586
PostgresJoinType options = PostgresJoinType.getRandom();
86-
PostgresJoin j = new PostgresJoin(table, joinClause, options);
87+
PostgresJoin j = new PostgresJoin(new PostgresTableReference(table), joinClause, options);
8788
joinStatements.add(j);
8889
}
8990
return joinStatements;

0 commit comments

Comments
 (0)