forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiDBUpdateGenerator.java
More file actions
57 lines (47 loc) · 1.93 KB
/
TiDBUpdateGenerator.java
File metadata and controls
57 lines (47 loc) · 1.93 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
package sqlancer.tidb.gen;
import java.sql.SQLException;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.gen.AbstractUpdateGenerator;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.tidb.TiDBErrors;
import sqlancer.tidb.TiDBExpressionGenerator;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.TiDBSchema.TiDBColumn;
import sqlancer.tidb.TiDBSchema.TiDBTable;
import sqlancer.tidb.visitor.TiDBVisitor;
public final class TiDBUpdateGenerator extends AbstractUpdateGenerator<TiDBColumn> {
private final TiDBGlobalState globalState;
private TiDBExpressionGenerator gen;
private TiDBUpdateGenerator(TiDBGlobalState globalState) {
this.globalState = globalState;
}
public static SQLQueryAdapter getQuery(TiDBGlobalState globalState) throws SQLException {
return new TiDBUpdateGenerator(globalState).generate();
}
private SQLQueryAdapter generate() throws SQLException {
TiDBTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
List<TiDBColumn> columns = table.getRandomNonEmptyColumnSubset();
gen = new TiDBExpressionGenerator(globalState).setColumns(table.getColumns());
sb.append("UPDATE ");
sb.append(table.getName());
sb.append(" SET ");
updateColumns(columns);
if (Randomly.getBoolean()) {
sb.append(" WHERE ");
TiDBErrors.addExpressionErrors(errors);
sb.append(TiDBVisitor.asString(gen.generateExpression()));
}
TiDBErrors.addInsertErrors(errors);
return new SQLQueryAdapter(sb.toString(), errors);
}
@Override
protected void updateValue(TiDBColumn column) {
if (Randomly.getBoolean()) {
sb.append(gen.generateConstant());
} else {
sb.append(TiDBVisitor.asString(gen.generateExpression()));
TiDBErrors.addExpressionErrors(errors);
}
}
}