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

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