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

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