1 package org.hibernate.impl; 3 4 import org.hibernate.HibernateException; 5 import org.hibernate.MappingException; 6 import org.hibernate.exception.JDBCExceptionHelper; 7 import org.hibernate.hql.HolderInstantiator; 8 import org.hibernate.type.Type; 9 import org.hibernate.loader.Loader; 10 import org.hibernate.engine.SessionImplementor; 11 import org.hibernate.engine.QueryParameters; 12 13 import java.sql.ResultSet ; 14 import java.sql.PreparedStatement ; 15 import java.sql.SQLException ; 16 17 22 public class FetchingScrollableResultsImpl extends AbstractScrollableResults { 23 24 public FetchingScrollableResultsImpl( 25 ResultSet rs, 26 PreparedStatement ps, 27 SessionImplementor sess, 28 Loader loader, 29 QueryParameters queryParameters, 30 Type[] types, 31 HolderInstantiator holderInstantiator) throws MappingException { 32 super( rs, ps, sess, loader, queryParameters, types, holderInstantiator ); 33 } 34 35 private Object [] currentRow = null; 36 private int currentPosition = 0; 37 private Integer maxPosition = null; 38 39 protected Object [] getCurrentRow() { 40 return currentRow; 41 } 42 43 48 public boolean next() throws HibernateException { 49 if ( maxPosition != null && maxPosition.intValue() <= currentPosition ) { 50 currentRow = null; 51 currentPosition = maxPosition.intValue() + 1; 52 return false; 53 } 54 55 Object row = getLoader().loadSequentialRowsForward( 56 getResultSet(), 57 getSession(), 58 getQueryParameters(), 59 false 60 ); 61 62 63 boolean afterLast; 64 try { 65 afterLast = getResultSet().isAfterLast(); 66 } 67 catch( SQLException e ) { 68 throw JDBCExceptionHelper.convert( 69 getSession().getFactory().getSQLExceptionConverter(), 70 e, 71 "exception calling isAfterLast()" 72 ); 73 } 74 75 currentPosition++; 76 currentRow = new Object [] { row }; 77 78 if ( afterLast ) { 79 if ( maxPosition == null ) { 80 maxPosition = new Integer ( currentPosition ); 82 } 83 } 84 85 afterScrollOperation(); 86 87 return true; 88 } 89 90 95 public boolean previous() throws HibernateException { 96 if ( currentPosition <= 1 ) { 97 currentPosition = 0; 98 currentRow = null; 99 return false; 100 } 101 102 Object loadResult = getLoader().loadSequentialRowsReverse( 103 getResultSet(), 104 getSession(), 105 getQueryParameters(), 106 false, 107 ( maxPosition != null && currentPosition > maxPosition.intValue() ) 108 ); 109 110 currentRow = new Object [] { loadResult }; 111 currentPosition--; 112 113 afterScrollOperation(); 114 115 return true; 116 117 } 118 119 126 public boolean scroll(int positions) throws HibernateException { 127 boolean more = false; 128 if ( positions > 0 ) { 129 for ( int i = 0; i < positions; i++ ) { 131 more = next(); 132 if ( !more ) { 133 break; 134 } 135 } 136 } 137 else if ( positions < 0 ) { 138 for ( int i = 0; i < ( 0 - positions ); i++ ) { 140 more = previous(); 141 if ( !more ) { 142 break; 143 } 144 } 145 } 146 else { 147 throw new HibernateException( "scroll(0) not valid" ); 148 } 149 150 afterScrollOperation(); 151 152 return more; 153 } 154 155 160 public boolean last() throws HibernateException { 161 boolean more = false; 162 if ( maxPosition != null ) { 163 for ( int i = currentPosition; i < maxPosition.intValue(); i++ ) { 164 more = next(); 165 } 166 } 167 else { 168 try { 169 if ( getResultSet().isAfterLast() ) { 170 return false; 173 } 174 175 while ( !getResultSet().isAfterLast() ) { 176 more = next(); 177 } 178 } 179 catch( SQLException e ) { 180 throw JDBCExceptionHelper.convert( 181 getSession().getFactory().getSQLExceptionConverter(), 182 e, 183 "exception calling isAfterLast()" 184 ); 185 } 186 } 187 188 afterScrollOperation(); 189 190 return more; 191 } 192 193 198 public boolean first() throws HibernateException { 199 beforeFirst(); 200 boolean more = next(); 201 202 afterScrollOperation(); 203 204 return more; 205 } 206 207 210 public void beforeFirst() throws HibernateException { 211 try { 212 getResultSet().beforeFirst(); 213 } 214 catch( SQLException e ) { 215 throw JDBCExceptionHelper.convert( 216 getSession().getFactory().getSQLExceptionConverter(), 217 e, 218 "exception calling beforeFirst()" 219 ); 220 } 221 currentRow = null; 222 currentPosition = 0; 223 } 224 225 228 public void afterLast() throws HibernateException { 229 last(); 232 next(); 233 afterScrollOperation(); 234 } 235 236 243 public boolean isFirst() throws HibernateException { 244 return currentPosition == 1; 245 } 246 247 254 public boolean isLast() throws HibernateException { 255 if ( maxPosition == null ) { 256 return false; 258 } 259 else { 260 return currentPosition == maxPosition.intValue(); 261 } 262 } 263 264 269 public int getRowNumber() throws HibernateException { 270 return currentPosition; 271 } 272 273 281 public boolean setRowNumber(int rowNumber) throws HibernateException { 282 if ( rowNumber == 1 ) { 283 return first(); 284 } 285 else if ( rowNumber == -1 ) { 286 return last(); 287 } 288 else if ( maxPosition != null && rowNumber == maxPosition.intValue() ) { 289 return last(); 290 } 291 return scroll( rowNumber - currentPosition ); 292 } 293 } 294 | Popular Tags |