您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DatabaseBuilder.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. Copyright (c) 2012 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.nio.channels.FileChannel;
  17. import java.nio.charset.Charset;
  18. import java.nio.file.Path;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.GregorianCalendar;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.TimeZone;
  26. import com.healthmarketscience.jackcess.impl.CodecProvider;
  27. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  28. import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
  29. import com.healthmarketscience.jackcess.util.MemFileChannel;
  30. /**
  31. * Builder style class for opening/creating a {@link Database}.
  32. * <p>
  33. * Simple example usage:
  34. * <pre>
  35. * Database db = DatabaseBuilder.open(new File("test.mdb"));
  36. * </pre>
  37. * <p>
  38. * Advanced example usage:
  39. * <pre>
  40. * Database db = new DatabaseBuilder(new File("test.mdb"))
  41. * .setReadOnly(true)
  42. * .open();
  43. * </pre>
  44. *
  45. * @author James Ahlborn
  46. * @usage _general_class_
  47. */
  48. public class DatabaseBuilder
  49. {
  50. /** the file name of the mdb to open/create */
  51. private Path _mdbFile;
  52. /** whether or not to open existing mdb read-only */
  53. private boolean _readOnly;
  54. /** whether or not to auto-sync writes to the filesystem */
  55. private boolean _autoSync = Database.DEFAULT_AUTO_SYNC;
  56. /** optional charset for mdbs with unspecified charsets */
  57. private Charset _charset;
  58. /** optional timezone override for interpreting dates */
  59. private TimeZone _timeZone;
  60. /** optional CodecProvider for handling encoded mdbs */
  61. private CodecProvider _codecProvider;
  62. /** FileFormat to use when creating a new mdb */
  63. private Database.FileFormat _fileFormat;
  64. /** optional pre-opened FileChannel, will _not_ be closed by Database
  65. close */
  66. private FileChannel _channel;
  67. /** database properties (if any) */
  68. private Map<String,PropertyMap.Property> _dbProps;
  69. /** database summary properties (if any) */
  70. private Map<String,PropertyMap.Property> _summaryProps;
  71. /** database user-defined (if any) */
  72. private Map<String,PropertyMap.Property> _userProps;
  73. public DatabaseBuilder() {
  74. this((Path)null);
  75. }
  76. public DatabaseBuilder(File mdbFile) {
  77. this(toPath(mdbFile));
  78. }
  79. public DatabaseBuilder(Path mdbFile) {
  80. _mdbFile = mdbFile;
  81. }
  82. /**
  83. * File containing an existing database for {@link #open} or target file for
  84. * new database for {@link #create} (in which case, <b>tf this file already
  85. * exists, it will be overwritten.</b>)
  86. * @usage _general_method_
  87. */
  88. public DatabaseBuilder setFile(File mdbFile) {
  89. return setPath(toPath(mdbFile));
  90. }
  91. /**
  92. * File containing an existing database for {@link #open} or target file for
  93. * new database for {@link #create} (in which case, <b>tf this file already
  94. * exists, it will be overwritten.</b>)
  95. * @usage _general_method_
  96. */
  97. public DatabaseBuilder setPath(Path mdbFile) {
  98. _mdbFile = mdbFile;
  99. return this;
  100. }
  101. /**
  102. * Sets flag which, iff {@code true}, will force opening file in
  103. * read-only mode ({@link #open} only).
  104. * @usage _general_method_
  105. */
  106. public DatabaseBuilder setReadOnly(boolean readOnly) {
  107. _readOnly = readOnly;
  108. return this;
  109. }
  110. /**
  111. * Sets whether or not to enable auto-syncing on write. if {@code true},
  112. * write operations will be immediately flushed to disk upon completion.
  113. * This leaves the database in a (fairly) consistent state on each write,
  114. * but can be very inefficient for many updates. if {@code false}, flushing
  115. * to disk happens at the jvm's leisure, which can be much faster, but may
  116. * leave the database in an inconsistent state if failures are encountered
  117. * during writing. Writes may be flushed at any time using {@link
  118. * Database#flush}.
  119. * @usage _intermediate_method_
  120. */
  121. public DatabaseBuilder setAutoSync(boolean autoSync) {
  122. _autoSync = autoSync;
  123. return this;
  124. }
  125. /**
  126. * Sets the Charset to use, if {@code null}, uses default.
  127. * @usage _intermediate_method_
  128. */
  129. public DatabaseBuilder setCharset(Charset charset) {
  130. _charset = charset;
  131. return this;
  132. }
  133. /**
  134. * Sets the TimeZone to use for interpreting dates, if {@code null}, uses
  135. * default
  136. * @usage _intermediate_method_
  137. */
  138. public DatabaseBuilder setTimeZone(TimeZone timeZone) {
  139. _timeZone = timeZone;
  140. return this;
  141. }
  142. /**
  143. * Sets the CodecProvider for handling page encoding/decoding, may be
  144. * {@code null} if no special encoding is necessary
  145. * @usage _intermediate_method_
  146. */
  147. public DatabaseBuilder setCodecProvider(CodecProvider codecProvider) {
  148. _codecProvider = codecProvider;
  149. return this;
  150. }
  151. /**
  152. * Sets the version of new database ({@link #create} only).
  153. * @usage _general_method_
  154. */
  155. public DatabaseBuilder setFileFormat(Database.FileFormat fileFormat) {
  156. _fileFormat = fileFormat;
  157. return this;
  158. }
  159. /**
  160. * Sets a pre-opened FileChannel. if provided explicitly, <i>it will not be
  161. * closed by the Database instance</i>. This allows ultimate control of
  162. * where the mdb file exists (which may not be on disk, e.g.
  163. * {@link MemFileChannel}). If provided, the File parameter will be
  164. * available from {@link Database#getFile}, but otherwise ignored.
  165. * @usage _advanced_method_
  166. */
  167. public DatabaseBuilder setChannel(FileChannel channel) {
  168. _channel = channel;
  169. return this;
  170. }
  171. /**
  172. * Sets the database property with the given name to the given value.
  173. * Attempts to determine the type of the property (see
  174. * {@link PropertyMap#put(String,Object)} for details on determining the
  175. * property type).
  176. */
  177. public DatabaseBuilder putDatabaseProperty(String name, Object value) {
  178. return putDatabaseProperty(name, null, value);
  179. }
  180. /**
  181. * Sets the database property with the given name and type to the given
  182. * value.
  183. */
  184. public DatabaseBuilder putDatabaseProperty(String name, DataType type,
  185. Object value) {
  186. _dbProps = putProperty(_dbProps, name, type, value);
  187. return this;
  188. }
  189. /**
  190. * Sets the summary database property with the given name to the given
  191. * value. Attempts to determine the type of the property (see
  192. * {@link PropertyMap#put(String,Object)} for details on determining the
  193. * property type).
  194. */
  195. public DatabaseBuilder putSummaryProperty(String name, Object value) {
  196. return putSummaryProperty(name, null, value);
  197. }
  198. /**
  199. * Sets the summary database property with the given name and type to
  200. * the given value.
  201. */
  202. public DatabaseBuilder putSummaryProperty(String name, DataType type,
  203. Object value) {
  204. _summaryProps = putProperty(_summaryProps, name, type, value);
  205. return this;
  206. }
  207. /**
  208. * Sets the user-defined database property with the given name to the given
  209. * value. Attempts to determine the type of the property (see
  210. * {@link PropertyMap#put(String,Object)} for details on determining the
  211. * property type).
  212. */
  213. public DatabaseBuilder putUserDefinedProperty(String name, Object value) {
  214. return putUserDefinedProperty(name, null, value);
  215. }
  216. /**
  217. * Sets the user-defined database property with the given name and type to
  218. * the given value.
  219. */
  220. public DatabaseBuilder putUserDefinedProperty(String name, DataType type,
  221. Object value) {
  222. _userProps = putProperty(_userProps, name, type, value);
  223. return this;
  224. }
  225. private static Map<String,PropertyMap.Property> putProperty(
  226. Map<String,PropertyMap.Property> props, String name, DataType type,
  227. Object value)
  228. {
  229. if(props == null) {
  230. props = new HashMap<String,PropertyMap.Property>();
  231. }
  232. props.put(name, PropertyMapImpl.createProperty(name, type, value));
  233. return props;
  234. }
  235. /**
  236. * Opens an existingnew Database using the configured information.
  237. */
  238. public Database open() throws IOException {
  239. return DatabaseImpl.open(_mdbFile, _readOnly, _channel, _autoSync, _charset,
  240. _timeZone, _codecProvider);
  241. }
  242. /**
  243. * Creates a new Database using the configured information.
  244. */
  245. public Database create() throws IOException {
  246. Database db = DatabaseImpl.create(_fileFormat, _mdbFile, _channel, _autoSync,
  247. _charset, _timeZone);
  248. if(_dbProps != null) {
  249. PropertyMap props = db.getDatabaseProperties();
  250. props.putAll(_dbProps.values());
  251. props.save();
  252. }
  253. if(_summaryProps != null) {
  254. PropertyMap props = db.getSummaryProperties();
  255. props.putAll(_summaryProps.values());
  256. props.save();
  257. }
  258. if(_userProps != null) {
  259. PropertyMap props = db.getUserDefinedProperties();
  260. props.putAll(_userProps.values());
  261. props.save();
  262. }
  263. return db;
  264. }
  265. /**
  266. * Open an existing Database. If the existing file is not writeable, the
  267. * file will be opened read-only. Auto-syncing is enabled for the returned
  268. * Database.
  269. *
  270. * @param mdbFile File containing the database
  271. *
  272. * @see DatabaseBuilder for more flexible Database opening
  273. * @usage _general_method_
  274. */
  275. public static Database open(File mdbFile) throws IOException {
  276. return new DatabaseBuilder(mdbFile).open();
  277. }
  278. /**
  279. * Open an existing Database. If the existing file is not writeable, the
  280. * file will be opened read-only. Auto-syncing is enabled for the returned
  281. * Database.
  282. *
  283. * @param mdbFile File containing the database
  284. *
  285. * @see DatabaseBuilder for more flexible Database opening
  286. * @usage _general_method_
  287. */
  288. public static Database open(Path mdbFile) throws IOException {
  289. return new DatabaseBuilder(mdbFile).open();
  290. }
  291. /**
  292. * Create a new Database for the given fileFormat
  293. *
  294. * @param fileFormat version of new database.
  295. * @param mdbFile Location to write the new database to. <b>If this file
  296. * already exists, it will be overwritten.</b>
  297. *
  298. * @see DatabaseBuilder for more flexible Database creation
  299. * @usage _general_method_
  300. */
  301. public static Database create(Database.FileFormat fileFormat, File mdbFile)
  302. throws IOException
  303. {
  304. return new DatabaseBuilder(mdbFile).setFileFormat(fileFormat).create();
  305. }
  306. /**
  307. * Returns a SimpleDateFormat for the given format string which is
  308. * configured with a compatible Calendar instance (see
  309. * {@link #toCompatibleCalendar}).
  310. */
  311. public static SimpleDateFormat createDateFormat(String formatStr) {
  312. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  313. toCompatibleCalendar(sdf.getCalendar());
  314. return sdf;
  315. }
  316. /**
  317. * Ensures that the given {@link Calendar} is configured to be compatible
  318. * with how Access handles dates. Specifically, alters the gregorian change
  319. * (the java default gregorian change switches to julian dates for dates pre
  320. * 1582-10-15, whereas Access uses <a href="https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar">proleptic gregorian dates</a>).
  321. */
  322. public static Calendar toCompatibleCalendar(Calendar cal) {
  323. if(cal instanceof GregorianCalendar) {
  324. ((GregorianCalendar)cal).setGregorianChange(new Date(Long.MIN_VALUE));
  325. }
  326. return cal;
  327. }
  328. /**
  329. * Convenience method for constructing a DatabaseBuilder.
  330. */
  331. public static DatabaseBuilder newDatabase() {
  332. return new DatabaseBuilder();
  333. }
  334. /**
  335. * Convenience method for constructing a DatabaseBuilder.
  336. */
  337. public static DatabaseBuilder newDatabase(Path path) {
  338. return new DatabaseBuilder(path);
  339. }
  340. /**
  341. * Convenience method for constructing a DatabaseBuilder.
  342. */
  343. public static DatabaseBuilder newDatabase(File file) {
  344. return new DatabaseBuilder(file);
  345. }
  346. /**
  347. * Convenience method for constructing a TableBuilder.
  348. */
  349. public static TableBuilder newTable(String name) {
  350. return new TableBuilder(name);
  351. }
  352. /**
  353. * Convenience method for constructing a TableBuilder.
  354. */
  355. public static TableBuilder newTable(String name, boolean escapeIdentifiers) {
  356. return new TableBuilder(name, escapeIdentifiers);
  357. }
  358. /**
  359. * Convenience method for constructing a ColumnBuilder.
  360. */
  361. public static ColumnBuilder newColumn(String name) {
  362. return new ColumnBuilder(name);
  363. }
  364. /**
  365. * Convenience method for constructing a TableBuilder.
  366. */
  367. public static ColumnBuilder newColumn(String name, DataType type) {
  368. return new ColumnBuilder(name, type);
  369. }
  370. /**
  371. * Convenience method for constructing an IndexBuilder.
  372. */
  373. public static IndexBuilder newIndex(String name) {
  374. return new IndexBuilder(name);
  375. }
  376. /**
  377. * Convenience method for constructing a primary key IndexBuilder.
  378. */
  379. public static IndexBuilder newPrimaryKey(String... colNames) {
  380. return new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
  381. .addColumns(colNames)
  382. .setPrimaryKey();
  383. }
  384. /**
  385. * Convenience method for constructing a RelationshipBuilder.
  386. */
  387. public static RelationshipBuilder newRelationship(
  388. String fromTable, String toTable) {
  389. return new RelationshipBuilder(fromTable, toTable);
  390. }
  391. /**
  392. * Convenience method for constructing a RelationshipBuilder.
  393. */
  394. public static RelationshipBuilder newRelationship(
  395. Table fromTable, Table toTable) {
  396. return new RelationshipBuilder(fromTable, toTable);
  397. }
  398. private static Path toPath(File file) {
  399. return ((file != null) ? file.toPath() : null);
  400. }
  401. }