You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseBuilder.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.util.HashMap;
  19. import java.util.Map;
  20. import java.util.TimeZone;
  21. import com.healthmarketscience.jackcess.impl.CodecProvider;
  22. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  23. import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
  24. import com.healthmarketscience.jackcess.util.MemFileChannel;
  25. /**
  26. * Builder style class for opening/creating a {@link Database}.
  27. * <p/>
  28. * Simple example usage:
  29. * <pre>
  30. * Database db = DatabaseBuilder.open(new File("test.mdb"));
  31. * </pre>
  32. * <p/>
  33. * Advanced example usage:
  34. * <pre class="prettyprint linenums">
  35. * Database db = new DatabaseBuilder(new File("test.mdb"))
  36. * .setReadOnly(true)
  37. * .open();
  38. * </pre>
  39. *
  40. * @author James Ahlborn
  41. * @usage _general_class_
  42. */
  43. public class DatabaseBuilder
  44. {
  45. /** the file name of the mdb to open/create */
  46. private File _mdbFile;
  47. /** whether or not to open existing mdb read-only */
  48. private boolean _readOnly;
  49. /** whether or not to auto-sync writes to the filesystem */
  50. private boolean _autoSync = Database.DEFAULT_AUTO_SYNC;
  51. /** optional charset for mdbs with unspecified charsets */
  52. private Charset _charset;
  53. /** optional timezone override for interpreting dates */
  54. private TimeZone _timeZone;
  55. /** optional CodecProvider for handling encoded mdbs */
  56. private CodecProvider _codecProvider;
  57. /** FileFormat to use when creating a new mdb */
  58. private Database.FileFormat _fileFormat;
  59. /** optional pre-opened FileChannel, will _not_ be closed by Database
  60. close */
  61. private FileChannel _channel;
  62. /** database properties (if any) */
  63. private Map<String,PropertyMap.Property> _dbProps;
  64. /** database summary properties (if any) */
  65. private Map<String,PropertyMap.Property> _summaryProps;
  66. /** database user-defined (if any) */
  67. private Map<String,PropertyMap.Property> _userProps;
  68. public DatabaseBuilder() {
  69. this(null);
  70. }
  71. public DatabaseBuilder(File mdbFile) {
  72. _mdbFile = mdbFile;
  73. }
  74. /**
  75. * File containing an existing database for {@link #open} or target file for
  76. * new database for {@link #create} (in which case, <b>tf this file already
  77. * exists, it will be overwritten.</b>)
  78. * @usage _general_method_
  79. */
  80. public DatabaseBuilder setFile(File mdbFile) {
  81. _mdbFile = mdbFile;
  82. return this;
  83. }
  84. /**
  85. * Sets flag which, iff {@code true}, will force opening file in
  86. * read-only mode ({@link #open} only).
  87. * @usage _general_method_
  88. */
  89. public DatabaseBuilder setReadOnly(boolean readOnly) {
  90. _readOnly = readOnly;
  91. return this;
  92. }
  93. /**
  94. * Sets whether or not to enable auto-syncing on write. if {@code true},
  95. * write operations will be immediately flushed to disk upon completion.
  96. * This leaves the database in a (fairly) consistent state on each write,
  97. * but can be very inefficient for many updates. if {@code false}, flushing
  98. * to disk happens at the jvm's leisure, which can be much faster, but may
  99. * leave the database in an inconsistent state if failures are encountered
  100. * during writing. Writes may be flushed at any time using {@link
  101. * Database#flush}.
  102. * @usage _intermediate_method_
  103. */
  104. public DatabaseBuilder setAutoSync(boolean autoSync) {
  105. _autoSync = autoSync;
  106. return this;
  107. }
  108. /**
  109. * Sets the Charset to use, if {@code null}, uses default.
  110. * @usage _intermediate_method_
  111. */
  112. public DatabaseBuilder setCharset(Charset charset) {
  113. _charset = charset;
  114. return this;
  115. }
  116. /**
  117. * Sets the TimeZone to use for interpreting dates, if {@code null}, uses
  118. * default
  119. * @usage _intermediate_method_
  120. */
  121. public DatabaseBuilder setTimeZone(TimeZone timeZone) {
  122. _timeZone = timeZone;
  123. return this;
  124. }
  125. /**
  126. * Sets the CodecProvider for handling page encoding/decoding, may be
  127. * {@code null} if no special encoding is necessary
  128. * @usage _intermediate_method_
  129. */
  130. public DatabaseBuilder setCodecProvider(CodecProvider codecProvider) {
  131. _codecProvider = codecProvider;
  132. return this;
  133. }
  134. /**
  135. * Sets the version of new database ({@link #create} only).
  136. * @usage _general_method_
  137. */
  138. public DatabaseBuilder setFileFormat(Database.FileFormat fileFormat) {
  139. _fileFormat = fileFormat;
  140. return this;
  141. }
  142. /**
  143. * Sets a pre-opened FileChannel. if provided explicitly, <i>it will not be
  144. * closed by the Database instance</i>. This allows ultimate control of
  145. * where the mdb file exists (which may not be on disk, e.g.
  146. * {@link MemFileChannel}). If provided, the File parameter will be
  147. * available from {@link Database#getFile}, but otherwise ignored.
  148. * @usage _advanced_method_
  149. */
  150. public DatabaseBuilder setChannel(FileChannel channel) {
  151. _channel = channel;
  152. return this;
  153. }
  154. /**
  155. * Sets the database property with the given name to the given value.
  156. * Attempts to determine the type of the property (see
  157. * {@link PropertyMap#put(String,Object)} for details on determining the
  158. * property type).
  159. */
  160. public DatabaseBuilder putDatabaseProperty(String name, Object value) {
  161. return putDatabaseProperty(name, null, value);
  162. }
  163. /**
  164. * Sets the database property with the given name and type to the given
  165. * value.
  166. */
  167. public DatabaseBuilder putDatabaseProperty(String name, DataType type,
  168. Object value) {
  169. _dbProps = putProperty(_dbProps, name, type, value);
  170. return this;
  171. }
  172. /**
  173. * Sets the summary database property with the given name to the given
  174. * value. Attempts to determine the type of the property (see
  175. * {@link PropertyMap#put(String,Object)} for details on determining the
  176. * property type).
  177. */
  178. public DatabaseBuilder putSummaryProperty(String name, Object value) {
  179. return putSummaryProperty(name, null, value);
  180. }
  181. /**
  182. * Sets the summary database property with the given name and type to
  183. * the given value.
  184. */
  185. public DatabaseBuilder putSummaryProperty(String name, DataType type,
  186. Object value) {
  187. _summaryProps = putProperty(_summaryProps, name, type, value);
  188. return this;
  189. }
  190. /**
  191. * Sets the user-defined database property with the given name to the given
  192. * value. Attempts to determine the type of the property (see
  193. * {@link PropertyMap#put(String,Object)} for details on determining the
  194. * property type).
  195. */
  196. public DatabaseBuilder putUserDefinedProperty(String name, Object value) {
  197. return putUserDefinedProperty(name, null, value);
  198. }
  199. /**
  200. * Sets the user-defined database property with the given name and type to
  201. * the given value.
  202. */
  203. public DatabaseBuilder putUserDefinedProperty(String name, DataType type,
  204. Object value) {
  205. _userProps = putProperty(_userProps, name, type, value);
  206. return this;
  207. }
  208. private static Map<String,PropertyMap.Property> putProperty(
  209. Map<String,PropertyMap.Property> props, String name, DataType type,
  210. Object value)
  211. {
  212. if(props == null) {
  213. props = new HashMap<String,PropertyMap.Property>();
  214. }
  215. props.put(name, PropertyMapImpl.createProperty(name, type, value));
  216. return props;
  217. }
  218. /**
  219. * Opens an existingnew Database using the configured information.
  220. */
  221. public Database open() throws IOException {
  222. return DatabaseImpl.open(_mdbFile, _readOnly, _channel, _autoSync, _charset,
  223. _timeZone, _codecProvider);
  224. }
  225. /**
  226. * Creates a new Database using the configured information.
  227. */
  228. public Database create() throws IOException {
  229. Database db = DatabaseImpl.create(_fileFormat, _mdbFile, _channel, _autoSync,
  230. _charset, _timeZone);
  231. if(_dbProps != null) {
  232. PropertyMap props = db.getDatabaseProperties();
  233. props.putAll(_dbProps.values());
  234. props.save();
  235. }
  236. if(_summaryProps != null) {
  237. PropertyMap props = db.getSummaryProperties();
  238. props.putAll(_summaryProps.values());
  239. props.save();
  240. }
  241. if(_userProps != null) {
  242. PropertyMap props = db.getUserDefinedProperties();
  243. props.putAll(_userProps.values());
  244. props.save();
  245. }
  246. return db;
  247. }
  248. /**
  249. * Open an existing Database. If the existing file is not writeable, the
  250. * file will be opened read-only. Auto-syncing is enabled for the returned
  251. * Database.
  252. *
  253. * @param mdbFile File containing the database
  254. *
  255. * @see DatabaseBuilder for more flexible Database opening
  256. * @usage _general_method_
  257. */
  258. public static Database open(File mdbFile) throws IOException {
  259. return new DatabaseBuilder(mdbFile).open();
  260. }
  261. /**
  262. * Create a new Database for the given fileFormat
  263. *
  264. * @param fileFormat version of new database.
  265. * @param mdbFile Location to write the new database to. <b>If this file
  266. * already exists, it will be overwritten.</b>
  267. *
  268. * @see DatabaseBuilder for more flexible Database creation
  269. * @usage _general_method_
  270. */
  271. public static Database create(Database.FileFormat fileFormat, File mdbFile)
  272. throws IOException
  273. {
  274. return new DatabaseBuilder(mdbFile).setFileFormat(fileFormat).create();
  275. }
  276. }