001: //$Id: AbstractScrollableResults.java 7469 2005-07-14 13:12:19Z steveebersole $
002: package org.hibernate.impl;
003:
004: import java.math.BigDecimal;
005: import java.math.BigInteger;
006: import java.sql.Blob;
007: import java.sql.Clob;
008: import java.sql.PreparedStatement;
009: import java.sql.ResultSet;
010: import java.sql.SQLException;
011: import java.util.Calendar;
012: import java.util.Date;
013: import java.util.Locale;
014: import java.util.TimeZone;
015:
016: import org.hibernate.Hibernate;
017: import org.hibernate.HibernateException;
018: import org.hibernate.MappingException;
019: import org.hibernate.ScrollableResults;
020: import org.hibernate.engine.QueryParameters;
021: import org.hibernate.engine.SessionImplementor;
022: import org.hibernate.exception.JDBCExceptionHelper;
023: import org.hibernate.hql.HolderInstantiator;
024: import org.hibernate.loader.Loader;
025: import org.hibernate.type.Type;
026:
027: /**
028: * Implementation of the <tt>ScrollableResults</tt> interface
029: *
030: * @author Steve Ebersole
031: */
032: public abstract class AbstractScrollableResults implements
033: ScrollableResults {
034:
035: private final ResultSet resultSet;
036: private final PreparedStatement ps;
037: private final SessionImplementor session;
038: private final Loader loader;
039: private final QueryParameters queryParameters;
040: private final Type[] types;
041: private HolderInstantiator holderInstantiator;
042:
043: public AbstractScrollableResults(ResultSet rs,
044: PreparedStatement ps, SessionImplementor sess,
045: Loader loader, QueryParameters queryParameters,
046: Type[] types, HolderInstantiator holderInstantiator)
047: throws MappingException {
048: this .resultSet = rs;
049: this .ps = ps;
050: this .session = sess;
051: this .loader = loader;
052: this .queryParameters = queryParameters;
053: this .types = types;
054: this .holderInstantiator = holderInstantiator != null
055: && holderInstantiator.isRequired() ? holderInstantiator
056: : null;
057: }
058:
059: protected abstract Object[] getCurrentRow();
060:
061: protected ResultSet getResultSet() {
062: return resultSet;
063: }
064:
065: protected PreparedStatement getPs() {
066: return ps;
067: }
068:
069: protected SessionImplementor getSession() {
070: return session;
071: }
072:
073: protected Loader getLoader() {
074: return loader;
075: }
076:
077: protected QueryParameters getQueryParameters() {
078: return queryParameters;
079: }
080:
081: protected Type[] getTypes() {
082: return types;
083: }
084:
085: protected HolderInstantiator getHolderInstantiator() {
086: return holderInstantiator;
087: }
088:
089: public final void close() throws HibernateException {
090: try {
091: // not absolutely necessary, but does help with aggressive release
092: session.getBatcher().closeQueryStatement(ps, resultSet);
093: } catch (SQLException sqle) {
094: throw JDBCExceptionHelper.convert(session.getFactory()
095: .getSQLExceptionConverter(), sqle,
096: "could not close results");
097: }
098: }
099:
100: public final Object[] get() throws HibernateException {
101: return getCurrentRow();
102: }
103:
104: public final Object get(int col) throws HibernateException {
105: return getCurrentRow()[col];
106: }
107:
108: /**
109: * Check that the requested type is compatible with the result type, and
110: * return the column value. This version makes sure the the classes
111: * are identical.
112: *
113: * @param col the column
114: * @param returnType a "final" type
115: */
116: protected final Object getFinal(int col, Type returnType)
117: throws HibernateException {
118: if (holderInstantiator != null) {
119: throw new HibernateException(
120: "query specifies a holder class");
121: }
122:
123: if (returnType.getReturnedClass() == types[col]
124: .getReturnedClass()) {
125: return get(col);
126: } else {
127: return throwInvalidColumnTypeException(col, types[col],
128: returnType);
129: }
130: }
131:
132: /**
133: * Check that the requested type is compatible with the result type, and
134: * return the column value. This version makes sure the the classes
135: * are "assignable".
136: *
137: * @param col the column
138: * @param returnType any type
139: */
140: protected final Object getNonFinal(int col, Type returnType)
141: throws HibernateException {
142: if (holderInstantiator != null) {
143: throw new HibernateException(
144: "query specifies a holder class");
145: }
146:
147: if (returnType.getReturnedClass().isAssignableFrom(
148: types[col].getReturnedClass())) {
149: return get(col);
150: } else {
151: return throwInvalidColumnTypeException(col, types[col],
152: returnType);
153: }
154: }
155:
156: public final BigDecimal getBigDecimal(int col)
157: throws HibernateException {
158: return (BigDecimal) getFinal(col, Hibernate.BIG_DECIMAL);
159: }
160:
161: public final BigInteger getBigInteger(int col)
162: throws HibernateException {
163: return (BigInteger) getFinal(col, Hibernate.BIG_INTEGER);
164: }
165:
166: public final byte[] getBinary(int col) throws HibernateException {
167: return (byte[]) getFinal(col, Hibernate.BINARY);
168: }
169:
170: public final String getText(int col) throws HibernateException {
171: return (String) getFinal(col, Hibernate.TEXT);
172: }
173:
174: public final Blob getBlob(int col) throws HibernateException {
175: return (Blob) getNonFinal(col, Hibernate.BLOB);
176: }
177:
178: public final Clob getClob(int col) throws HibernateException {
179: return (Clob) getNonFinal(col, Hibernate.CLOB);
180: }
181:
182: public final Boolean getBoolean(int col) throws HibernateException {
183: return (Boolean) getFinal(col, Hibernate.BOOLEAN);
184: }
185:
186: public final Byte getByte(int col) throws HibernateException {
187: return (Byte) getFinal(col, Hibernate.BYTE);
188: }
189:
190: public final Character getCharacter(int col)
191: throws HibernateException {
192: return (Character) getFinal(col, Hibernate.CHARACTER);
193: }
194:
195: public final Date getDate(int col) throws HibernateException {
196: return (Date) getNonFinal(col, Hibernate.TIMESTAMP);
197: }
198:
199: public final Calendar getCalendar(int col)
200: throws HibernateException {
201: return (Calendar) getNonFinal(col, Hibernate.CALENDAR);
202: }
203:
204: public final Double getDouble(int col) throws HibernateException {
205: return (Double) getFinal(col, Hibernate.DOUBLE);
206: }
207:
208: public final Float getFloat(int col) throws HibernateException {
209: return (Float) getFinal(col, Hibernate.FLOAT);
210: }
211:
212: public final Integer getInteger(int col) throws HibernateException {
213: return (Integer) getFinal(col, Hibernate.INTEGER);
214: }
215:
216: public final Long getLong(int col) throws HibernateException {
217: return (Long) getFinal(col, Hibernate.LONG);
218: }
219:
220: public final Short getShort(int col) throws HibernateException {
221: return (Short) getFinal(col, Hibernate.SHORT);
222: }
223:
224: public final String getString(int col) throws HibernateException {
225: return (String) getFinal(col, Hibernate.STRING);
226: }
227:
228: public final Locale getLocale(int col) throws HibernateException {
229: return (Locale) getFinal(col, Hibernate.LOCALE);
230: }
231:
232: /*public final Currency getCurrency(int col) throws HibernateException {
233: return (Currency) get(col);
234: }*/
235:
236: public final TimeZone getTimeZone(int col)
237: throws HibernateException {
238: return (TimeZone) getNonFinal(col, Hibernate.TIMEZONE);
239: }
240:
241: public final Type getType(int i) {
242: return types[i];
243: }
244:
245: private Object throwInvalidColumnTypeException(int i, Type type,
246: Type returnType) throws HibernateException {
247: throw new HibernateException("incompatible column types: "
248: + type.getName() + ", " + returnType.getName());
249: }
250:
251: protected void afterScrollOperation() {
252: session.afterScrollOperation();
253: }
254: }
|