|
1 | 1 | package sqlancer.mysql.ast; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import sqlancer.Randomly; |
| 8 | +import sqlancer.mysql.MySQLGlobalState; |
| 9 | +import sqlancer.mysql.MySQLSchema.MySQLColumn; |
| 10 | +import sqlancer.mysql.MySQLSchema.MySQLTable; |
| 11 | +import sqlancer.mysql.gen.MySQLExpressionGenerator; |
| 12 | + |
3 | 13 | public class MySQLJoin implements MySQLExpression { |
4 | 14 |
|
5 | | - @Override |
6 | | - public MySQLConstant getExpectedValue() { |
7 | | - throw new UnsupportedOperationException(); |
| 15 | + public enum JoinType { |
| 16 | + NATURAL, INNER, STRAIGHT, LEFT, RIGHT, CROSS; |
| 17 | + } |
| 18 | + |
| 19 | + private final MySQLTable table; |
| 20 | + private MySQLExpression onClause; |
| 21 | + private JoinType type; |
| 22 | + |
| 23 | + public MySQLJoin(MySQLJoin other) { |
| 24 | + this.table = other.table; |
| 25 | + this.onClause = other.onClause; |
| 26 | + this.type = other.type; |
8 | 27 | } |
9 | 28 |
|
| 29 | + public MySQLJoin(MySQLTable table, MySQLExpression onClause, JoinType type) { |
| 30 | + this.table = table; |
| 31 | + this.onClause = onClause; |
| 32 | + this.type = type; |
| 33 | + } |
| 34 | + |
| 35 | + public MySQLTable getTable() { |
| 36 | + return table; |
| 37 | + } |
| 38 | + |
| 39 | + public MySQLExpression getOnClause() { |
| 40 | + return onClause; |
| 41 | + } |
| 42 | + |
| 43 | + public JoinType getType() { |
| 44 | + return type; |
| 45 | + } |
| 46 | + |
| 47 | + public void setOnClause(MySQLExpression onClause) { |
| 48 | + this.onClause = onClause; |
| 49 | + } |
| 50 | + |
| 51 | + public void setType(JoinType type) { |
| 52 | + this.type = type; |
| 53 | + } |
| 54 | + |
| 55 | + public static List<MySQLJoin> getRandomJoinClauses(List<MySQLTable> tables, MySQLGlobalState globalState) { |
| 56 | + List<MySQLJoin> joinStatements = new ArrayList<>(); |
| 57 | + List<JoinType> options = new ArrayList<>(Arrays.asList(JoinType.values())); |
| 58 | + List<MySQLColumn> columns = new ArrayList<>(); |
| 59 | + if (tables.size() > 1) { |
| 60 | + int nrJoinClauses = (int) Randomly.getNotCachedInteger(0, tables.size()); |
| 61 | + // Natural join is incompatible with other joins |
| 62 | + // because it needs unique column names |
| 63 | + // while other joins will produce duplicate column names |
| 64 | + if (nrJoinClauses > 1) { |
| 65 | + options.remove(JoinType.NATURAL); |
| 66 | + } |
| 67 | + for (int i = 0; i < nrJoinClauses; i++) { |
| 68 | + MySQLTable table = Randomly.fromList(tables); |
| 69 | + tables.remove(table); |
| 70 | + columns.addAll(table.getColumns()); |
| 71 | + MySQLExpressionGenerator joinGen = new MySQLExpressionGenerator(globalState).setColumns(columns); |
| 72 | + MySQLExpression joinClause = joinGen.generateExpression(); |
| 73 | + JoinType selectedOption = Randomly.fromList(options); |
| 74 | + if (selectedOption == JoinType.NATURAL) { |
| 75 | + // NATURAL joins do not have an ON clause |
| 76 | + joinClause = null; |
| 77 | + } |
| 78 | + MySQLJoin j = new MySQLJoin(table, joinClause, selectedOption); |
| 79 | + joinStatements.add(j); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + return joinStatements; |
| 84 | + } |
10 | 85 | } |
0 commit comments