1 package org.hibernate.impl; 3 4 import org.hibernate.HibernateException; 5 import org.hibernate.MappingException; 6 import org.hibernate.ScrollableResults; 7 import org.hibernate.engine.QueryParameters; 8 import org.hibernate.engine.SessionImplementor; 9 import org.hibernate.exception.JDBCExceptionHelper; 10 import org.hibernate.hql.HolderInstantiator; 11 import org.hibernate.loader.Loader; 12 import org.hibernate.type.Type; 13 14 import java.sql.PreparedStatement ; 15 import java.sql.ResultSet ; 16 import java.sql.SQLException ; 17 18 22 public class ScrollableResultsImpl extends AbstractScrollableResults implements ScrollableResults { 23 24 private Object [] currentRow; 25 26 public ScrollableResultsImpl( 27 ResultSet rs, 28 PreparedStatement ps, 29 SessionImplementor sess, 30 Loader loader, 31 QueryParameters queryParameters, 32 Type[] types, HolderInstantiator holderInstantiator) throws MappingException { 33 super( rs, ps, sess, loader, queryParameters, types, holderInstantiator ); 34 } 35 36 protected Object [] getCurrentRow() { 37 return currentRow; 38 } 39 40 43 public boolean scroll(int i) throws HibernateException { 44 try { 45 boolean result = getResultSet().relative(i); 46 prepareCurrentRow(result); 47 return result; 48 } 49 catch (SQLException sqle) { 50 throw JDBCExceptionHelper.convert( 51 getSession().getFactory().getSQLExceptionConverter(), 52 sqle, 53 "could not advance using scroll()" 54 ); 55 } 56 } 57 58 61 public boolean first() throws HibernateException { 62 try { 63 boolean result = getResultSet().first(); 64 prepareCurrentRow(result); 65 return result; 66 } 67 catch (SQLException sqle) { 68 throw JDBCExceptionHelper.convert( 69 getSession().getFactory().getSQLExceptionConverter(), 70 sqle, 71 "could not advance using first()" 72 ); 73 } 74 } 75 76 79 public boolean last() throws HibernateException { 80 try { 81 boolean result = getResultSet().last(); 82 prepareCurrentRow(result); 83 return result; 84 } 85 catch (SQLException sqle) { 86 throw JDBCExceptionHelper.convert( 87 getSession().getFactory().getSQLExceptionConverter(), 88 sqle, 89 "could not advance using last()" 90 ); 91 } 92 } 93 94 97 public boolean next() throws HibernateException { 98 try { 99 boolean result = getResultSet().next(); 100 prepareCurrentRow(result); 101 return result; 102 } 103 catch (SQLException sqle) { 104 throw JDBCExceptionHelper.convert( 105 getSession().getFactory().getSQLExceptionConverter(), 106 sqle, 107 "could not advance using next()" 108 ); 109 } 110 } 111 112 115 public boolean previous() throws HibernateException { 116 try { 117 boolean result = getResultSet().previous(); 118 prepareCurrentRow(result); 119 return result; 120 } 121 catch (SQLException sqle) { 122 throw JDBCExceptionHelper.convert( 123 getSession().getFactory().getSQLExceptionConverter(), 124 sqle, 125 "could not advance using previous()" 126 ); 127 } 128 } 129 130 133 public void afterLast() throws HibernateException { 134 try { 135 getResultSet().afterLast(); 136 } 137 catch (SQLException sqle) { 138 throw JDBCExceptionHelper.convert( 139 getSession().getFactory().getSQLExceptionConverter(), 140 sqle, 141 "exception calling afterLast()" 142 ); 143 } 144 } 145 146 149 public void beforeFirst() throws HibernateException { 150 try { 151 getResultSet().beforeFirst(); 152 } 153 catch (SQLException sqle) { 154 throw JDBCExceptionHelper.convert( 155 getSession().getFactory().getSQLExceptionConverter(), 156 sqle, 157 "exception calling beforeFirst()" 158 ); 159 } 160 } 161 162 165 public boolean isFirst() throws HibernateException { 166 try { 167 return getResultSet().isFirst(); 168 } 169 catch (SQLException sqle) { 170 throw JDBCExceptionHelper.convert( 171 getSession().getFactory().getSQLExceptionConverter(), 172 sqle, 173 "exception calling isFirst()" 174 ); 175 } 176 } 177 178 181 public boolean isLast() throws HibernateException { 182 try { 183 return getResultSet().isLast(); 184 } 185 catch (SQLException sqle) { 186 throw JDBCExceptionHelper.convert( 187 getSession().getFactory().getSQLExceptionConverter(), 188 sqle, 189 "exception calling isLast()" 190 ); 191 } 192 } 193 194 public int getRowNumber() throws HibernateException { 195 try { 196 return getResultSet().getRow()-1; 197 } 198 catch (SQLException sqle) { 199 throw JDBCExceptionHelper.convert( 200 getSession().getFactory().getSQLExceptionConverter(), 201 sqle, 202 "exception calling getRow()" 203 ); 204 } 205 } 206 207 public boolean setRowNumber(int rowNumber) throws HibernateException { 208 if (rowNumber>=0) rowNumber++; 209 try { 210 boolean result = getResultSet().absolute(rowNumber); 211 prepareCurrentRow(result); 212 return result; 213 } 214 catch (SQLException sqle) { 215 throw JDBCExceptionHelper.convert( 216 getSession().getFactory().getSQLExceptionConverter(), 217 sqle, 218 "could not advance using absolute()" 219 ); 220 } 221 } 222 223 private void prepareCurrentRow(boolean underlyingScrollSuccessful) 224 throws HibernateException { 225 226 if (!underlyingScrollSuccessful) { 227 currentRow = null; 228 return; 229 } 230 231 Object result = getLoader().loadSingleRow( 232 getResultSet(), 233 getSession(), 234 getQueryParameters(), 235 false 236 ); 237 if ( result != null && result.getClass().isArray() ) { 238 currentRow = (Object []) result; 239 } 240 else { 241 currentRow = new Object [] { result }; 242 } 243 244 if ( getHolderInstantiator() != null ) { 245 currentRow = new Object [] { getHolderInstantiator().instantiate(currentRow) }; 246 } 247 248 afterScrollOperation(); 249 } 250 251 } 252 | Popular Tags |