~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/impl/AbstractScrollableResults.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: AbstractScrollableResults.java 11651 2007-06-07 18:22:50Z steve.ebersole@jboss.com $
 
2
package org.hibernate.impl;
 
3
 
 
4
import java.math.BigDecimal;
 
5
import java.math.BigInteger;
 
6
import java.sql.Blob;
 
7
import java.sql.Clob;
 
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;
 
15
 
 
16
import org.apache.commons.logging.Log;
 
17
import org.apache.commons.logging.LogFactory;
 
18
 
 
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;
 
29
 
 
30
/**
 
31
 * Implementation of the <tt>ScrollableResults</tt> interface
 
32
 *
 
33
 * @author Steve Ebersole
 
34
 */
 
35
public abstract class AbstractScrollableResults implements ScrollableResults {
 
36
 
 
37
        private static final Log log = LogFactory.getLog( AbstractScrollableResults.class );
 
38
 
 
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;
 
46
 
 
47
        public AbstractScrollableResults(
 
48
                ResultSet rs,
 
49
                PreparedStatement ps,
 
50
                SessionImplementor sess,
 
51
                        Loader loader,
 
52
                        QueryParameters queryParameters,
 
53
                Type[] types,
 
54
                HolderInstantiator holderInstantiator) throws MappingException {
 
55
                this.resultSet=rs;
 
56
                this.ps=ps;
 
57
                this.session = sess;
 
58
                this.loader = loader;
 
59
                this.queryParameters = queryParameters;
 
60
                this.types = types;
 
61
                this.holderInstantiator = holderInstantiator!=null && holderInstantiator.isRequired()
 
62
                        ? holderInstantiator 
 
63
                        : null;
 
64
        }
 
65
 
 
66
        protected abstract Object[] getCurrentRow();
 
67
 
 
68
        protected ResultSet getResultSet() {
 
69
                return resultSet;
 
70
        }
 
71
 
 
72
        protected PreparedStatement getPs() {
 
73
                return ps;
 
74
        }
 
75
 
 
76
        protected SessionImplementor getSession() {
 
77
                return session;
 
78
        }
 
79
 
 
80
        protected Loader getLoader() {
 
81
                return loader;
 
82
        }
 
83
 
 
84
        protected QueryParameters getQueryParameters() {
 
85
                return queryParameters;
 
86
        }
 
87
 
 
88
        protected Type[] getTypes() {
 
89
                return types;
 
90
        }
 
91
 
 
92
        protected HolderInstantiator getHolderInstantiator() {
 
93
                return holderInstantiator;
 
94
        }
 
95
 
 
96
        public final void close() throws HibernateException {
 
97
                try {
 
98
                        // not absolutely necessary, but does help with aggressive release
 
99
                        session.getBatcher().closeQueryStatement( ps, resultSet );
 
100
                }
 
101
                catch (SQLException sqle) {
 
102
                        throw JDBCExceptionHelper.convert(
 
103
                                        session.getFactory().getSQLExceptionConverter(),
 
104
                                        sqle,
 
105
                                        "could not close results"
 
106
                                );
 
107
                }
 
108
                finally {
 
109
                        try {
 
110
                                session.getPersistenceContext().getLoadContexts().cleanup( resultSet );
 
111
                        }
 
112
                        catch( Throwable ignore ) {
 
113
                                // ignore this error for now
 
114
                                log.trace( "exception trying to cleanup load context : " + ignore.getMessage() );
 
115
                        }
 
116
                }
 
117
        }
 
118
 
 
119
        public final Object[] get() throws HibernateException {
 
120
                return getCurrentRow();
 
121
        }
 
122
 
 
123
        public final Object get(int col) throws HibernateException {
 
124
                return getCurrentRow()[col];
 
125
        }
 
126
 
 
127
        /**
 
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
 
130
         * are identical.
 
131
         *
 
132
         * @param col the column
 
133
         * @param returnType a "final" type
 
134
         */
 
135
        protected final Object getFinal(int col, Type returnType) throws HibernateException {
 
136
                if ( holderInstantiator!=null ) {
 
137
                        throw new HibernateException("query specifies a holder class");
 
138
                }
 
139
                
 
140
                if ( returnType.getReturnedClass()==types[col].getReturnedClass() ) {
 
141
                        return get(col);
 
142
                }
 
143
                else {
 
144
                        return throwInvalidColumnTypeException(col, types[col], returnType);
 
145
                }
 
146
        }
 
147
 
 
148
        /**
 
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
 
151
         * are "assignable".
 
152
         *
 
153
         * @param col the column
 
154
         * @param returnType any type
 
155
         */
 
156
        protected final Object getNonFinal(int col, Type returnType) throws HibernateException {
 
157
                if ( holderInstantiator!=null ) {
 
158
                        throw new HibernateException("query specifies a holder class");
 
159
                }
 
160
                
 
161
                if ( returnType.getReturnedClass().isAssignableFrom( types[col].getReturnedClass() ) ) {
 
162
                        return get(col);
 
163
                }
 
164
                else {
 
165
                        return throwInvalidColumnTypeException(col, types[col], returnType);
 
166
                }
 
167
        }
 
168
 
 
169
        public final BigDecimal getBigDecimal(int col) throws HibernateException {
 
170
                return (BigDecimal) getFinal(col, Hibernate.BIG_DECIMAL);
 
171
        }
 
172
 
 
173
        public final BigInteger getBigInteger(int col) throws HibernateException {
 
174
                return (BigInteger) getFinal(col, Hibernate.BIG_INTEGER);
 
175
        }
 
176
 
 
177
        public final byte[] getBinary(int col) throws HibernateException {
 
178
                return (byte[]) getFinal(col, Hibernate.BINARY);
 
179
        }
 
180
 
 
181
        public final String getText(int col) throws HibernateException {
 
182
                return (String) getFinal(col, Hibernate.TEXT);
 
183
        }
 
184
 
 
185
        public final Blob getBlob(int col) throws HibernateException {
 
186
                return (Blob) getNonFinal(col, Hibernate.BLOB);
 
187
        }
 
188
 
 
189
        public final Clob getClob(int col) throws HibernateException {
 
190
                return (Clob) getNonFinal(col, Hibernate.CLOB);
 
191
        }
 
192
 
 
193
        public final Boolean getBoolean(int col) throws HibernateException {
 
194
                return (Boolean) getFinal(col, Hibernate.BOOLEAN);
 
195
        }
 
196
 
 
197
        public final Byte getByte(int col) throws HibernateException {
 
198
                return (Byte) getFinal(col, Hibernate.BYTE);
 
199
        }
 
200
 
 
201
        public final Character getCharacter(int col) throws HibernateException {
 
202
                return (Character) getFinal(col, Hibernate.CHARACTER);
 
203
        }
 
204
 
 
205
        public final Date getDate(int col) throws HibernateException {
 
206
                return (Date) getNonFinal(col, Hibernate.TIMESTAMP);
 
207
        }
 
208
 
 
209
        public final Calendar getCalendar(int col) throws HibernateException {
 
210
                return (Calendar) getNonFinal(col, Hibernate.CALENDAR);
 
211
        }
 
212
 
 
213
        public final Double getDouble(int col) throws HibernateException {
 
214
                return (Double) getFinal(col, Hibernate.DOUBLE);
 
215
        }
 
216
 
 
217
        public final Float getFloat(int col) throws HibernateException {
 
218
                return (Float) getFinal(col, Hibernate.FLOAT);
 
219
        }
 
220
 
 
221
        public final Integer getInteger(int col) throws HibernateException {
 
222
                return (Integer) getFinal(col, Hibernate.INTEGER);
 
223
        }
 
224
 
 
225
        public final Long getLong(int col) throws HibernateException {
 
226
                return (Long) getFinal(col, Hibernate.LONG);
 
227
        }
 
228
 
 
229
        public final Short getShort(int col) throws HibernateException {
 
230
                return (Short) getFinal(col, Hibernate.SHORT);
 
231
        }
 
232
 
 
233
        public final String getString(int col) throws HibernateException {
 
234
                return (String) getFinal(col, Hibernate.STRING);
 
235
        }
 
236
 
 
237
        public final Locale getLocale(int col) throws HibernateException {
 
238
                return (Locale) getFinal(col, Hibernate.LOCALE);
 
239
        }
 
240
 
 
241
        /*public final Currency getCurrency(int col) throws HibernateException {
 
242
                return (Currency) get(col);
 
243
        }*/
 
244
 
 
245
        public final TimeZone getTimeZone(int col) throws HibernateException {
 
246
                return (TimeZone) getNonFinal(col, Hibernate.TIMEZONE);
 
247
        }
 
248
 
 
249
        public final Type getType(int i) {
 
250
                return types[i];
 
251
        }
 
252
 
 
253
        private Object throwInvalidColumnTypeException(
 
254
                int i,
 
255
                Type type,
 
256
                Type returnType) throws HibernateException {
 
257
                throw new HibernateException( 
 
258
                                "incompatible column types: " + 
 
259
                                type.getName() + 
 
260
                                ", " + 
 
261
                                returnType.getName() 
 
262
                );
 
263
        }
 
264
 
 
265
        protected void afterScrollOperation() {
 
266
                session.afterScrollOperation();
 
267
        }
 
268
}