1
//$Id: AbstractScrollableResults.java 11651 2007-06-07 18:22:50Z steve.ebersole@jboss.com $
2
package org.hibernate.impl;
4
import java.math.BigDecimal;
5
import java.math.BigInteger;
8
import java.sql.PreparedStatement;
9
import java.sql.ResultSet;
10
import java.sql.SQLException;
11
import java.util.Calendar;
12
import java.util.Date;
13
import java.util.Locale;
14
import java.util.TimeZone;
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
19
import org.hibernate.Hibernate;
20
import org.hibernate.HibernateException;
21
import org.hibernate.MappingException;
22
import org.hibernate.ScrollableResults;
23
import org.hibernate.engine.QueryParameters;
24
import org.hibernate.engine.SessionImplementor;
25
import org.hibernate.exception.JDBCExceptionHelper;
26
import org.hibernate.hql.HolderInstantiator;
27
import org.hibernate.loader.Loader;
28
import org.hibernate.type.Type;
31
* Implementation of the <tt>ScrollableResults</tt> interface
33
* @author Steve Ebersole
35
public abstract class AbstractScrollableResults implements ScrollableResults {
37
private static final Log log = LogFactory.getLog( AbstractScrollableResults.class );
39
private final ResultSet resultSet;
40
private final PreparedStatement ps;
41
private final SessionImplementor session;
42
private final Loader loader;
43
private final QueryParameters queryParameters;
44
private final Type[] types;
45
private HolderInstantiator holderInstantiator;
47
public AbstractScrollableResults(
50
SessionImplementor sess,
52
QueryParameters queryParameters,
54
HolderInstantiator holderInstantiator) throws MappingException {
59
this.queryParameters = queryParameters;
61
this.holderInstantiator = holderInstantiator!=null && holderInstantiator.isRequired()
66
protected abstract Object[] getCurrentRow();
68
protected ResultSet getResultSet() {
72
protected PreparedStatement getPs() {
76
protected SessionImplementor getSession() {
80
protected Loader getLoader() {
84
protected QueryParameters getQueryParameters() {
85
return queryParameters;
88
protected Type[] getTypes() {
92
protected HolderInstantiator getHolderInstantiator() {
93
return holderInstantiator;
96
public final void close() throws HibernateException {
98
// not absolutely necessary, but does help with aggressive release
99
session.getBatcher().closeQueryStatement( ps, resultSet );
101
catch (SQLException sqle) {
102
throw JDBCExceptionHelper.convert(
103
session.getFactory().getSQLExceptionConverter(),
105
"could not close results"
110
session.getPersistenceContext().getLoadContexts().cleanup( resultSet );
112
catch( Throwable ignore ) {
113
// ignore this error for now
114
log.trace( "exception trying to cleanup load context : " + ignore.getMessage() );
119
public final Object[] get() throws HibernateException {
120
return getCurrentRow();
123
public final Object get(int col) throws HibernateException {
124
return getCurrentRow()[col];
128
* Check that the requested type is compatible with the result type, and
129
* return the column value. This version makes sure the the classes
132
* @param col the column
133
* @param returnType a "final" type
135
protected final Object getFinal(int col, Type returnType) throws HibernateException {
136
if ( holderInstantiator!=null ) {
137
throw new HibernateException("query specifies a holder class");
140
if ( returnType.getReturnedClass()==types[col].getReturnedClass() ) {
144
return throwInvalidColumnTypeException(col, types[col], returnType);
149
* Check that the requested type is compatible with the result type, and
150
* return the column value. This version makes sure the the classes
153
* @param col the column
154
* @param returnType any type
156
protected final Object getNonFinal(int col, Type returnType) throws HibernateException {
157
if ( holderInstantiator!=null ) {
158
throw new HibernateException("query specifies a holder class");
161
if ( returnType.getReturnedClass().isAssignableFrom( types[col].getReturnedClass() ) ) {
165
return throwInvalidColumnTypeException(col, types[col], returnType);
169
public final BigDecimal getBigDecimal(int col) throws HibernateException {
170
return (BigDecimal) getFinal(col, Hibernate.BIG_DECIMAL);
173
public final BigInteger getBigInteger(int col) throws HibernateException {
174
return (BigInteger) getFinal(col, Hibernate.BIG_INTEGER);
177
public final byte[] getBinary(int col) throws HibernateException {
178
return (byte[]) getFinal(col, Hibernate.BINARY);
181
public final String getText(int col) throws HibernateException {
182
return (String) getFinal(col, Hibernate.TEXT);
185
public final Blob getBlob(int col) throws HibernateException {
186
return (Blob) getNonFinal(col, Hibernate.BLOB);
189
public final Clob getClob(int col) throws HibernateException {
190
return (Clob) getNonFinal(col, Hibernate.CLOB);
193
public final Boolean getBoolean(int col) throws HibernateException {
194
return (Boolean) getFinal(col, Hibernate.BOOLEAN);
197
public final Byte getByte(int col) throws HibernateException {
198
return (Byte) getFinal(col, Hibernate.BYTE);
201
public final Character getCharacter(int col) throws HibernateException {
202
return (Character) getFinal(col, Hibernate.CHARACTER);
205
public final Date getDate(int col) throws HibernateException {
206
return (Date) getNonFinal(col, Hibernate.TIMESTAMP);
209
public final Calendar getCalendar(int col) throws HibernateException {
210
return (Calendar) getNonFinal(col, Hibernate.CALENDAR);
213
public final Double getDouble(int col) throws HibernateException {
214
return (Double) getFinal(col, Hibernate.DOUBLE);
217
public final Float getFloat(int col) throws HibernateException {
218
return (Float) getFinal(col, Hibernate.FLOAT);
221
public final Integer getInteger(int col) throws HibernateException {
222
return (Integer) getFinal(col, Hibernate.INTEGER);
225
public final Long getLong(int col) throws HibernateException {
226
return (Long) getFinal(col, Hibernate.LONG);
229
public final Short getShort(int col) throws HibernateException {
230
return (Short) getFinal(col, Hibernate.SHORT);
233
public final String getString(int col) throws HibernateException {
234
return (String) getFinal(col, Hibernate.STRING);
237
public final Locale getLocale(int col) throws HibernateException {
238
return (Locale) getFinal(col, Hibernate.LOCALE);
241
/*public final Currency getCurrency(int col) throws HibernateException {
242
return (Currency) get(col);
245
public final TimeZone getTimeZone(int col) throws HibernateException {
246
return (TimeZone) getNonFinal(col, Hibernate.TIMEZONE);
249
public final Type getType(int i) {
253
private Object throwInvalidColumnTypeException(
256
Type returnType) throws HibernateException {
257
throw new HibernateException(
258
"incompatible column types: " +
265
protected void afterScrollOperation() {
266
session.afterScrollOperation();