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

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