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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright (c) 2012 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.nio.channels.FileChannel;
  20. import java.nio.charset.Charset;
  21. import java.util.TimeZone;
  22. /**
  23. * Builder style class for opening/creating a Database.
  24. *
  25. * @author James Ahlborn
  26. */
  27. public class DatabaseBuilder
  28. {
  29. /** the file name of the mdb to open/create */
  30. private File _mdbFile;
  31. /** whether or not to open existing mdb read-only */
  32. private boolean _readOnly;
  33. /** whether or not to auto-sync writes to the filesystem */
  34. private boolean _autoSync = Database.DEFAULT_AUTO_SYNC;
  35. /** optional charset for mdbs with unspecified charsets */
  36. private Charset _charset;
  37. /** optional timezone override for interpreting dates */
  38. private TimeZone _timeZone;
  39. /** optional CodecProvider for handling encoded mdbs */
  40. private CodecProvider _codecProvider;
  41. /** FileFormat to use when creating a new mdb */
  42. private Database.FileFormat _fileFormat;
  43. /** optional pre-opened FileChannel, will _not_ be closed by Database
  44. close */
  45. private FileChannel _channel;
  46. public DatabaseBuilder() {
  47. this(null);
  48. }
  49. public DatabaseBuilder(File mdbFile) {
  50. _mdbFile = mdbFile;
  51. }
  52. /**
  53. * File containing an existing database for {@link #open} or target file for
  54. * new database for {@link #create} (in which case, <b>tf this file already
  55. * exists, it will be overwritten.</b>)
  56. * @usage _general_method_
  57. */
  58. public DatabaseBuilder setFile(File mdbFile) {
  59. _mdbFile = mdbFile;
  60. return this;
  61. }
  62. /**
  63. * Sets flag which, iff {@code true}, will force opening file in
  64. * read-only mode ({@link #open} only).
  65. * @usage _general_method_
  66. */
  67. public DatabaseBuilder setReadOnly(boolean readOnly) {
  68. _readOnly = readOnly;
  69. return this;
  70. }
  71. /**
  72. * Sets whether or not to enable auto-syncing on write. if {@code true},
  73. * writes will be immediately flushed to disk. This leaves the database in
  74. * a (fairly) consistent state on each write, but can be very inefficient
  75. * for many updates. if {@code false}, flushing to disk happens at the
  76. * jvm's leisure, which can be much faster, but may leave the database in an
  77. * inconsistent state if failures are encountered during writing. Writes
  78. * may be flushed at any time using {@link Database#flush}.
  79. * @usage _intermediate_method_
  80. */
  81. public DatabaseBuilder setAutoSync(boolean autoSync) {
  82. _autoSync = autoSync;
  83. return this;
  84. }
  85. /**
  86. * Sets the Charset to use, if {@code null}, uses default.
  87. * @usage _intermediate_method_
  88. */
  89. public DatabaseBuilder setCharset(Charset charset) {
  90. _charset = charset;
  91. return this;
  92. }
  93. /**
  94. * Sets the TimeZone to use for interpreting dates, if {@code null}, uses
  95. * default
  96. * @usage _intermediate_method_
  97. */
  98. public DatabaseBuilder setTimeZone(TimeZone timeZone) {
  99. _timeZone = timeZone;
  100. return this;
  101. }
  102. /**
  103. * Sets the CodecProvider for handling page encoding/decoding, may be
  104. * {@code null} if no special encoding is necessary
  105. * @usage _intermediate_method_
  106. */
  107. public DatabaseBuilder setCodecProvider(CodecProvider codecProvider) {
  108. _codecProvider = codecProvider;
  109. return this;
  110. }
  111. /**
  112. * Sets the version of new database ({@link #create} only).
  113. * @usage _general_method_
  114. */
  115. public DatabaseBuilder setFileFormat(Database.FileFormat fileFormat) {
  116. _fileFormat = fileFormat;
  117. return this;
  118. }
  119. /**
  120. * Sets a pre-opened FileChannel. if provided explicitly, <i>it will not be
  121. * closed by the Database instance</i>. This allows ultimate control of
  122. * where the mdb file exists (which may not be on disk, e.g.
  123. * {@link MemFileChannel}). If provided, the File parameter will be
  124. * available from {@link Database#getFile}, but otherwise ignored.
  125. * @usage _advanced_method_
  126. */
  127. public DatabaseBuilder setChannel(FileChannel channel) {
  128. _channel = channel;
  129. return this;
  130. }
  131. /**
  132. * Opens an existingnew Database using the configured information.
  133. */
  134. public Database open() throws IOException {
  135. return Database.open(_mdbFile, _readOnly, _channel, _autoSync, _charset,
  136. _timeZone, _codecProvider);
  137. }
  138. /**
  139. * Creates a new Database using the configured information.
  140. */
  141. public Database create() throws IOException {
  142. return Database.create(_fileFormat, _mdbFile, _channel, _autoSync, _charset,
  143. _timeZone);
  144. }
  145. }