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.

Table.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.IOException;
  15. import java.time.LocalDateTime;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.stream.Stream;
  20. import java.util.stream.StreamSupport;
  21. import com.healthmarketscience.jackcess.util.ErrorHandler;
  22. import com.healthmarketscience.jackcess.util.OleBlob;
  23. /**
  24. * A single database table. A Table instance is retrieved from a {@link
  25. * Database} instance. The Table instance provides access to the table
  26. * metadata as well as the table data. There are basic data operations on the
  27. * Table interface (i.e. {@link #iterator} {@link #addRow}, {@link #updateRow}
  28. * and {@link #deleteRow}), but for advanced search and data manipulation a
  29. * {@link Cursor} instance should be used. New Tables can be created using a
  30. * {@link TableBuilder}. The {@link com.healthmarketscience.jackcess.util.Joiner} utility can be used to traverse
  31. * table relationships (e.g. find rows in another table based on a foreign-key
  32. * relationship).
  33. * <p>
  34. * A Table instance is not thread-safe (see {@link Database} for more
  35. * thread-safety details).
  36. *
  37. * @author James Ahlborn
  38. * @usage _general_class_
  39. */
  40. public interface Table extends Iterable<Row>, TableDefinition
  41. {
  42. /**
  43. * enum which controls the ordering of the columns in a table.
  44. * @usage _intermediate_class_
  45. */
  46. public enum ColumnOrder {
  47. /** columns are ordered based on the order of the data in the table (this
  48. order does not change as columns are added to the table). */
  49. DATA,
  50. /** columns are ordered based on the "display" order (this order can be
  51. changed arbitrarily) */
  52. DISPLAY;
  53. }
  54. /**
  55. * @return The name of the table
  56. * @usage _general_method_
  57. */
  58. @Override
  59. public String getName();
  60. /**
  61. * Whether or not this table has been marked as hidden.
  62. * @usage _general_method_
  63. */
  64. @Override
  65. public boolean isHidden();
  66. /**
  67. * Whether or not this table is a system (internal) table.
  68. * @usage _general_method_
  69. */
  70. @Override
  71. public boolean isSystem();
  72. /**
  73. * @usage _general_method_
  74. */
  75. @Override
  76. public int getColumnCount();
  77. /**
  78. * @usage _general_method_
  79. */
  80. @Override
  81. public Database getDatabase();
  82. /**
  83. * Gets the currently configured ErrorHandler (always non-{@code null}).
  84. * This will be used to handle all errors unless overridden at the Cursor
  85. * level.
  86. * @usage _intermediate_method_
  87. */
  88. public ErrorHandler getErrorHandler();
  89. /**
  90. * Sets a new ErrorHandler. If {@code null}, resets to using the
  91. * ErrorHandler configured at the Database level.
  92. * @usage _intermediate_method_
  93. */
  94. public void setErrorHandler(ErrorHandler newErrorHandler);
  95. /**
  96. * Gets the currently configured auto number insert policy.
  97. * @see Database#isAllowAutoNumberInsert
  98. * @usage _intermediate_method_
  99. */
  100. public boolean isAllowAutoNumberInsert();
  101. /**
  102. * Sets the new auto number insert policy for the Table. If {@code null},
  103. * resets to using the policy configured at the Database level.
  104. * @usage _intermediate_method_
  105. */
  106. public void setAllowAutoNumberInsert(Boolean allowAutoNumInsert);
  107. /**
  108. * @return All of the columns in this table (unmodifiable List)
  109. * @usage _general_method_
  110. */
  111. @Override
  112. public List<? extends Column> getColumns();
  113. /**
  114. * @return the column with the given name
  115. * @usage _general_method_
  116. */
  117. @Override
  118. public Column getColumn(String name);
  119. /**
  120. * @return the properties for this table
  121. * @usage _general_method_
  122. */
  123. @Override
  124. public PropertyMap getProperties() throws IOException;
  125. /**
  126. * @return the created date for this table if available
  127. * @usage _general_method_
  128. */
  129. @Override
  130. public LocalDateTime getCreatedDate() throws IOException;
  131. /**
  132. * Note: jackcess <i>does not automatically update the modified date of a
  133. * Table</i>.
  134. *
  135. * @return the last updated date for this table if available
  136. * @usage _general_method_
  137. */
  138. @Override
  139. public LocalDateTime getUpdatedDate() throws IOException;
  140. /**
  141. * @return All of the Indexes on this table (unmodifiable List)
  142. * @usage _intermediate_method_
  143. */
  144. @Override
  145. public List<? extends Index> getIndexes();
  146. /**
  147. * @return the index with the given name
  148. * @throws IllegalArgumentException if there is no index with the given name
  149. * @usage _intermediate_method_
  150. */
  151. @Override
  152. public Index getIndex(String name);
  153. /**
  154. * @return the primary key index for this table
  155. * @throws IllegalArgumentException if there is no primary key index on this
  156. * table
  157. * @usage _intermediate_method_
  158. */
  159. @Override
  160. public Index getPrimaryKeyIndex();
  161. /**
  162. * @return the foreign key index joining this table to the given other table
  163. * @throws IllegalArgumentException if there is no relationship between this
  164. * table and the given table
  165. * @usage _intermediate_method_
  166. */
  167. @Override
  168. public Index getForeignKeyIndex(Table otherTable);
  169. /**
  170. * Converts a map of columnName -&gt; columnValue to an array of row values
  171. * appropriate for a call to {@link #addRow(Object...)}.
  172. * @usage _general_method_
  173. */
  174. public Object[] asRow(Map<String,?> rowMap);
  175. /**
  176. * Converts a map of columnName -&gt; columnValue to an array of row values
  177. * appropriate for a call to {@link Cursor#updateCurrentRow(Object...)}.
  178. * @usage _general_method_
  179. */
  180. public Object[] asUpdateRow(Map<String,?> rowMap);
  181. /**
  182. * @usage _general_method_
  183. */
  184. public int getRowCount();
  185. /**
  186. * Adds a single row to this table and writes it to disk. The values are
  187. * expected to be given in the order that the Columns are listed by the
  188. * {@link #getColumns} method. This is by default the storage order of the
  189. * Columns in the database, however this order can be influenced by setting
  190. * the ColumnOrder via {@link Database#setColumnOrder} prior to opening
  191. * the Table. The {@link #asRow} method can be used to easily convert a row
  192. * Map into the appropriate row array for this Table.
  193. * <p>
  194. * Note, if this table has an auto-number column, the value generated will be
  195. * put back into the given row array (assuming the given row array is at
  196. * least as long as the number of Columns in this Table).
  197. *
  198. * @param row row values for a single row. the given row array will be
  199. * modified if this table contains an auto-number column,
  200. * otherwise it will not be modified.
  201. * @return the given row values if long enough, otherwise a new array. the
  202. * returned array will contain any autonumbers generated
  203. * @usage _general_method_
  204. */
  205. public Object[] addRow(Object... row) throws IOException;
  206. /**
  207. * Calls {@link #asRow} on the given row map and passes the result to {@link
  208. * #addRow}.
  209. * <p>
  210. * Note, if this table has an auto-number column, the value generated will be
  211. * put back into the given row map.
  212. * @return the given row map, which will contain any autonumbers generated
  213. * @usage _general_method_
  214. */
  215. public <M extends Map<String,Object>> M addRowFromMap(M row)
  216. throws IOException;
  217. /**
  218. * Add multiple rows to this table, only writing to disk after all
  219. * rows have been written, and every time a data page is filled. This
  220. * is much more efficient than calling {@link #addRow} multiple times.
  221. * <p>
  222. * Note, if this table has an auto-number column, the values written will be
  223. * put back into the given row arrays (assuming the given row array is at
  224. * least as long as the number of Columns in this Table).
  225. * <p>
  226. * Most exceptions thrown from this method will be wrapped with a {@link
  227. * BatchUpdateException} which gives useful information in the case of a
  228. * partially successful write.
  229. *
  230. * @see #addRow(Object...) for more details on row arrays
  231. *
  232. * @param rows List of Object[] row values. the rows will be modified if
  233. * this table contains an auto-number column, otherwise they
  234. * will not be modified.
  235. * @return the given row values list (unless row values were to small), with
  236. * appropriately sized row values (the ones passed in if long
  237. * enough). the returned arrays will contain any autonumbers
  238. * generated
  239. * @usage _general_method_
  240. */
  241. public List<? extends Object[]> addRows(List<? extends Object[]> rows)
  242. throws IOException;
  243. /**
  244. * Calls {@link #asRow} on the given row maps and passes the results to
  245. * {@link #addRows}.
  246. * <p>
  247. * Note, if this table has an auto-number column, the values generated will
  248. * be put back into the appropriate row maps.
  249. * <p>
  250. * Most exceptions thrown from this method will be wrapped with a {@link
  251. * BatchUpdateException} which gives useful information in the case of a
  252. * partially successful write.
  253. *
  254. * @return the given row map list, where the row maps will contain any
  255. * autonumbers generated
  256. * @usage _general_method_
  257. */
  258. public <M extends Map<String,Object>> List<M> addRowsFromMaps(List<M> rows)
  259. throws IOException;
  260. /**
  261. * Update the given row. Provided Row must have previously been returned
  262. * from this Table.
  263. * @return the given row, updated with the current row values
  264. * @throws IllegalStateException if the given row is not valid, or deleted.
  265. */
  266. public Row updateRow(Row row) throws IOException;
  267. /**
  268. * Delete the given row. Provided Row must have previously been returned
  269. * from this Table.
  270. * @return the given row
  271. * @throws IllegalStateException if the given row is not valid
  272. */
  273. public Row deleteRow(Row row) throws IOException;
  274. /**
  275. * Calls {@link #reset} on this table and returns a modifiable
  276. * Iterator which will iterate through all the rows of this table. Use of
  277. * the Iterator follows the same restrictions as a call to
  278. * {@link #getNextRow}.
  279. * <p>
  280. * For more advanced iteration, use the {@link #getDefaultCursor default
  281. * cursor} directly.
  282. * @throws RuntimeIOException if an IOException is thrown by one of the
  283. * operations, the actual exception will be contained within
  284. * @usage _general_method_
  285. */
  286. @Override
  287. public Iterator<Row> iterator();
  288. /**
  289. * @return a Stream using the default Iterator.
  290. */
  291. default public Stream<Row> stream() {
  292. return StreamSupport.stream(spliterator(), false);
  293. }
  294. /**
  295. * After calling this method, {@link #getNextRow} will return the first row
  296. * in the table, see {@link Cursor#reset} (uses the {@link #getDefaultCursor
  297. * default cursor}).
  298. * @usage _general_method_
  299. */
  300. public void reset();
  301. /**
  302. * @return The next row in this table (Column name -&gt; Column value) (uses
  303. * the {@link #getDefaultCursor default cursor})
  304. * @usage _general_method_
  305. */
  306. public Row getNextRow() throws IOException;
  307. /**
  308. * @return a simple Cursor, initialized on demand and held by this table.
  309. * This cursor backs the row traversal methods available on the
  310. * Table interface. For advanced Table traversal and manipulation,
  311. * use the Cursor directly.
  312. */
  313. public Cursor getDefaultCursor();
  314. /**
  315. * Convenience method for constructing a new CursorBuilder for this Table.
  316. */
  317. public CursorBuilder newCursor();
  318. /**
  319. * Convenience method for constructing a new OleBlob.Builder.
  320. */
  321. default public OleBlob.Builder newBlob() {
  322. return new OleBlob.Builder();
  323. }
  324. }