-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLite3ColumnBuilder.java
More file actions
164 lines (146 loc) · 6.05 KB
/
SQLite3ColumnBuilder.java
File metadata and controls
164 lines (146 loc) · 6.05 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package sqlancer.sqlite3.gen;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.sqlite3.SQLite3GlobalState;
import sqlancer.sqlite3.SQLite3OracleFactory;
import sqlancer.sqlite3.SQLite3Visitor;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
public class SQLite3ColumnBuilder {
private boolean containsPrimaryKey;
private boolean containsAutoIncrement;
private final StringBuilder sb = new StringBuilder();
private boolean conflictClauseInserted;
private boolean allowPrimaryKey = true;
private boolean allowUnique = true;
private boolean allowDefaultValue = true;
private boolean allowCheck = true;
private boolean allowNotNull = true;
private enum Constraints {
NOT_NULL, PRIMARY_KEY, UNIQUE, CHECK, GENERATED_AS
}
public boolean isContainsAutoIncrement() {
return containsAutoIncrement;
}
public boolean isConflictClauseInserted() {
return conflictClauseInserted;
}
public boolean isContainsPrimaryKey() {
return containsPrimaryKey;
}
public String createColumn(String columnName, SQLite3GlobalState globalState, List<SQLite3Column> columns) {
if (globalState.getDbmsSpecificOptions().oracles == SQLite3OracleFactory.PQS
|| !globalState.getDbmsSpecificOptions().testCheckConstraints) {
allowCheck = false;
}
sb.append(columnName);
sb.append(" ");
String dataType = Randomly.fromOptions("INT", "TEXT", "BLOB", "REAL", "INTEGER");
sb.append(dataType);
if (Randomly.getBooleanWithRatherLowProbability()) {
List<Constraints> constraints = Randomly.subset(Constraints.values());
if (!Randomly.getBooleanWithSmallProbability()
|| !globalState.getDbmsSpecificOptions().testGeneratedColumns) {
constraints.remove(Constraints.GENERATED_AS);
}
if (constraints.contains(Constraints.GENERATED_AS)) {
allowDefaultValue = false;
allowPrimaryKey = false;
}
for (Constraints c : constraints) {
switch (c) {
case GENERATED_AS:
sb.append(" GENERATED ALWAYS AS (");
sb.append(SQLite3Visitor.asString(new SQLite3ExpressionGenerator(globalState)
.deterministicOnly().setColumns(columns.stream()
.filter(p -> !p.getName().contentEquals(columnName)).collect(Collectors.toList()))
.generateExpression()));
sb.append(")");
break;
case PRIMARY_KEY:
// only one primary key is allow if not specified as table constraint
if (allowPrimaryKey) {
sb.append(" PRIMARY KEY");
containsPrimaryKey = true;
boolean hasOrdering = Randomly.getBoolean();
if (hasOrdering) {
if (Randomly.getBoolean()) {
sb.append(" ASC");
} else {
sb.append(" DESC");
}
}
if (Randomly.getBoolean()) {
insertOnConflictClause();
}
if (!hasOrdering && dataType.equals("INTEGER") && Randomly.getBoolean()) {
containsAutoIncrement = true;
sb.append(" AUTOINCREMENT");
}
}
break;
case UNIQUE:
if (allowUnique) {
sb.append(" UNIQUE");
if (Randomly.getBoolean()) {
insertOnConflictClause();
}
}
break;
case NOT_NULL:
if (allowNotNull) {
sb.append(" NOT NULL");
if (Randomly.getBoolean()) {
insertOnConflictClause();
}
}
break;
case CHECK:
if (allowCheck) {
sb.append(SQLite3Common.getCheckConstraint(globalState, columns));
}
break;
default:
throw new AssertionError();
}
}
}
if (allowDefaultValue && Randomly.getBooleanWithSmallProbability()) {
sb.append(" DEFAULT ");
sb.append(SQLite3Visitor.asString(SQLite3ExpressionGenerator.getRandomLiteralValue(globalState)));
}
if (Randomly.getBooleanWithSmallProbability()) {
String randomCollate = SQLite3Common.getRandomCollate();
sb.append(randomCollate);
}
return sb.toString();
}
// it seems that only one conflict clause can be inserted
private void insertOnConflictClause() {
if (!conflictClauseInserted) {
sb.append(" ON CONFLICT ");
sb.append(Randomly.fromOptions("ROLLBACK", "ABORT", "FAIL", "IGNORE", "REPLACE"));
conflictClauseInserted = true;
}
}
public SQLite3ColumnBuilder allowPrimaryKey(boolean allowPrimaryKey) {
this.allowPrimaryKey = allowPrimaryKey;
return this;
}
public SQLite3ColumnBuilder allowUnique(boolean allowUnique) {
this.allowUnique = allowUnique;
return this;
}
public SQLite3ColumnBuilder allowDefaultValue(boolean allowDefaultValue) {
this.allowDefaultValue = allowDefaultValue;
return this;
}
public SQLite3ColumnBuilder allowCheck(boolean allowCheck) {
this.allowCheck = allowCheck;
return this;
}
public SQLite3ColumnBuilder allowNotNull(boolean allowNotNull) {
this.allowNotNull = allowNotNull;
return this;
}
}