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

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