iteratorimpl.java
来自「用Java实现的23个常用设计模式源代码」· Java 代码 · 共 122 行
JAVA
122 行
//$Id: IteratorImpl.java,v 1.5.2.3 2003/11/27 16:09:59 oneovthafew Exp $package net.sf.hibernate.impl;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.NoSuchElementException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.hibernate.HibernateException;import net.sf.hibernate.LazyInitializationException;import net.sf.hibernate.engine.HibernateIterator;import net.sf.hibernate.engine.SessionImplementor;import net.sf.hibernate.type.Type;/** * An implementation of <tt>java.util.Iterator</tt> that is * returned by <tt>iterate()</tt> query execution methods. * @author Gavin King */public final class IteratorImpl implements HibernateIterator { private static final Log log = LogFactory.getLog(IteratorImpl.class); private ResultSet rs; private final SessionImplementor sess; private final Type[] types; private final boolean single; private Object[] nextResults; private Object currentResult; private boolean hasNext; private final String[][] names; private PreparedStatement ps; private Object nextResult; public IteratorImpl(ResultSet rs, PreparedStatement ps, SessionImplementor sess, Type[] types, String[][] columnNames) throws HibernateException, SQLException { this.rs=rs; this.ps=ps; this.sess = sess; this.types = types; this.names = columnNames; single = types.length==1; postNext( rs.next() ); } public void close() throws SQLException { if (ps!=null) { log.debug("closing iterator"); nextResults = null; sess.getBatcher().closeQueryStatement(ps, rs); ps = null; rs = null; hasNext = false; } } private void postNext(boolean hasNext) throws HibernateException, SQLException { this.hasNext = hasNext; if (!hasNext) { log.debug("exhausted results"); close(); } else { log.debug("retrieving next results"); if (single) { nextResult = types[0].nullSafeGet( rs, names[0], sess, null ); } else { nextResults = new Object[types.length]; for (int i=0; i<types.length; i++) { nextResults[i] = types[i].nullSafeGet( rs, names[i], sess, null ); } nextResult = nextResults; } } } public boolean hasNext() { return hasNext; } public Object next() { if ( nextResult==null ) throw new NoSuchElementException("No more results"); try { currentResult = nextResult; postNext( rs.next() ); log.debug("returning current results"); return currentResult; } catch (Exception sqle) { log.error("could not get next result", sqle); throw new LazyInitializationException(sqle); } } public void remove() { if (!single) throw new UnsupportedOperationException("Not a single column hibernate query result set"); if (currentResult==null) throw new IllegalStateException("Called Iterator.remove() before next()"); try { sess.delete(currentResult); } catch (Exception sqle) { log.error("could not remove", sqle); throw new LazyInitializationException(sqle); } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?