-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathAbstractTableGenerator.java
More file actions
77 lines (68 loc) · 2.4 KB
/
Copy pathAbstractTableGenerator.java
File metadata and controls
77 lines (68 loc) · 2.4 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
package sqlancer.common.gen;
import java.util.List;
import sqlancer.common.schema.AbstractTableColumn;
public abstract class AbstractTableGenerator<C extends AbstractTableColumn<?, ?>> extends AbstractGenerator {
/**
* Appends {@code CREATE TABLE <name>}.
*
* @param tableName
* the name of the table to create.
*/
protected void appendCreateTable(String tableName) {
appendCreateTable(tableName, false);
}
/**
* Appends {@code CREATE TABLE [IF NOT EXISTS ]<name>}.
*
* @param tableName
* the name of the table to create.
* @param ifNotExists
* whether to emit the {@code IF NOT EXISTS} clause.
*/
protected void appendCreateTable(String tableName, boolean ifNotExists) {
sb.append("CREATE TABLE ");
if (ifNotExists) {
sb.append("IF NOT EXISTS ");
}
sb.append(tableName);
}
/**
* Appends a parenthesized, comma-separated column definition list, e.g. {@code (c0 INT, c1 TEXT)}. Delegates each
* column's rendering to {@link #appendColumnDefinition(AbstractTableColumn)}.
*
* @param columns
* the columns to render.
*/
protected void appendColumnDefinitions(List<C> columns) {
sb.append("(");
appendColumnDefinitionList(columns);
sb.append(")");
}
/**
* Appends a comma-separated column definition list without enclosing parentheses, e.g. {@code c0 INT, c1 TEXT}.
* Useful when subclasses also emit table-level constraints (e.g. {@code PRIMARY KEY (...)}) inside the same parens.
*
* @param columns
* the columns to render.
*/
protected void appendColumnDefinitionList(List<C> columns) {
for (int i = 0; i < columns.size(); i++) {
if (i != 0) {
sb.append(", ");
}
appendColumnDefinition(columns.get(i));
}
}
/**
* Appends a single column's definition. Default output is {@code <name> <type>}, e.g. {@code c0 INT}. Override to
* add constraints such as {@code NOT NULL}, {@code DEFAULT ...}, or {@code CHECK (...)}.
*
* @param column
* the column whose definition to render.
*/
protected void appendColumnDefinition(C column) {
sb.append(column.getName());
sb.append(" ");
sb.append(column.getType());
}
}