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

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