KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: AbstractScrollableResults.java,v 1.3 2005/07/14 13:12:01 steveebersole Exp $
2
package org.hibernate.impl;
3
4 import java.math.BigDecimal JavaDoc;
5 import java.math.BigInteger JavaDoc;
6 import java.sql.Blob JavaDoc;
7 import java.sql.Clob JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.util.Calendar JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.TimeZone JavaDoc;
15
16 import org.hibernate.Hibernate;
17 import org.hibernate.HibernateException;
18 import org.hibernate.MappingException;
19 import org.hibernate.ScrollableResults;
20 import org.hibernate.engine.QueryParameters;
21 import org.hibernate.engine.SessionImplementor;
22 import org.hibernate.exception.JDBCExceptionHelper;
23 import org.hibernate.hql.HolderInstantiator;
24 import org.hibernate.loader.Loader;
25 import org.hibernate.type.Type;
26
27 /**
28  * Implementation of the <tt>ScrollableResults</tt> interface
29  *
30  * @author Steve Ebersole
31  */

32 public abstract class AbstractScrollableResults implements ScrollableResults {
33
34     private final ResultSet JavaDoc resultSet;
35     private final PreparedStatement JavaDoc ps;
36     private final SessionImplementor session;
37     private final Loader loader;
38     private final QueryParameters queryParameters;
39     private final Type[] types;
40     private HolderInstantiator holderInstantiator;
41
42     public AbstractScrollableResults(
43             ResultSet JavaDoc rs,
44             PreparedStatement JavaDoc ps,
45             SessionImplementor sess,
46             Loader loader,
47             QueryParameters queryParameters,
48             Type[] types,
49             HolderInstantiator holderInstantiator) throws MappingException {
50         this.resultSet=rs;
51         this.ps=ps;
52         this.session = sess;
53         this.loader = loader;
54         this.queryParameters = queryParameters;
55         this.types = types;
56         this.holderInstantiator = holderInstantiator!=null && holderInstantiator.isRequired()
57                 ? holderInstantiator
58                 : null;
59     }
60
61     protected abstract Object JavaDoc[] getCurrentRow();
62
63     protected ResultSet JavaDoc getResultSet() {
64         return resultSet;
65     }
66
67     protected PreparedStatement JavaDoc getPs() {
68         return ps;
69     }
70
71     protected SessionImplementor getSession() {
72         return session;
73     }
74
75     protected Loader getLoader() {
76         return loader;
77     }
78
79     protected QueryParameters getQueryParameters() {
80         return queryParameters;
81     }
82
83     protected Type[] getTypes() {
84         return types;
85     }
86
87     protected HolderInstantiator getHolderInstantiator() {
88         return holderInstantiator;
89     }
90
91     public final void close() throws HibernateException {
92         try {
93             // not absolutely necessary, but does help with aggressive release
94
session.getBatcher().closeQueryStatement(ps, resultSet);
95         }
96         catch (SQLException JavaDoc sqle) {
97             throw JDBCExceptionHelper.convert(
98                     session.getFactory().getSQLExceptionConverter(),
99                     sqle,
100                     "could not close results"
101                 );
102         }
103     }
104
105     public final Object JavaDoc[] get() throws HibernateException {
106         return getCurrentRow();
107     }
108
109     public final Object JavaDoc get(int col) throws HibernateException {
110         return getCurrentRow()[col];
111     }
112
113     /**
114      * Check that the requested type is compatible with the result type, and
115      * return the column value. This version makes sure the the classes
116      * are identical.
117      *
118      * @param col the column
119      * @param returnType a "final" type
120      */

121     protected final Object JavaDoc getFinal(int col, Type returnType) throws HibernateException {
122         if ( holderInstantiator!=null ) {
123             throw new HibernateException("query specifies a holder class");
124         }
125         
126         if ( returnType.getReturnedClass()==types[col].getReturnedClass() ) {
127             return get(col);
128         }
129         else {
130             return throwInvalidColumnTypeException(col, types[col], returnType);
131         }
132     }
133
134     /**
135      * Check that the requested type is compatible with the result type, and
136      * return the column value. This version makes sure the the classes
137      * are "assignable".
138      *
139      * @param col the column
140      * @param returnType any type
141      */

142     protected final Object JavaDoc getNonFinal(int col, Type returnType) throws HibernateException {
143         if ( holderInstantiator!=null ) {
144             throw new HibernateException("query specifies a holder class");
145         }
146         
147         if ( returnType.getReturnedClass().isAssignableFrom( types[col].getReturnedClass() ) ) {
148             return get(col);
149         }
150         else {
151             return throwInvalidColumnTypeException(col, types[col], returnType);
152         }
153     }
154
155     public final BigDecimal JavaDoc getBigDecimal(int col) throws HibernateException {
156         return (BigDecimal JavaDoc) getFinal(col, Hibernate.BIG_DECIMAL);
157     }
158
159     public final BigInteger JavaDoc getBigInteger(int col) throws HibernateException {
160         return (BigInteger JavaDoc) getFinal(col, Hibernate.BIG_INTEGER);
161     }
162
163     public final byte[] getBinary(int col) throws HibernateException {
164         return (byte[]) getFinal(col, Hibernate.BINARY);
165     }
166
167     public final String JavaDoc getText(int col) throws HibernateException {
168         return (String JavaDoc) getFinal(col, Hibernate.TEXT);
169     }
170
171     public final Blob JavaDoc getBlob(int col) throws HibernateException {
172         return (Blob JavaDoc) getNonFinal(col, Hibernate.BLOB);
173     }
174
175     public final Clob JavaDoc getClob(int col) throws HibernateException {
176         return (Clob JavaDoc) getNonFinal(col, Hibernate.CLOB);
177     }
178
179     public final Boolean JavaDoc getBoolean(int col) throws HibernateException {
180         return (Boolean JavaDoc) getFinal(col, Hibernate.BOOLEAN);
181     }
182
183     public final Byte JavaDoc getByte(int col) throws HibernateException {
184         return (Byte JavaDoc) getFinal(col, Hibernate.BYTE);
185     }
186
187     public final Character JavaDoc getCharacter(int col) throws HibernateException {
188         return (Character JavaDoc) getFinal(col, Hibernate.CHARACTER);
189     }
190
191     public final Date JavaDoc getDate(int col) throws HibernateException {
192         return (Date JavaDoc) getNonFinal(col, Hibernate.TIMESTAMP);
193     }
194
195     public final Calendar JavaDoc getCalendar(int col) throws HibernateException {
196         return (Calendar JavaDoc) getNonFinal(col, Hibernate.CALENDAR);
197     }
198
199     public final Double JavaDoc getDouble(int col) throws HibernateException {
200         return (Double JavaDoc) getFinal(col, Hibernate.DOUBLE);
201     }
202
203     public final Float JavaDoc getFloat(int col) throws HibernateException {
204         return (Float JavaDoc) getFinal(col, Hibernate.FLOAT);
205     }
206
207     public final Integer JavaDoc getInteger(int col) throws HibernateException {
208         return (Integer JavaDoc) getFinal(col, Hibernate.INTEGER);
209     }
210
211     public final Long JavaDoc getLong(int col) throws HibernateException {
212         return (Long JavaDoc) getFinal(col, Hibernate.LONG);
213     }
214
215     public final Short JavaDoc getShort(int col) throws HibernateException {
216         return (Short JavaDoc) getFinal(col, Hibernate.SHORT);
217     }
218
219     public final String JavaDoc getString(int col) throws HibernateException {
220         return (String JavaDoc) getFinal(col, Hibernate.STRING);
221     }
222
223     public final Locale JavaDoc getLocale(int col) throws HibernateException {
224         return (Locale JavaDoc) getFinal(col, Hibernate.LOCALE);
225     }
226
227     /*public final Currency getCurrency(int col) throws HibernateException {
228         return (Currency) get(col);
229     }*/

230
231     public final TimeZone JavaDoc getTimeZone(int col) throws HibernateException {
232         return (TimeZone JavaDoc) getNonFinal(col, Hibernate.TIMEZONE);
233     }
234
235     public final Type getType(int i) {
236         return types[i];
237     }
238
239     private Object JavaDoc throwInvalidColumnTypeException(
240             int i,
241             Type type,
242             Type returnType) throws HibernateException {
243         throw new HibernateException(
244                 "incompatible column types: " +
245                 type.getName() +
246                 ", " +
247                 returnType.getName()
248         );
249     }
250
251     protected void afterScrollOperation() {
252         session.afterScrollOperation();
253     }
254 }
255
Popular Tags