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.

QueryDelegate.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util.sqlcontainer.query;
  17. import java.io.Serializable;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.List;
  21. import com.vaadin.data.Container.Filter;
  22. import com.vaadin.data.util.sqlcontainer.RowId;
  23. import com.vaadin.data.util.sqlcontainer.RowItem;
  24. public interface QueryDelegate extends Serializable {
  25. /**
  26. * Generates and executes a query to determine the current row count from
  27. * the DB. Row count will be fetched using filters that are currently set to
  28. * the QueryDelegate.
  29. *
  30. * @return row count
  31. * @throws SQLException
  32. */
  33. public int getCount() throws SQLException;
  34. /**
  35. * Executes a paged SQL query and returns the ResultSet. The query is
  36. * defined through implementations of this QueryDelegate interface.
  37. *
  38. * @param offset
  39. * the first item of the page to load
  40. * @param pagelength
  41. * the length of the page to load
  42. * @return a ResultSet containing the rows of the page
  43. * @throws SQLException
  44. * if the database access fails.
  45. */
  46. public ResultSet getResults(int offset, int pagelength) throws SQLException;
  47. /**
  48. * Allows the SQLContainer implementation to check whether the QueryDelegate
  49. * implementation implements paging in the getResults method.
  50. *
  51. * @see QueryDelegate#getResults(int, int)
  52. *
  53. * @return true if the delegate implements paging
  54. */
  55. public boolean implementationRespectsPagingLimits();
  56. /**
  57. * Sets the filters to apply when performing the SQL query. These are
  58. * translated into a WHERE clause. Default filtering mode will be used.
  59. *
  60. * @param filters
  61. * The filters to apply.
  62. * @throws UnsupportedOperationException
  63. * if the implementation doesn't support filtering.
  64. */
  65. public void setFilters(List<Filter> filters)
  66. throws UnsupportedOperationException;
  67. /**
  68. * Sets the order in which to retrieve rows from the database. The result
  69. * can be ordered by zero or more columns and each column can be in
  70. * ascending or descending order. These are translated into an ORDER BY
  71. * clause in the SQL query.
  72. *
  73. * @param orderBys
  74. * A list of the OrderBy conditions.
  75. * @throws UnsupportedOperationException
  76. * if the implementation doesn't support ordering.
  77. */
  78. public void setOrderBy(List<OrderBy> orderBys)
  79. throws UnsupportedOperationException;
  80. /**
  81. * Stores a row in the database. The implementation of this interface
  82. * decides how to identify whether to store a new row or update an existing
  83. * one.
  84. *
  85. * @param columnToValueMap
  86. * A map containing the values for all columns to be stored or
  87. * updated.
  88. * @return the number of affected rows in the database table
  89. * @throws UnsupportedOperationException
  90. * if the implementation is read only.
  91. */
  92. public int storeRow(RowItem row) throws UnsupportedOperationException,
  93. SQLException;
  94. /**
  95. * Removes the given RowItem from the database.
  96. *
  97. * @param row
  98. * RowItem to be removed
  99. * @return true on success
  100. * @throws UnsupportedOperationException
  101. * @throws SQLException
  102. */
  103. public boolean removeRow(RowItem row) throws UnsupportedOperationException,
  104. SQLException;
  105. /**
  106. * Starts a new database transaction. Used when storing multiple changes.
  107. *
  108. * Note that if a transaction is already open, it will be rolled back when a
  109. * new transaction is started.
  110. *
  111. * @throws SQLException
  112. * if the database access fails.
  113. */
  114. public void beginTransaction() throws SQLException;
  115. /**
  116. * Commits a transaction. If a transaction is not open nothing should
  117. * happen.
  118. *
  119. * @throws SQLException
  120. * if the database access fails.
  121. */
  122. public void commit() throws SQLException;
  123. /**
  124. * Rolls a transaction back. If a transaction is not open nothing should
  125. * happen.
  126. *
  127. * @throws SQLException
  128. * if the database access fails.
  129. */
  130. public void rollback() throws SQLException;
  131. /**
  132. * Returns a list of primary key column names. The list is either fetched
  133. * from the database (TableQuery) or given as an argument depending on
  134. * implementation.
  135. *
  136. * @return
  137. */
  138. public List<String> getPrimaryKeyColumns();
  139. /**
  140. * Performs a query to find out whether the SQL table contains a row with
  141. * the given set of primary keys.
  142. *
  143. * @param keys
  144. * the primary keys
  145. * @return true if the SQL table contains a row with the provided keys
  146. * @throws SQLException
  147. */
  148. public boolean containsRowWithKey(Object... keys) throws SQLException;
  149. /************************/
  150. /** ROWID CHANGE EVENT **/
  151. /************************/
  152. /**
  153. * An <code>Event</code> object specifying the old and new RowId of an added
  154. * item after the addition has been successfully committed.
  155. */
  156. public interface RowIdChangeEvent extends Serializable {
  157. /**
  158. * Gets the old (temporary) RowId of the added row that raised this
  159. * event.
  160. *
  161. * @return old RowId
  162. */
  163. public RowId getOldRowId();
  164. /**
  165. * Gets the new, possibly database assigned RowId of the added row that
  166. * raised this event.
  167. *
  168. * @return new RowId
  169. */
  170. public RowId getNewRowId();
  171. }
  172. /** RowId change listener interface. */
  173. public interface RowIdChangeListener extends Serializable {
  174. /**
  175. * Lets the listener know that a RowId has been changed.
  176. *
  177. * @param event
  178. */
  179. public void rowIdChange(QueryDelegate.RowIdChangeEvent event);
  180. }
  181. /**
  182. * The interface for adding and removing <code>RowIdChangeEvent</code>
  183. * listeners. By implementing this interface a class explicitly announces
  184. * that it will generate a <code>RowIdChangeEvent</code> when it performs a
  185. * database commit that may change the RowId.
  186. */
  187. public interface RowIdChangeNotifier extends Serializable {
  188. /**
  189. * Adds a RowIdChangeListener for the object.
  190. *
  191. * @param listener
  192. * listener to be added
  193. */
  194. public void addRowIdChangeListener(
  195. QueryDelegate.RowIdChangeListener listener);
  196. /**
  197. * @deprecated As of 7.0, replaced by
  198. * {@link #addRowIdChangeListener(RowIdChangeListener)}
  199. **/
  200. @Deprecated
  201. public void addListener(QueryDelegate.RowIdChangeListener listener);
  202. /**
  203. * Removes the specified RowIdChangeListener from the object.
  204. *
  205. * @param listener
  206. * listener to be removed
  207. */
  208. public void removeRowIdChangeListener(
  209. QueryDelegate.RowIdChangeListener listener);
  210. /**
  211. * @deprecated As of 7.0, replaced by
  212. * {@link #removeRowIdChangeListener(RowIdChangeListener)}
  213. **/
  214. @Deprecated
  215. public void removeListener(QueryDelegate.RowIdChangeListener listener);
  216. }
  217. }