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.File;
20 import java.io.IOException;
21 import java.nio.channels.FileChannel;
22 import java.nio.charset.Charset;
23 import java.nio.file.Path;
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.TimeZone;
31
32 import com.healthmarketscience.jackcess.impl.CodecProvider;
33 import com.healthmarketscience.jackcess.impl.DatabaseImpl;
34 import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
35 import com.healthmarketscience.jackcess.util.MemFileChannel;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class DatabaseBuilder
56 {
57
58 private Path _mdbFile;
59
60 private boolean _readOnly;
61
62 private boolean _autoSync = Database.DEFAULT_AUTO_SYNC;
63
64 private Charset _charset;
65
66 private TimeZone _timeZone;
67
68 private CodecProvider _codecProvider;
69
70 private Database.FileFormat _fileFormat;
71
72
73 private FileChannel _channel;
74
75 private Map<String,PropertyMap.Property> _dbProps;
76
77 private Map<String,PropertyMap.Property> _summaryProps;
78
79 private Map<String,PropertyMap.Property> _userProps;
80
81 private boolean _ignoreBrokenSystemCatalogIndex;
82
83 public DatabaseBuilder() {
84 this((Path)null);
85 }
86
87 public DatabaseBuilder(File mdbFile) {
88 this(toPath(mdbFile));
89 }
90
91 public DatabaseBuilder(Path mdbFile) {
92 _mdbFile = mdbFile;
93 }
94
95
96
97
98
99
100
101 public DatabaseBuilder setFile(File mdbFile) {
102 return setPath(toPath(mdbFile));
103 }
104
105
106
107
108
109
110
111 public DatabaseBuilder setPath(Path mdbFile) {
112 _mdbFile = mdbFile;
113 return this;
114 }
115
116
117
118
119
120
121 public DatabaseBuilder setReadOnly(boolean readOnly) {
122 _readOnly = readOnly;
123 return this;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137 public DatabaseBuilder setAutoSync(boolean autoSync) {
138 _autoSync = autoSync;
139 return this;
140 }
141
142
143
144
145
146 public DatabaseBuilder setCharset(Charset charset) {
147 _charset = charset;
148 return this;
149 }
150
151
152
153
154
155
156 public DatabaseBuilder setTimeZone(TimeZone timeZone) {
157 _timeZone = timeZone;
158 return this;
159 }
160
161
162
163
164
165
166 public DatabaseBuilder setCodecProvider(CodecProvider codecProvider) {
167 _codecProvider = codecProvider;
168 return this;
169 }
170
171
172
173
174
175 public DatabaseBuilder setFileFormat(Database.FileFormat fileFormat) {
176 _fileFormat = fileFormat;
177 return this;
178 }
179
180
181
182
183
184
185
186
187
188 public DatabaseBuilder setChannel(FileChannel channel) {
189 _channel = channel;
190 return this;
191 }
192
193
194
195
196
197
198
199 public DatabaseBuilder putDatabaseProperty(String name, Object value) {
200 return putDatabaseProperty(name, null, value);
201 }
202
203
204
205
206
207 public DatabaseBuilder putDatabaseProperty(String name, DataType type,
208 Object value) {
209 _dbProps = putProperty(_dbProps, name, type, value);
210 return this;
211 }
212
213
214
215
216
217
218
219 public DatabaseBuilder putSummaryProperty(String name, Object value) {
220 return putSummaryProperty(name, null, value);
221 }
222
223
224
225
226
227 public DatabaseBuilder putSummaryProperty(String name, DataType type,
228 Object value) {
229 _summaryProps = putProperty(_summaryProps, name, type, value);
230 return this;
231 }
232
233
234
235
236
237
238
239 public DatabaseBuilder putUserDefinedProperty(String name, Object value) {
240 return putUserDefinedProperty(name, null, value);
241 }
242
243
244
245
246
247 public DatabaseBuilder putUserDefinedProperty(String name, DataType type,
248 Object value) {
249 _userProps = putProperty(_userProps, name, type, value);
250 return this;
251 }
252
253 private static Map<String,PropertyMap.Property> putProperty(
254 Map<String,PropertyMap.Property> props, String name, DataType type,
255 Object value)
256 {
257 if(props == null) {
258 props = new HashMap<String,PropertyMap.Property>();
259 }
260 props.put(name, PropertyMapImpl.createProperty(name, type, value));
261 return props;
262 }
263
264
265
266
267
268
269 public DatabaseBuilder setIgnoreBrokenSystemCatalogIndex(boolean ignore) {
270 _ignoreBrokenSystemCatalogIndex = ignore;
271 return this;
272 }
273
274
275
276
277 public Database open() throws IOException {
278 return DatabaseImpl.open(_mdbFile, _readOnly, _channel, _autoSync, _charset,
279 _timeZone, _codecProvider,
280 _ignoreBrokenSystemCatalogIndex);
281 }
282
283
284
285
286 public Database create() throws IOException {
287 Database db = DatabaseImpl.create(_fileFormat, _mdbFile, _channel, _autoSync,
288 _charset, _timeZone);
289 if(_dbProps != null) {
290 PropertyMap props = db.getDatabaseProperties();
291 props.putAll(_dbProps.values());
292 props.save();
293 }
294 if(_summaryProps != null) {
295 PropertyMap props = db.getSummaryProperties();
296 props.putAll(_summaryProps.values());
297 props.save();
298 }
299 if(_userProps != null) {
300 PropertyMap props = db.getUserDefinedProperties();
301 props.putAll(_userProps.values());
302 props.save();
303 }
304 return db;
305 }
306
307
308
309
310
311
312
313
314
315
316
317 public static Database open(File mdbFile) throws IOException {
318 return new DatabaseBuilder(mdbFile).open();
319 }
320
321
322
323
324
325
326
327
328
329
330
331 public static Database open(Path mdbFile) throws IOException {
332 return new DatabaseBuilder(mdbFile).open();
333 }
334
335
336
337
338
339
340
341
342
343
344
345 public static Database create(Database.FileFormat fileFormat, File mdbFile)
346 throws IOException
347 {
348 return new DatabaseBuilder(mdbFile).setFileFormat(fileFormat).create();
349 }
350
351
352
353
354
355
356 public static SimpleDateFormat createDateFormat(String formatStr) {
357 SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
358 toCompatibleCalendar(sdf.getCalendar());
359 return sdf;
360 }
361
362
363
364
365
366
367
368 public static Calendar toCompatibleCalendar(Calendar cal) {
369 if(cal instanceof GregorianCalendar) {
370 ((GregorianCalendar)cal).setGregorianChange(new Date(Long.MIN_VALUE));
371 }
372 return cal;
373 }
374
375
376
377
378 public static DatabaseBuilder newDatabase() {
379 return new DatabaseBuilder();
380 }
381
382
383
384
385 public static DatabaseBuilder newDatabase(Path path) {
386 return new DatabaseBuilder(path);
387 }
388
389
390
391
392 public static DatabaseBuilder newDatabase(File file) {
393 return new DatabaseBuilder(file);
394 }
395
396
397
398
399 public static TableBuilder newTable(String name) {
400 return new TableBuilder(name);
401 }
402
403
404
405
406 public static TableBuilder newTable(String name, boolean escapeIdentifiers) {
407 return new TableBuilder(name, escapeIdentifiers);
408 }
409
410
411
412
413 public static ColumnBuilder newColumn(String name) {
414 return new ColumnBuilder(name);
415 }
416
417
418
419
420 public static ColumnBuilder newColumn(String name, DataType type) {
421 return new ColumnBuilder(name, type);
422 }
423
424
425
426
427 public static IndexBuilder newIndex(String name) {
428 return new IndexBuilder(name);
429 }
430
431
432
433
434 public static IndexBuilder newPrimaryKey(String... colNames) {
435 return new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
436 .addColumns(colNames)
437 .setPrimaryKey();
438 }
439
440
441
442
443 public static RelationshipBuilder newRelationship(
444 String fromTable, String toTable) {
445 return new RelationshipBuilder(fromTable, toTable);
446 }
447
448
449
450
451 public static RelationshipBuilder newRelationship(
452 Table../../../com/healthmarketscience/jackcess/Table.html#Table">Table fromTable, Table toTable) {
453 return new RelationshipBuilder(fromTable, toTable);
454 }
455
456 private static Path toPath(File file) {
457 return ((file != null) ? file.toPath() : null);
458 }
459 }