1 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.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 32 public abstract class AbstractScrollableResults implements ScrollableResults { 33 34 private final ResultSet resultSet; 35 private final PreparedStatement 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 rs, 44 PreparedStatement 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 [] getCurrentRow(); 62 63 protected ResultSet getResultSet() { 64 return resultSet; 65 } 66 67 protected PreparedStatement 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 session.getBatcher().closeQueryStatement(ps, resultSet); 95 } 96 catch (SQLException sqle) { 97 throw JDBCExceptionHelper.convert( 98 session.getFactory().getSQLExceptionConverter(), 99 sqle, 100 "could not close results" 101 ); 102 } 103 } 104 105 public final Object [] get() throws HibernateException { 106 return getCurrentRow(); 107 } 108 109 public final Object get(int col) throws HibernateException { 110 return getCurrentRow()[col]; 111 } 112 113 121 protected final Object 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 142 protected final Object 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 getBigDecimal(int col) throws HibernateException { 156 return (BigDecimal ) getFinal(col, Hibernate.BIG_DECIMAL); 157 } 158 159 public final BigInteger getBigInteger(int col) throws HibernateException { 160 return (BigInteger ) 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 getText(int col) throws HibernateException { 168 return (String ) getFinal(col, Hibernate.TEXT); 169 } 170 171 public final Blob getBlob(int col) throws HibernateException { 172 return (Blob ) getNonFinal(col, Hibernate.BLOB); 173 } 174 175 public final Clob getClob(int col) throws HibernateException { 176 return (Clob ) getNonFinal(col, Hibernate.CLOB); 177 } 178 179 public final Boolean getBoolean(int col) throws HibernateException { 180 return (Boolean ) getFinal(col, Hibernate.BOOLEAN); 181 } 182 183 public final Byte getByte(int col) throws HibernateException { 184 return (Byte ) getFinal(col, Hibernate.BYTE); 185 } 186 187 public final Character getCharacter(int col) throws HibernateException { 188 return (Character ) getFinal(col, Hibernate.CHARACTER); 189 } 190 191 public final Date getDate(int col) throws HibernateException { 192 return (Date ) getNonFinal(col, Hibernate.TIMESTAMP); 193 } 194 195 public final Calendar getCalendar(int col) throws HibernateException { 196 return (Calendar ) getNonFinal(col, Hibernate.CALENDAR); 197 } 198 199 public final Double getDouble(int col) throws HibernateException { 200 return (Double ) getFinal(col, Hibernate.DOUBLE); 201 } 202 203 public final Float getFloat(int col) throws HibernateException { 204 return (Float ) getFinal(col, Hibernate.FLOAT); 205 } 206 207 public final Integer getInteger(int col) throws HibernateException { 208 return (Integer ) getFinal(col, Hibernate.INTEGER); 209 } 210 211 public final Long getLong(int col) throws HibernateException { 212 return (Long ) getFinal(col, Hibernate.LONG); 213 } 214 215 public final Short getShort(int col) throws HibernateException { 216 return (Short ) getFinal(col, Hibernate.SHORT); 217 } 218 219 public final String getString(int col) throws HibernateException { 220 return (String ) getFinal(col, Hibernate.STRING); 221 } 222 223 public final Locale getLocale(int col) throws HibernateException { 224 return (Locale ) getFinal(col, Hibernate.LOCALE); 225 } 226 227 230 231 public final TimeZone getTimeZone(int col) throws HibernateException { 232 return (TimeZone ) getNonFinal(col, Hibernate.TIMEZONE); 233 } 234 235 public final Type getType(int i) { 236 return types[i]; 237 } 238 239 private Object 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 |