1 package org.hibernate.impl; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.NoSuchElementException ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.HibernateException; 12 import org.hibernate.JDBCException; 13 import org.hibernate.engine.HibernateIterator; 14 import org.hibernate.event.EventSource; 15 import org.hibernate.exception.JDBCExceptionHelper; 16 import org.hibernate.hql.HolderInstantiator; 17 import org.hibernate.type.EntityType; 18 import org.hibernate.type.Type; 19 20 25 public final class IteratorImpl implements HibernateIterator { 26 27 private static final Log log = LogFactory.getLog(IteratorImpl.class); 28 29 private ResultSet rs; 30 private final EventSource session; 31 private final Type[] types; 32 private final boolean single; 33 private Object currentResult; 34 private boolean hasNext; 35 private final String [][] names; 36 private PreparedStatement ps; 37 private Object nextResult; 38 private HolderInstantiator holderInstantiator; 39 40 public IteratorImpl( 41 ResultSet rs, 42 PreparedStatement ps, 43 EventSource sess, 44 Type[] types, 45 String [][] columnNames, 46 HolderInstantiator holderInstantiator) 47 throws HibernateException, SQLException { 48 49 this.rs=rs; 50 this.ps=ps; 51 this.session = sess; 52 this.types = types; 53 this.names = columnNames; 54 this.holderInstantiator = holderInstantiator; 55 56 single = types.length==1; 57 58 postNext(); 59 } 60 61 public void close() throws JDBCException { 62 if (ps!=null) { 63 try { 64 log.debug("closing iterator"); 65 nextResult = null; 66 session.getBatcher().closeQueryStatement(ps, rs); 67 ps = null; 68 rs = null; 69 hasNext = false; 70 } 71 catch (SQLException e) { 72 log.info( "Unable to close iterator", e ); 73 throw JDBCExceptionHelper.convert( 74 session.getFactory().getSQLExceptionConverter(), 75 e, 76 "Unable to close iterator" 77 ); 78 } 79 } 80 } 81 82 private void postNext() throws HibernateException, SQLException { 83 this.hasNext = rs.next(); 84 if (!hasNext) { 85 log.debug("exhausted results"); 86 close(); 87 } 88 else { 89 log.debug("retrieving next results"); 90 boolean isHolder = holderInstantiator.isRequired(); 91 92 if ( single && !isHolder ) { 93 nextResult = types[0].nullSafeGet( rs, names[0], session, null ); 94 } 95 else { 96 Object [] nextResults = new Object [types.length]; 97 for (int i=0; i<types.length; i++) { 98 nextResults[i] = types[i].nullSafeGet( rs, names[i], session, null ); 99 } 100 101 if (isHolder) { 102 nextResult = holderInstantiator.instantiate(nextResults); 103 } 104 else { 105 nextResult = nextResults; 106 } 107 } 108 109 } 110 } 111 112 public boolean hasNext() { 113 return hasNext; 114 } 115 116 public Object next() { 117 if ( !hasNext ) throw new NoSuchElementException ("No more results"); 118 try { 119 currentResult = nextResult; 120 postNext(); 121 log.debug("returning current results"); 122 return currentResult; 123 } 124 catch (SQLException sqle) { 125 throw JDBCExceptionHelper.convert( 126 session.getFactory().getSQLExceptionConverter(), 127 sqle, 128 "could not get next iterator result" 129 ); 130 } 131 } 132 133 public void remove() { 134 if (!single) { 135 throw new UnsupportedOperationException ("Not a single column hibernate query result set"); 136 } 137 if (currentResult==null) { 138 throw new IllegalStateException ("Called Iterator.remove() before next()"); 139 } 140 if ( !( types[0] instanceof EntityType ) ) { 141 throw new UnsupportedOperationException ("Not an entity"); 142 } 143 144 session.delete( 145 ( (EntityType) types[0] ).getAssociatedEntityName(), 146 currentResult, 147 false 148 ); 149 } 150 151 } 152 | Popular Tags |