1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import com.healthmarketscience.jackcess.impl.DatabaseImpl;
30 import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
31 import com.healthmarketscience.jackcess.impl.TableCreator;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class TableBuilder {
54
55
56 private static final String ESCAPE_PREFIX = "x";
57
58
59 private static final class ReservedWords {
60
61
62
63
64 private static final Set<String> VALUES =
65 new HashSet<String>(Arrays.asList(
66 "add", "all", "alphanumeric", "alter", "and", "any", "application", "as",
67 "asc", "assistant", "autoincrement", "avg", "between", "binary", "bit",
68 "boolean", "by", "byte", "char", "character", "column", "compactdatabase",
69 "constraint", "container", "count", "counter", "create", "createdatabase",
70 "createfield", "creategroup", "createindex", "createobject", "createproperty",
71 "createrelation", "createtabledef", "createuser", "createworkspace",
72 "currency", "currentuser", "database", "date", "datetime", "delete",
73 "desc", "description", "disallow", "distinct", "distinctrow", "document",
74 "double", "drop", "echo", "else", "end", "eqv", "error", "exists", "exit",
75 "false", "field", "fields", "fillcache", "float", "float4", "float8",
76 "foreign", "form", "forms", "from", "full", "function", "general",
77 "getobject", "getoption", "gotopage", "group", "group by", "guid", "having",
78 "idle", "ieeedouble", "ieeesingle", "if", "ignore", "imp", "in", "index",
79 "indexes", "inner", "insert", "inserttext", "int", "integer", "integer1",
80 "integer2", "integer4", "into", "is", "join", "key", "lastmodified", "left",
81 "level", "like", "logical", "logical1", "long", "longbinary", "longtext",
82 "macro", "match", "max", "min", "mod", "memo", "module", "money", "move",
83 "name", "newpassword", "no", "not", "null", "number", "numeric", "object",
84 "oleobject", "off", "on", "openrecordset", "option", "or", "order", "outer",
85 "owneraccess", "parameter", "parameters", "partial", "percent", "pivot",
86 "primary", "procedure", "property", "queries", "query", "quit", "real",
87 "recalc", "recordset", "references", "refresh", "refreshlink",
88 "registerdatabase", "relation", "repaint", "repairdatabase", "report",
89 "reports", "requery", "right", "screen", "section", "select", "set",
90 "setfocus", "setoption", "short", "single", "smallint", "some", "sql",
91 "stdev", "stdevp", "string", "sum", "table", "tabledef", "tabledefs",
92 "tableid", "text", "time", "timestamp", "top", "transform", "true", "type",
93 "union", "unique", "update", "user", "value", "values", "var", "varp",
94 "varbinary", "varchar", "where", "with", "workspace", "xor", "year", "yes",
95 "yesno"));
96 }
97
98
99
100 private String _name;
101
102 private List<ColumnBuilder> _columns = new ArrayList<ColumnBuilder>();
103
104 private List<IndexBuilder> _indexes = new ArrayList<IndexBuilder>();
105
106 private boolean _escapeIdentifiers;
107
108 private Map<String,PropertyMap.Property> _props;
109
110
111 public TableBuilder(String name) {
112 this(name, false);
113 }
114
115 public TableBuilder(String name, boolean escapeIdentifiers) {
116 _name = name;
117 _escapeIdentifiers = escapeIdentifiers;
118 if(_escapeIdentifiers) {
119 _name = escapeIdentifier(_name);
120 }
121 }
122
123 public String getName() {
124 return _name;
125 }
126
127
128
129
130 public TableBuilder addColumn(ColumnBuilder column) {
131 if(_escapeIdentifiers) {
132 column.escapeName();
133 }
134 _columns.add(column);
135 return this;
136 }
137
138
139
140
141 public TableBuilder addColumns(Collection<? extends ColumnBuilder> columns) {
142 if(columns != null) {
143 for(ColumnBuilder col : columns) {
144 addColumn(col);
145 }
146 }
147 return this;
148 }
149
150 public List<ColumnBuilder> getColumns() {
151 return _columns;
152 }
153
154
155
156
157 public TableBuilder addIndex(IndexBuilder index) {
158 if(_escapeIdentifiers) {
159 index.setName(escapeIdentifier(index.getName()));
160 for(IndexBuilder.Column col : index.getColumns()) {
161 col.setName(escapeIdentifier(col.getName()));
162 }
163 }
164 _indexes.add(index);
165 return this;
166 }
167
168
169
170
171 public TableBuilder addIndexes(Collection<? extends IndexBuilder> indexes) {
172 if(indexes != null) {
173 for(IndexBuilder col : indexes) {
174 addIndex(col);
175 }
176 }
177 return this;
178 }
179
180 public List<IndexBuilder> getIndexes() {
181 return _indexes;
182 }
183
184
185
186
187
188 public TableBuilder setEscapeIdentifiers(boolean escapeIdentifiers) {
189 _escapeIdentifiers = escapeIdentifiers;
190 return this;
191 }
192
193
194
195
196
197 public TableBuilder setPrimaryKey(String... colNames) {
198 addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
199 .addColumns(colNames).setPrimaryKey());
200 return this;
201 }
202
203
204
205
206 public TableBuilder escapeName() {
207 _name = escapeIdentifier(_name);
208 return this;
209 }
210
211
212
213
214
215
216
217 public TableBuilder putProperty(String name, Object value) {
218 return putProperty(name, null, value);
219 }
220
221
222
223
224 public TableBuilder putProperty(String name, DataType type, Object value) {
225 if(_props == null) {
226 _props = new HashMap<String,PropertyMap.Property>();
227 }
228 _props.put(name, PropertyMapImpl.createProperty(name, type, value));
229 return this;
230 }
231
232 public Map<String,PropertyMap.Property> getProperties() {
233 return _props;
234 }
235
236
237
238
239
240 public Table toTable(Database db) throws IOException {
241 return new TableCreator(((DatabaseImpl)db)).createTable(this);
242 }
243
244
245
246
247
248 public static String escapeIdentifier(String s) {
249 if (isReservedWord(s)) {
250 return ESCAPE_PREFIX + s;
251 }
252 return s;
253 }
254
255
256
257
258
259
260 public static boolean isReservedWord(String s) {
261 return ReservedWords.VALUES.contains(s.toLowerCase());
262 }
263
264 }