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.

Database.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. Copyright (c) 2013 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.Closeable;
  15. import java.io.File;
  16. import java.io.Flushable;
  17. import java.io.IOException;
  18. import java.nio.charset.Charset;
  19. import java.util.ConcurrentModificationException;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.TimeZone;
  25. import com.healthmarketscience.jackcess.query.Query;
  26. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  27. import com.healthmarketscience.jackcess.util.ColumnValidatorFactory;
  28. import com.healthmarketscience.jackcess.util.ErrorHandler;
  29. import com.healthmarketscience.jackcess.util.LinkResolver;
  30. import com.healthmarketscience.jackcess.util.TableIterableBuilder;
  31. /**
  32. * An Access database instance. A new instance can be instantiated by opening
  33. * an existing database file ({@link DatabaseBuilder#open(File)}) or creating
  34. * a new database file ({@link DatabaseBuilder#create(Database.FileFormat,File)}) (for
  35. * more advanced opening/creating use {@link DatabaseBuilder}). Once a
  36. * Database has been opened, you can interact with the data via the relevant
  37. * {@link Table}. When a Database instance is no longer useful, it should
  38. * <b>always</b> be closed ({@link #close}) to avoid corruption.
  39. * <p/>
  40. * Database instances (and all the related objects) are <i>not</i>
  41. * thread-safe. However, separate Database instances (and their respective
  42. * objects) can be used by separate threads without a problem.
  43. * <p/>
  44. * Database instances do not implement any "transactional" support, and
  45. * therefore concurrent editing of the same database file by multiple Database
  46. * instances (or with outside programs such as MS Access) <i>will generally
  47. * result in database file corruption</i>.
  48. *
  49. * @author James Ahlborn
  50. * @usage _general_class_
  51. */
  52. public interface Database extends Iterable<Table>, Closeable, Flushable
  53. {
  54. /** default value for the auto-sync value ({@code true}). this is slower,
  55. * but leaves more chance of a useable database in the face of failures.
  56. * @usage _general_field_
  57. */
  58. public static final boolean DEFAULT_AUTO_SYNC = true;
  59. /**
  60. * the default sort order for table columns.
  61. * @usage _intermediate_field_
  62. */
  63. public static final Table.ColumnOrder DEFAULT_COLUMN_ORDER =
  64. Table.ColumnOrder.DATA;
  65. /** system property which can be used to set the default TimeZone used for
  66. * date calculations.
  67. * @usage _general_field_
  68. */
  69. public static final String TIMEZONE_PROPERTY =
  70. "com.healthmarketscience.jackcess.timeZone";
  71. /** system property prefix which can be used to set the default Charset
  72. * used for text data (full property includes the JetFormat version).
  73. * @usage _general_field_
  74. */
  75. public static final String CHARSET_PROPERTY_PREFIX =
  76. "com.healthmarketscience.jackcess.charset.";
  77. /** system property which can be used to set the path from which classpath
  78. * resources are loaded (must end with a "/" if non-empty). Default value
  79. * is {@value com.healthmarketscience.jackcess.impl.DatabaseImpl#DEFAULT_RESOURCE_PATH}
  80. * if unspecified.
  81. * @usage _general_field_
  82. */
  83. public static final String RESOURCE_PATH_PROPERTY =
  84. "com.healthmarketscience.jackcess.resourcePath";
  85. /** (boolean) system property which can be used to indicate that the current
  86. * vm has a poor nio implementation (specifically for
  87. * {@code FileChannel.transferFrom})
  88. * @usage _intermediate_field_
  89. */
  90. public static final String BROKEN_NIO_PROPERTY =
  91. "com.healthmarketscience.jackcess.brokenNio";
  92. /** system property which can be used to set the default sort order for
  93. * table columns. Value should be one {@link Table.ColumnOrder} enum
  94. * values.
  95. * @usage _intermediate_field_
  96. */
  97. public static final String COLUMN_ORDER_PROPERTY =
  98. "com.healthmarketscience.jackcess.columnOrder";
  99. /** system property which can be used to set the default enforcement of
  100. * foreign-key relationships. Defaults to {@code true}.
  101. * @usage _general_field_
  102. */
  103. public static final String FK_ENFORCE_PROPERTY =
  104. "com.healthmarketscience.jackcess.enforceForeignKeys";
  105. /** system property which can be used to set the default allow auto number
  106. * insert policy. Defaults to {@code false}.
  107. * @usage _general_field_
  108. */
  109. public static final String ALLOW_AUTONUM_INSERT_PROPERTY =
  110. "com.healthmarketscience.jackcess.allowAutoNumberInsert";
  111. /**
  112. * Enum which indicates which version of Access created the database.
  113. * @usage _general_class_
  114. */
  115. public enum FileFormat {
  116. V1997(".mdb"),
  117. V2000(".mdb"),
  118. V2003(".mdb"),
  119. V2007(".accdb"),
  120. V2010(".accdb"),
  121. MSISAM(".mny");
  122. private final String _ext;
  123. private FileFormat(String ext) {
  124. _ext = ext;
  125. }
  126. /**
  127. * @return the file extension used for database files with this format.
  128. */
  129. public String getFileExtension() { return _ext; }
  130. @Override
  131. public String toString() {
  132. return name() + " [" + DatabaseImpl.getFileFormatDetails(this).getFormat() + "]";
  133. }
  134. }
  135. /**
  136. * Returns the File underlying this Database
  137. */
  138. public File getFile();
  139. /**
  140. * @return The names of all of the user tables
  141. * @usage _general_method_
  142. */
  143. public Set<String> getTableNames() throws IOException;
  144. /**
  145. * @return The names of all of the system tables (String). Note, in order
  146. * to read these tables, you must use {@link #getSystemTable}.
  147. * <i>Extreme care should be taken if modifying these tables
  148. * directly!</i>.
  149. * @usage _intermediate_method_
  150. */
  151. public Set<String> getSystemTableNames() throws IOException;
  152. /**
  153. * @return an unmodifiable Iterator of the user Tables in this Database.
  154. * @throws RuntimeIOException if an IOException is thrown by one of the
  155. * operations, the actual exception will be contained within
  156. * @throws ConcurrentModificationException if a table is added to the
  157. * database while an Iterator is in use.
  158. * @usage _general_method_
  159. */
  160. public Iterator<Table> iterator();
  161. /**
  162. * Convenience method for constructing a new TableIterableBuilder for this
  163. * cursor. A TableIterableBuilder provides a variety of options for more
  164. * flexible iteration of Tables.
  165. */
  166. public TableIterableBuilder newIterable();
  167. /**
  168. * @param name Table name (case-insensitive)
  169. * @return The table, or null if it doesn't exist
  170. * @usage _general_method_
  171. */
  172. public Table getTable(String name) throws IOException;
  173. /**
  174. * Finds all the relationships in the database between the given tables.
  175. * @usage _intermediate_method_
  176. */
  177. public List<Relationship> getRelationships(Table table1, Table table2)
  178. throws IOException;
  179. /**
  180. * Finds all the relationships in the database for the given table.
  181. * @usage _intermediate_method_
  182. */
  183. public List<Relationship> getRelationships(Table table) throws IOException;
  184. /**
  185. * Finds all the relationships in the database in <i>non-system</i> tables.
  186. * </p>
  187. * Warning, this may load <i>all</i> the Tables (metadata, not data) in the
  188. * database which could cause memory issues.
  189. * @usage _intermediate_method_
  190. */
  191. public List<Relationship> getRelationships() throws IOException;
  192. /**
  193. * Finds <i>all</i> the relationships in the database, <i>including system
  194. * tables</i>.
  195. * </p>
  196. * Warning, this may load <i>all</i> the Tables (metadata, not data) in the
  197. * database which could cause memory issues.
  198. * @usage _intermediate_method_
  199. */
  200. public List<Relationship> getSystemRelationships()
  201. throws IOException;
  202. /**
  203. * Finds all the queries in the database.
  204. * @usage _intermediate_method_
  205. */
  206. public List<Query> getQueries() throws IOException;
  207. /**
  208. * Returns a reference to <i>any</i> available table in this access
  209. * database, including system tables.
  210. * <p>
  211. * Warning, this method is not designed for common use, only for the
  212. * occassional time when access to a system table is necessary. Messing
  213. * with system tables can strip the paint off your house and give your whole
  214. * family a permanent, orange afro. You have been warned.
  215. *
  216. * @param tableName Table name, may be a system table
  217. * @return The table, or {@code null} if it doesn't exist
  218. * @usage _intermediate_method_
  219. */
  220. public Table getSystemTable(String tableName) throws IOException;
  221. /**
  222. * @return the core properties for the database
  223. * @usage _general_method_
  224. */
  225. public PropertyMap getDatabaseProperties() throws IOException;
  226. /**
  227. * @return the summary properties for the database
  228. * @usage _general_method_
  229. */
  230. public PropertyMap getSummaryProperties() throws IOException;
  231. /**
  232. * @return the user-defined properties for the database
  233. * @usage _general_method_
  234. */
  235. public PropertyMap getUserDefinedProperties() throws IOException;
  236. /**
  237. * @return the current database password, or {@code null} if none set.
  238. * @usage _general_method_
  239. */
  240. public String getDatabasePassword() throws IOException;
  241. /**
  242. * Create a new table in this database
  243. * @param name Name of the table to create in this database
  244. * @param linkedDbName path to the linked database
  245. * @param linkedTableName name of the table in the linked database
  246. * @usage _general_method_
  247. */
  248. public void createLinkedTable(String name, String linkedDbName,
  249. String linkedTableName)
  250. throws IOException;
  251. /**
  252. * Flushes any current changes to the database file (and any linked
  253. * databases) to disk.
  254. * @usage _general_method_
  255. */
  256. public void flush() throws IOException;
  257. /**
  258. * Close the database file (and any linked databases). A Database
  259. * <b>must</b> be closed after use or changes could be lost and the Database
  260. * file corrupted. A Database instance should be treated like any other
  261. * external resource which would be closed in a finally block (e.g. an
  262. * OutputStream or jdbc Connection).
  263. * @usage _general_method_
  264. */
  265. public void close() throws IOException;
  266. /**
  267. * Gets the currently configured ErrorHandler (always non-{@code null}).
  268. * This will be used to handle all errors unless overridden at the Table or
  269. * Cursor level.
  270. * @usage _intermediate_method_
  271. */
  272. public ErrorHandler getErrorHandler();
  273. /**
  274. * Sets a new ErrorHandler. If {@code null}, resets to the
  275. * {@link ErrorHandler#DEFAULT}.
  276. * @usage _intermediate_method_
  277. */
  278. public void setErrorHandler(ErrorHandler newErrorHandler);
  279. /**
  280. * Gets the currently configured LinkResolver (always non-{@code null}).
  281. * This will be used to handle all linked database loading.
  282. * @usage _intermediate_method_
  283. */
  284. public LinkResolver getLinkResolver();
  285. /**
  286. * Sets a new LinkResolver. If {@code null}, resets to the
  287. * {@link LinkResolver#DEFAULT}.
  288. * @usage _intermediate_method_
  289. */
  290. public void setLinkResolver(LinkResolver newLinkResolver);
  291. /**
  292. * Returns an unmodifiable view of the currently loaded linked databases,
  293. * mapped from the linked database file name to the linked database. This
  294. * information may be useful for implementing a LinkResolver.
  295. * @usage _intermediate_method_
  296. */
  297. public Map<String,Database> getLinkedDatabases();
  298. /**
  299. * Returns {@code true} if this Database links to the given Table, {@code
  300. * false} otherwise.
  301. * @usage _general_method_
  302. */
  303. public boolean isLinkedTable(Table table) throws IOException;
  304. /**
  305. * Gets currently configured TimeZone (always non-{@code null}).
  306. * @usage _intermediate_method_
  307. */
  308. public TimeZone getTimeZone();
  309. /**
  310. * Sets a new TimeZone. If {@code null}, resets to the default value.
  311. * @usage _intermediate_method_
  312. */
  313. public void setTimeZone(TimeZone newTimeZone);
  314. /**
  315. * Gets currently configured Charset (always non-{@code null}).
  316. * @usage _intermediate_method_
  317. */
  318. public Charset getCharset();
  319. /**
  320. * Sets a new Charset. If {@code null}, resets to the default value.
  321. * @usage _intermediate_method_
  322. */
  323. public void setCharset(Charset newCharset);
  324. /**
  325. * Gets currently configured {@link Table.ColumnOrder} (always non-{@code
  326. * null}).
  327. * @usage _intermediate_method_
  328. */
  329. public Table.ColumnOrder getColumnOrder();
  330. /**
  331. * Sets a new Table.ColumnOrder. If {@code null}, resets to the default value.
  332. * @usage _intermediate_method_
  333. */
  334. public void setColumnOrder(Table.ColumnOrder newColumnOrder);
  335. /**
  336. * Gets current foreign-key enforcement policy.
  337. * @usage _intermediate_method_
  338. */
  339. public boolean isEnforceForeignKeys();
  340. /**
  341. * Sets a new foreign-key enforcement policy. If {@code null}, resets to
  342. * the default value.
  343. * @usage _intermediate_method_
  344. */
  345. public void setEnforceForeignKeys(Boolean newEnforceForeignKeys);
  346. /**
  347. * Gets current allow auto number insert policy. By default, jackcess does
  348. * not allow auto numbers to be inserted or updated directly (they are
  349. * always handled internally by the Table). Setting this policy to {@code
  350. * true} allows the caller to optionally set the value explicitly when
  351. * adding or updating rows (if a value is not provided, it will still be
  352. * handled internally by the Table). This value can be set database-wide
  353. * using {@link #setAllowAutoNumberInsert} and/or on a per-table basis using
  354. * {@link Table#setAllowAutoNumberInsert} (and/or on a jvm-wide using the
  355. * {@link #ALLOW_AUTONUM_INSERT_PROPERTY} system property). Note that
  356. * <i>enabling this feature should be done with care</i> to reduce the
  357. * chances of screwing up the database.
  358. *
  359. * @usage _intermediate_method_
  360. */
  361. public boolean isAllowAutoNumberInsert();
  362. /**
  363. * Sets the new auto number insert policy for the database (unless
  364. * overridden at the Table level). If {@code null}, resets to the default
  365. * value.
  366. * @usage _intermediate_method_
  367. */
  368. public void setAllowAutoNumberInsert(Boolean allowAutoNumInsert);
  369. /**
  370. * Gets currently configured ColumnValidatorFactory (always non-{@code null}).
  371. * @usage _intermediate_method_
  372. */
  373. public ColumnValidatorFactory getColumnValidatorFactory();
  374. /**
  375. * Sets a new ColumnValidatorFactory. If {@code null}, resets to the
  376. * default value. The configured ColumnValidatorFactory will be used to
  377. * create ColumnValidator instances on any <i>user</i> tables loaded from
  378. * this point onward (this will not be used for system tables).
  379. * @usage _intermediate_method_
  380. */
  381. public void setColumnValidatorFactory(ColumnValidatorFactory newFactory);
  382. /**
  383. * Returns the FileFormat of this database (which may involve inspecting the
  384. * database itself).
  385. * @throws IllegalStateException if the file format cannot be determined
  386. * @usage _general_method_
  387. */
  388. public FileFormat getFileFormat() throws IOException;
  389. }