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.

Cursor.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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.Collection;
  16. import java.util.Iterator;
  17. import java.util.Map;
  18. import com.healthmarketscience.jackcess.util.ColumnMatcher;
  19. import com.healthmarketscience.jackcess.util.ErrorHandler;
  20. import com.healthmarketscience.jackcess.util.IterableBuilder;
  21. /**
  22. * Manages iteration for a {@link Table}. Different cursors provide different
  23. * methods of traversing a table. Cursors should be fairly robust in the face
  24. * of table modification during traversal (although depending on how the table
  25. * is traversed, row updates may or may not be seen). Multiple cursors may
  26. * traverse the same table simultaneously.
  27. * <p/>
  28. * Basic cursors will generally iterate table data in the order it appears in
  29. * the database and searches will require scanning the entire table.
  30. * Additional features are available when utilizing an {@link Index} backed
  31. * {@link IndexCursor}.
  32. * <p>
  33. * The {@link CursorBuilder} provides a variety of static utility methods to
  34. * construct cursors with given characteristics or easily search for specific
  35. * values as well as friendly and flexible construction options.
  36. * <p>
  37. * A Cursor instance is not thread-safe (see {@link Database} for more
  38. * thread-safety details).
  39. *
  40. * @author James Ahlborn
  41. * @usage _general_class_
  42. */
  43. public interface Cursor extends Iterable<Row>
  44. {
  45. public Id getId();
  46. public Table getTable();
  47. /**
  48. * Gets the currently configured ErrorHandler (always non-{@code null}).
  49. * This will be used to handle all errors.
  50. */
  51. public ErrorHandler getErrorHandler();
  52. /**
  53. * Sets a new ErrorHandler. If {@code null}, resets to using the
  54. * ErrorHandler configured at the Table level.
  55. */
  56. public void setErrorHandler(ErrorHandler newErrorHandler);
  57. /**
  58. * Returns the currently configured ColumnMatcher, always non-{@code null}.
  59. */
  60. public ColumnMatcher getColumnMatcher();
  61. /**
  62. * Sets a new ColumnMatcher. If {@code null}, resets to using the default
  63. * matcher (default depends on Cursor type).
  64. */
  65. public void setColumnMatcher(ColumnMatcher columnMatcher);
  66. /**
  67. * Returns the current state of the cursor which can be restored at a future
  68. * point in time by a call to {@link #restoreSavepoint}.
  69. * <p>
  70. * Savepoints may be used across different cursor instances for the same
  71. * table, but they must have the same {@link Id}.
  72. */
  73. public Savepoint getSavepoint();
  74. /**
  75. * Moves the cursor to a savepoint previously returned from
  76. * {@link #getSavepoint}.
  77. * @throws IllegalArgumentException if the given savepoint does not have a
  78. * cursorId equal to this cursor's id
  79. */
  80. public void restoreSavepoint(Savepoint savepoint)
  81. throws IOException;
  82. /**
  83. * Resets this cursor for forward traversal. Calls {@link #beforeFirst}.
  84. */
  85. public void reset();
  86. /**
  87. * Resets this cursor for forward traversal (sets cursor to before the first
  88. * row).
  89. */
  90. public void beforeFirst();
  91. /**
  92. * Resets this cursor for reverse traversal (sets cursor to after the last
  93. * row).
  94. */
  95. public void afterLast();
  96. /**
  97. * Returns {@code true} if the cursor is currently positioned before the
  98. * first row, {@code false} otherwise.
  99. */
  100. public boolean isBeforeFirst() throws IOException;
  101. /**
  102. * Returns {@code true} if the cursor is currently positioned after the
  103. * last row, {@code false} otherwise.
  104. */
  105. public boolean isAfterLast() throws IOException;
  106. /**
  107. * Returns {@code true} if the row at which the cursor is currently
  108. * positioned is deleted, {@code false} otherwise (including invalid rows).
  109. */
  110. public boolean isCurrentRowDeleted() throws IOException;
  111. /**
  112. * Calls {@link #beforeFirst} on this cursor and returns a modifiable
  113. * Iterator which will iterate through all the rows of this table. Use of
  114. * the Iterator follows the same restrictions as a call to
  115. * {@link #getNextRow}.
  116. * <p/>
  117. * For more flexible iteration see {@link #newIterable}.
  118. * @throws RuntimeIOException if an IOException is thrown by one of the
  119. * operations, the actual exception will be contained within
  120. */
  121. public Iterator<Row> iterator();
  122. /**
  123. * Convenience method for constructing a new IterableBuilder for this
  124. * cursor. An IterableBuilder provides a variety of options for more
  125. * flexible iteration.
  126. */
  127. public IterableBuilder newIterable();
  128. /**
  129. * Delete the current row.
  130. * <p/>
  131. * Note, re-deleting an already deleted row is allowed (it does nothing).
  132. * @throws IllegalStateException if the current row is not valid (at
  133. * beginning or end of table)
  134. */
  135. public void deleteCurrentRow() throws IOException;
  136. /**
  137. * Update the current row.
  138. * @return the given row values if long enough, otherwise a new array,
  139. * updated with the current row values
  140. * @throws IllegalStateException if the current row is not valid (at
  141. * beginning or end of table), or deleted.
  142. */
  143. public Object[] updateCurrentRow(Object... row) throws IOException;
  144. /**
  145. * Update the current row.
  146. * @return the given row, updated with the current row values
  147. * @throws IllegalStateException if the current row is not valid (at
  148. * beginning or end of table), or deleted.
  149. */
  150. public <M extends Map<String,Object>> M updateCurrentRowFromMap(M row)
  151. throws IOException;
  152. /**
  153. * Moves to the next row in the table and returns it.
  154. * @return The next row in this table (Column name -> Column value), or
  155. * {@code null} if no next row is found
  156. */
  157. public Row getNextRow() throws IOException;
  158. /**
  159. * Moves to the next row in the table and returns it.
  160. * @param columnNames Only column names in this collection will be returned
  161. * @return The next row in this table (Column name -> Column value), or
  162. * {@code null} if no next row is found
  163. */
  164. public Row getNextRow(Collection<String> columnNames)
  165. throws IOException;
  166. /**
  167. * Moves to the previous row in the table and returns it.
  168. * @return The previous row in this table (Column name -> Column value), or
  169. * {@code null} if no previous row is found
  170. */
  171. public Row getPreviousRow() throws IOException;
  172. /**
  173. * Moves to the previous row in the table and returns it.
  174. * @param columnNames Only column names in this collection will be returned
  175. * @return The previous row in this table (Column name -> Column value), or
  176. * {@code null} if no previous row is found
  177. */
  178. public Row getPreviousRow(Collection<String> columnNames)
  179. throws IOException;
  180. /**
  181. * Moves to the next row as defined by this cursor.
  182. * @return {@code true} if a valid next row was found, {@code false}
  183. * otherwise
  184. */
  185. public boolean moveToNextRow() throws IOException;
  186. /**
  187. * Moves to the previous row as defined by this cursor.
  188. * @return {@code true} if a valid previous row was found, {@code false}
  189. * otherwise
  190. */
  191. public boolean moveToPreviousRow() throws IOException;
  192. /**
  193. * Moves to the row with the given rowId. If the row is not found (or an
  194. * exception is thrown), the cursor is restored to its previous state.
  195. *
  196. * @return {@code true} if a valid row was found with the given id,
  197. * {@code false} if no row was found
  198. */
  199. public boolean findRow(RowId rowId) throws IOException;
  200. /**
  201. * Moves to the first row (as defined by the cursor) where the given column
  202. * has the given value. This may be more efficient on some cursors than
  203. * others. If a match is not found (or an exception is thrown), the cursor
  204. * is restored to its previous state.
  205. * <p>
  206. * Warning, this method <i>always</i> starts searching from the beginning of
  207. * the Table (you cannot use it to find successive matches).
  208. *
  209. * @param columnPattern column from the table for this cursor which is being
  210. * matched by the valuePattern
  211. * @param valuePattern value which is equal to the corresponding value in
  212. * the matched row
  213. * @return {@code true} if a valid row was found with the given value,
  214. * {@code false} if no row was found
  215. */
  216. public boolean findFirstRow(Column columnPattern, Object valuePattern)
  217. throws IOException;
  218. /**
  219. * Moves to the next row (as defined by the cursor) where the given column
  220. * has the given value. This may be more efficient on some cursors than
  221. * others. If a match is not found (or an exception is thrown), the cursor
  222. * is restored to its previous state.
  223. *
  224. * @param columnPattern column from the table for this cursor which is being
  225. * matched by the valuePattern
  226. * @param valuePattern value which is equal to the corresponding value in
  227. * the matched row
  228. * @return {@code true} if a valid row was found with the given value,
  229. * {@code false} if no row was found
  230. */
  231. public boolean findNextRow(Column columnPattern, Object valuePattern)
  232. throws IOException;
  233. /**
  234. * Moves to the first row (as defined by the cursor) where the given columns
  235. * have the given values. This may be more efficient on some cursors than
  236. * others. If a match is not found (or an exception is thrown), the cursor
  237. * is restored to its previous state.
  238. * <p>
  239. * Warning, this method <i>always</i> starts searching from the beginning of
  240. * the Table (you cannot use it to find successive matches).
  241. *
  242. * @param rowPattern column names and values which must be equal to the
  243. * corresponding values in the matched row
  244. * @return {@code true} if a valid row was found with the given values,
  245. * {@code false} if no row was found
  246. */
  247. public boolean findFirstRow(Map<String,?> rowPattern) throws IOException;
  248. /**
  249. * Moves to the next row (as defined by the cursor) where the given columns
  250. * have the given values. This may be more efficient on some cursors than
  251. * others. If a match is not found (or an exception is thrown), the cursor
  252. * is restored to its previous state.
  253. *
  254. * @param rowPattern column names and values which must be equal to the
  255. * corresponding values in the matched row
  256. * @return {@code true} if a valid row was found with the given values,
  257. * {@code false} if no row was found
  258. */
  259. public boolean findNextRow(Map<String,?> rowPattern) throws IOException;
  260. /**
  261. * Returns {@code true} if the current row matches the given pattern.
  262. * @param columnPattern column from the table for this cursor which is being
  263. * matched by the valuePattern
  264. * @param valuePattern value which is tested for equality with the
  265. * corresponding value in the current row
  266. */
  267. public boolean currentRowMatches(Column columnPattern, Object valuePattern)
  268. throws IOException;
  269. /**
  270. * Returns {@code true} if the current row matches the given pattern.
  271. * @param rowPattern column names and values which must be equal to the
  272. * corresponding values in the current row
  273. */
  274. public boolean currentRowMatches(Map<String,?> rowPattern) throws IOException;
  275. /**
  276. * Moves forward as many rows as possible up to the given number of rows.
  277. * @return the number of rows moved.
  278. */
  279. public int moveNextRows(int numRows) throws IOException;
  280. /**
  281. * Moves backward as many rows as possible up to the given number of rows.
  282. * @return the number of rows moved.
  283. */
  284. public int movePreviousRows(int numRows) throws IOException;
  285. /**
  286. * Returns the current row in this cursor (Column name -> Column value).
  287. */
  288. public Row getCurrentRow() throws IOException;
  289. /**
  290. * Returns the current row in this cursor (Column name -> Column value).
  291. * @param columnNames Only column names in this collection will be returned
  292. */
  293. public Row getCurrentRow(Collection<String> columnNames)
  294. throws IOException;
  295. /**
  296. * Returns the given column from the current row.
  297. */
  298. public Object getCurrentRowValue(Column column) throws IOException;
  299. /**
  300. * Updates a single value in the current row.
  301. * @throws IllegalStateException if the current row is not valid (at
  302. * beginning or end of table), or deleted.
  303. */
  304. public void setCurrentRowValue(Column column, Object value)
  305. throws IOException;
  306. /**
  307. * Identifier for a cursor. Will be equal to any other cursor of the same
  308. * type for the same table. Primarily used to check the validity of a
  309. * Savepoint.
  310. */
  311. public interface Id
  312. {
  313. }
  314. /**
  315. * Value object which maintains the current position of the cursor.
  316. */
  317. public interface Position
  318. {
  319. /**
  320. * Returns the unique RowId of the position of the cursor.
  321. */
  322. public RowId getRowId();
  323. }
  324. /**
  325. * Value object which represents a complete save state of the cursor.
  326. * Savepoints are created by calling {@link Cursor#getSavepoint} and used by
  327. * calling {@link Cursor#restoreSavepoint} to return the the cursor state at
  328. * the time the Savepoint was created.
  329. */
  330. public interface Savepoint
  331. {
  332. public Id getCursorId();
  333. public Position getCurrentPosition();
  334. }
  335. }