001: //$Id: ScrollableResultsImpl.java 7469 2005-07-14 13:12:19Z steveebersole $
002: package org.hibernate.impl;
003:
004: import org.hibernate.HibernateException;
005: import org.hibernate.MappingException;
006: import org.hibernate.ScrollableResults;
007: import org.hibernate.engine.QueryParameters;
008: import org.hibernate.engine.SessionImplementor;
009: import org.hibernate.exception.JDBCExceptionHelper;
010: import org.hibernate.hql.HolderInstantiator;
011: import org.hibernate.loader.Loader;
012: import org.hibernate.type.Type;
013:
014: import java.sql.PreparedStatement;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017:
018: /**
019: * Implementation of the <tt>ScrollableResults</tt> interface
020: * @author Gavin King
021: */
022: public class ScrollableResultsImpl extends AbstractScrollableResults
023: implements ScrollableResults {
024:
025: private Object[] currentRow;
026:
027: public ScrollableResultsImpl(ResultSet rs, PreparedStatement ps,
028: SessionImplementor sess, Loader loader,
029: QueryParameters queryParameters, Type[] types,
030: HolderInstantiator holderInstantiator)
031: throws MappingException {
032: super (rs, ps, sess, loader, queryParameters, types,
033: holderInstantiator);
034: }
035:
036: protected Object[] getCurrentRow() {
037: return currentRow;
038: }
039:
040: /**
041: * @see org.hibernate.ScrollableResults#scroll(int)
042: */
043: public boolean scroll(int i) throws HibernateException {
044: try {
045: boolean result = getResultSet().relative(i);
046: prepareCurrentRow(result);
047: return result;
048: } catch (SQLException sqle) {
049: throw JDBCExceptionHelper.convert(getSession().getFactory()
050: .getSQLExceptionConverter(), sqle,
051: "could not advance using scroll()");
052: }
053: }
054:
055: /**
056: * @see org.hibernate.ScrollableResults#first()
057: */
058: public boolean first() throws HibernateException {
059: try {
060: boolean result = getResultSet().first();
061: prepareCurrentRow(result);
062: return result;
063: } catch (SQLException sqle) {
064: throw JDBCExceptionHelper.convert(getSession().getFactory()
065: .getSQLExceptionConverter(), sqle,
066: "could not advance using first()");
067: }
068: }
069:
070: /**
071: * @see org.hibernate.ScrollableResults#last()
072: */
073: public boolean last() throws HibernateException {
074: try {
075: boolean result = getResultSet().last();
076: prepareCurrentRow(result);
077: return result;
078: } catch (SQLException sqle) {
079: throw JDBCExceptionHelper.convert(getSession().getFactory()
080: .getSQLExceptionConverter(), sqle,
081: "could not advance using last()");
082: }
083: }
084:
085: /**
086: * @see org.hibernate.ScrollableResults#next()
087: */
088: public boolean next() throws HibernateException {
089: try {
090: boolean result = getResultSet().next();
091: prepareCurrentRow(result);
092: return result;
093: } catch (SQLException sqle) {
094: throw JDBCExceptionHelper.convert(getSession().getFactory()
095: .getSQLExceptionConverter(), sqle,
096: "could not advance using next()");
097: }
098: }
099:
100: /**
101: * @see org.hibernate.ScrollableResults#previous()
102: */
103: public boolean previous() throws HibernateException {
104: try {
105: boolean result = getResultSet().previous();
106: prepareCurrentRow(result);
107: return result;
108: } catch (SQLException sqle) {
109: throw JDBCExceptionHelper.convert(getSession().getFactory()
110: .getSQLExceptionConverter(), sqle,
111: "could not advance using previous()");
112: }
113: }
114:
115: /**
116: * @see org.hibernate.ScrollableResults#afterLast()
117: */
118: public void afterLast() throws HibernateException {
119: try {
120: getResultSet().afterLast();
121: } catch (SQLException sqle) {
122: throw JDBCExceptionHelper.convert(getSession().getFactory()
123: .getSQLExceptionConverter(), sqle,
124: "exception calling afterLast()");
125: }
126: }
127:
128: /**
129: * @see org.hibernate.ScrollableResults#beforeFirst()
130: */
131: public void beforeFirst() throws HibernateException {
132: try {
133: getResultSet().beforeFirst();
134: } catch (SQLException sqle) {
135: throw JDBCExceptionHelper.convert(getSession().getFactory()
136: .getSQLExceptionConverter(), sqle,
137: "exception calling beforeFirst()");
138: }
139: }
140:
141: /**
142: * @see org.hibernate.ScrollableResults#isFirst()
143: */
144: public boolean isFirst() throws HibernateException {
145: try {
146: return getResultSet().isFirst();
147: } catch (SQLException sqle) {
148: throw JDBCExceptionHelper.convert(getSession().getFactory()
149: .getSQLExceptionConverter(), sqle,
150: "exception calling isFirst()");
151: }
152: }
153:
154: /**
155: * @see org.hibernate.ScrollableResults#isLast()
156: */
157: public boolean isLast() throws HibernateException {
158: try {
159: return getResultSet().isLast();
160: } catch (SQLException sqle) {
161: throw JDBCExceptionHelper.convert(getSession().getFactory()
162: .getSQLExceptionConverter(), sqle,
163: "exception calling isLast()");
164: }
165: }
166:
167: public int getRowNumber() throws HibernateException {
168: try {
169: return getResultSet().getRow() - 1;
170: } catch (SQLException sqle) {
171: throw JDBCExceptionHelper.convert(getSession().getFactory()
172: .getSQLExceptionConverter(), sqle,
173: "exception calling getRow()");
174: }
175: }
176:
177: public boolean setRowNumber(int rowNumber)
178: throws HibernateException {
179: if (rowNumber >= 0)
180: rowNumber++;
181: try {
182: boolean result = getResultSet().absolute(rowNumber);
183: prepareCurrentRow(result);
184: return result;
185: } catch (SQLException sqle) {
186: throw JDBCExceptionHelper.convert(getSession().getFactory()
187: .getSQLExceptionConverter(), sqle,
188: "could not advance using absolute()");
189: }
190: }
191:
192: private void prepareCurrentRow(boolean underlyingScrollSuccessful)
193: throws HibernateException {
194:
195: if (!underlyingScrollSuccessful) {
196: currentRow = null;
197: return;
198: }
199:
200: Object result = getLoader().loadSingleRow(getResultSet(),
201: getSession(), getQueryParameters(), false);
202: if (result != null && result.getClass().isArray()) {
203: currentRow = (Object[]) result;
204: } else {
205: currentRow = new Object[] { result };
206: }
207:
208: if (getHolderInstantiator() != null) {
209: currentRow = new Object[] { getHolderInstantiator()
210: .instantiate(currentRow) };
211: }
212:
213: afterScrollOperation();
214: }
215:
216: }
|