KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > impl > ScrollableResultsImpl


1 //$Id: ScrollableResultsImpl.java,v 1.17 2005/07/14 13:12:01 steveebersole Exp $
2
package org.hibernate.impl;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.MappingException;
6 import org.hibernate.ScrollableResults;
7 import org.hibernate.engine.QueryParameters;
8 import org.hibernate.engine.SessionImplementor;
9 import org.hibernate.exception.JDBCExceptionHelper;
10 import org.hibernate.hql.HolderInstantiator;
11 import org.hibernate.loader.Loader;
12 import org.hibernate.type.Type;
13
14 import java.sql.PreparedStatement JavaDoc;
15 import java.sql.ResultSet JavaDoc;
16 import java.sql.SQLException JavaDoc;
17
18 /**
19  * Implementation of the <tt>ScrollableResults</tt> interface
20  * @author Gavin King
21  */

22 public class ScrollableResultsImpl extends AbstractScrollableResults implements ScrollableResults {
23
24     private Object JavaDoc[] currentRow;
25
26     public ScrollableResultsImpl(
27             ResultSet JavaDoc rs,
28             PreparedStatement JavaDoc ps,
29             SessionImplementor sess,
30             Loader loader,
31             QueryParameters queryParameters,
32             Type[] types, HolderInstantiator holderInstantiator) throws MappingException {
33         super( rs, ps, sess, loader, queryParameters, types, holderInstantiator );
34     }
35
36     protected Object JavaDoc[] getCurrentRow() {
37         return currentRow;
38     }
39
40     /**
41      * @see org.hibernate.ScrollableResults#scroll(int)
42      */

43     public boolean scroll(int i) throws HibernateException {
44         try {
45             boolean result = getResultSet().relative(i);
46             prepareCurrentRow(result);
47             return result;
48         }
49         catch (SQLException JavaDoc sqle) {
50             throw JDBCExceptionHelper.convert(
51                     getSession().getFactory().getSQLExceptionConverter(),
52                     sqle,
53                     "could not advance using scroll()"
54                 );
55         }
56     }
57
58     /**
59      * @see org.hibernate.ScrollableResults#first()
60      */

61     public boolean first() throws HibernateException {
62         try {
63             boolean result = getResultSet().first();
64             prepareCurrentRow(result);
65             return result;
66         }
67         catch (SQLException JavaDoc sqle) {
68             throw JDBCExceptionHelper.convert(
69                     getSession().getFactory().getSQLExceptionConverter(),
70                     sqle,
71                     "could not advance using first()"
72                 );
73         }
74     }
75
76     /**
77      * @see org.hibernate.ScrollableResults#last()
78      */

79     public boolean last() throws HibernateException {
80         try {
81             boolean result = getResultSet().last();
82             prepareCurrentRow(result);
83             return result;
84         }
85         catch (SQLException JavaDoc sqle) {
86             throw JDBCExceptionHelper.convert(
87                     getSession().getFactory().getSQLExceptionConverter(),
88                     sqle,
89                     "could not advance using last()"
90                 );
91         }
92     }
93
94     /**
95      * @see org.hibernate.ScrollableResults#next()
96      */

97     public boolean next() throws HibernateException {
98         try {
99             boolean result = getResultSet().next();
100             prepareCurrentRow(result);
101             return result;
102         }
103         catch (SQLException JavaDoc sqle) {
104             throw JDBCExceptionHelper.convert(
105                     getSession().getFactory().getSQLExceptionConverter(),
106                     sqle,
107                     "could not advance using next()"
108                 );
109         }
110     }
111
112     /**
113      * @see org.hibernate.ScrollableResults#previous()
114      */

115     public boolean previous() throws HibernateException {
116         try {
117             boolean result = getResultSet().previous();
118             prepareCurrentRow(result);
119             return result;
120         }
121         catch (SQLException JavaDoc sqle) {
122             throw JDBCExceptionHelper.convert(
123                     getSession().getFactory().getSQLExceptionConverter(),
124                     sqle,
125                     "could not advance using previous()"
126                 );
127         }
128     }
129
130     /**
131      * @see org.hibernate.ScrollableResults#afterLast()
132      */

133     public void afterLast() throws HibernateException {
134         try {
135             getResultSet().afterLast();
136         }
137         catch (SQLException JavaDoc sqle) {
138             throw JDBCExceptionHelper.convert(
139                     getSession().getFactory().getSQLExceptionConverter(),
140                     sqle,
141                     "exception calling afterLast()"
142                 );
143         }
144     }
145
146     /**
147      * @see org.hibernate.ScrollableResults#beforeFirst()
148      */

149     public void beforeFirst() throws HibernateException {
150         try {
151             getResultSet().beforeFirst();
152         }
153         catch (SQLException JavaDoc sqle) {
154             throw JDBCExceptionHelper.convert(
155                     getSession().getFactory().getSQLExceptionConverter(),
156                     sqle,
157                     "exception calling beforeFirst()"
158                 );
159         }
160     }
161
162     /**
163      * @see org.hibernate.ScrollableResults#isFirst()
164      */

165     public boolean isFirst() throws HibernateException {
166         try {
167             return getResultSet().isFirst();
168         }
169         catch (SQLException JavaDoc sqle) {
170             throw JDBCExceptionHelper.convert(
171                     getSession().getFactory().getSQLExceptionConverter(),
172                     sqle,
173                     "exception calling isFirst()"
174                 );
175         }
176     }
177
178     /**
179      * @see org.hibernate.ScrollableResults#isLast()
180      */

181     public boolean isLast() throws HibernateException {
182         try {
183             return getResultSet().isLast();
184         }
185         catch (SQLException JavaDoc sqle) {
186             throw JDBCExceptionHelper.convert(
187                     getSession().getFactory().getSQLExceptionConverter(),
188                     sqle,
189                     "exception calling isLast()"
190                 );
191         }
192     }
193
194     public int getRowNumber() throws HibernateException {
195         try {
196             return getResultSet().getRow()-1;
197         }
198         catch (SQLException JavaDoc sqle) {
199             throw JDBCExceptionHelper.convert(
200                     getSession().getFactory().getSQLExceptionConverter(),
201                     sqle,
202                     "exception calling getRow()"
203                 );
204         }
205     }
206
207     public boolean setRowNumber(int rowNumber) throws HibernateException {
208         if (rowNumber>=0) rowNumber++;
209         try {
210             boolean result = getResultSet().absolute(rowNumber);
211             prepareCurrentRow(result);
212             return result;
213         }
214         catch (SQLException JavaDoc sqle) {
215             throw JDBCExceptionHelper.convert(
216                     getSession().getFactory().getSQLExceptionConverter(),
217                     sqle,
218                     "could not advance using absolute()"
219                 );
220         }
221     }
222
223     private void prepareCurrentRow(boolean underlyingScrollSuccessful)
224     throws HibernateException {
225         
226         if (!underlyingScrollSuccessful) {
227             currentRow = null;
228             return;
229         }
230
231         Object JavaDoc result = getLoader().loadSingleRow(
232                 getResultSet(),
233                 getSession(),
234                 getQueryParameters(),
235                 false
236         );
237         if ( result != null && result.getClass().isArray() ) {
238             currentRow = (Object JavaDoc[]) result;
239         }
240         else {
241             currentRow = new Object JavaDoc[] { result };
242         }
243
244         if ( getHolderInstantiator() != null ) {
245             currentRow = new Object JavaDoc[] { getHolderInstantiator().instantiate(currentRow) };
246         }
247
248         afterScrollOperation();
249     }
250
251 }
252
Popular Tags