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