1 package jp.sourceforge.ea2ddl.dao.allcommon.s2dao;
\r
3 import java.sql.ResultSet;
\r
4 import java.sql.Statement;
\r
5 import java.sql.SQLException;
\r
7 import org.seasar.extension.jdbc.impl.ResultSetWrapper;
\r
9 import jp.sourceforge.ea2ddl.dao.allcommon.cbean.FetchNarrowingBean;
\r
10 import jp.sourceforge.ea2ddl.dao.allcommon.s2dao.internal.sqlhandler.InternalBasicHandler.SQLExceptionHandler;
\r
13 * The wrapper of fetch narrowing result set.
\r
14 * @author DBFlute(AutoGenerator)
\r
16 public class FetchNarrowingResultSetWrapper extends ResultSetWrapper {
\r
18 // ===================================================================================
\r
21 /** The real result set. (NotNull) */
\r
22 protected ResultSet _resultSet;
\r
24 /** The bean of fetch narrowing. (NotNull) */
\r
25 protected FetchNarrowingBean _fetchNarrowingBean;
\r
27 /** The counter of fetch. */
\r
28 protected long _fetchCounter;
\r
30 /** the counter of request. */
\r
31 protected long _requestCounter;
\r
33 /** Does it offset by cursor forcedly? */
\r
34 protected boolean _offsetByCursorForcedly;
\r
36 /** Does it limit by cursor forcedly? */
\r
37 protected boolean _limitByCursorForcedly;
\r
39 /** Does it skip to cursor end? */
\r
40 protected boolean _skipToCursorEnd;
\r
42 // ===================================================================================
\r
47 * @param resultSet Original result set. (NotNull)
\r
48 * @param fetchNarrowingBean Fetch-narrowing-bean. (NotNull)
\r
49 * @param offsetByCursorForcedly Offset by cursor forcedly.
\r
50 * @param limitByCursorForcedly Limit by cursor forcedly.
\r
52 public FetchNarrowingResultSetWrapper(ResultSet resultSet, FetchNarrowingBean fetchNarrowingBean
\r
53 , boolean offsetByCursorForcedly, boolean limitByCursorForcedly) {
\r
56 _resultSet = resultSet;
\r
57 _fetchNarrowingBean = fetchNarrowingBean;
\r
58 _offsetByCursorForcedly = offsetByCursorForcedly;
\r
59 _limitByCursorForcedly = limitByCursorForcedly;
\r
64 // ===================================================================================
\r
68 * Skip to start-index.
\r
70 protected void skip() {
\r
71 if (!isAvailableSkipRecord()) {
\r
74 final int skipStartIndex = getFetchNarrowingSkipStartIndex();
\r
75 if (isScrollableCursor()) {
\r
77 if (0 == skipStartIndex) {
\r
78 _resultSet.beforeFirst();
\r
80 _resultSet.absolute(skipStartIndex);
\r
82 _fetchCounter = _resultSet.getRow();
\r
83 } catch (SQLException e) {
\r
84 handleSQLException(e, null);
\r
89 if (_fetchCounter >= skipStartIndex) {
\r
92 if (!_resultSet.next()) {
\r
93 _skipToCursorEnd = true;// [DBFLUTE-243]
\r
98 } catch (SQLException e) {
\r
99 handleSQLException(e, null);
\r
104 protected boolean isAvailableSkipRecord() {
\r
105 if (!isFetchNarrowingEffective()) {
\r
108 if (isOffsetByCursorForcedly()) {
\r
111 if (isFetchNarrowingSkipStartIndexEffective()) {
\r
117 // ===================================================================================
\r
122 * @return Does the result set have next record?
\r
123 * @throws SQLException
\r
125 public boolean next() throws SQLException {
\r
126 final boolean hasNext = super.next();
\r
128 if (!isAvailableLimitLoopCount()) {
\r
129 checkSafetyResult(hasNext);
\r
133 if (hasNext && _fetchCounter < getFetchNarrowingSkipStartIndex() + getFetchNarrowingLoopCount()) {
\r
135 checkSafetyResult(true);
\r
142 protected boolean isAvailableLimitLoopCount() {
\r
143 if (!isFetchNarrowingEffective()) {
\r
146 if (isLimitByCursorForcedly()) {
\r
149 if (isFetchNarrowingLoopCountEffective()) {
\r
155 protected void checkSafetyResult(boolean hasNext) {
\r
156 if (hasNext && getSafetyMaxResultSize() > 0 && _requestCounter > (getSafetyMaxResultSize() + 1)) {
\r
157 String msg = "You have already been in Danger Zone!";
\r
158 msg = msg + " Please confirm your query or data of table: safetyMaxResultSize=" + getSafetyMaxResultSize();
\r
159 throw new jp.sourceforge.ea2ddl.dao.allcommon.exception.DangerousResultSizeException(msg, getSafetyMaxResultSize());
\r
163 // ===================================================================================
\r
166 protected boolean isFetchNarrowingEffective() {
\r
167 return _fetchNarrowingBean.isFetchNarrowingEffective();
\r
170 protected boolean isFetchNarrowingSkipStartIndexEffective() {
\r
171 return _fetchNarrowingBean.isFetchNarrowingSkipStartIndexEffective();
\r
174 protected boolean isFetchNarrowingLoopCountEffective() {
\r
175 return _fetchNarrowingBean.isFetchNarrowingLoopCountEffective();
\r
178 protected int getFetchNarrowingSkipStartIndex() {
\r
179 return _fetchNarrowingBean.getFetchNarrowingSkipStartIndex();
\r
182 protected int getFetchNarrowingLoopCount() {
\r
183 return _fetchNarrowingBean.getFetchNarrowingLoopCount();
\r
186 public int getSafetyMaxResultSize() {
\r
187 return _fetchNarrowingBean.getSafetyMaxResultSize();
\r
190 protected boolean isScrollableCursor() {
\r
192 return !(_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY);
\r
193 } catch (SQLException e) {
\r
194 handleSQLException(e, null);
\r
195 return false;// Unreachable!
\r
199 // ===================================================================================
\r
200 // Exception Handler
\r
201 // =================
\r
202 protected void handleSQLException(SQLException e, Statement statement) {
\r
203 new SQLExceptionHandler().handleSQLException(e, statement);
\r
206 // ===================================================================================
\r
209 public boolean isOffsetByCursorForcedly() {
\r
210 return _offsetByCursorForcedly;
\r
213 public boolean isLimitByCursorForcedly() {
\r
214 return _limitByCursorForcedly;
\r
217 public boolean isSkipToCursorEnd() {
\r
218 return _skipToCursorEnd;
\r