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

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