KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > impl > IteratorImpl


1 //$Id: IteratorImpl.java,v 1.11 2005/06/22 04:19:31 oneovthafew Exp $
2
package org.hibernate.impl;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.NoSuchElementException JavaDoc;
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 /**
21  * An implementation of <tt>java.util.Iterator</tt> that is
22  * returned by <tt>iterate()</tt> query execution methods.
23  * @author Gavin King
24  */

25 public final class IteratorImpl implements HibernateIterator {
26
27     private static final Log log = LogFactory.getLog(IteratorImpl.class);
28
29     private ResultSet JavaDoc rs;
30     private final EventSource session;
31     private final Type[] types;
32     private final boolean single;
33     private Object JavaDoc currentResult;
34     private boolean hasNext;
35     private final String JavaDoc[][] names;
36     private PreparedStatement JavaDoc ps;
37     private Object JavaDoc nextResult;
38     private HolderInstantiator holderInstantiator;
39
40     public IteratorImpl(
41             ResultSet JavaDoc rs,
42             PreparedStatement JavaDoc ps,
43             EventSource sess,
44             Type[] types,
45             String JavaDoc[][] columnNames,
46             HolderInstantiator holderInstantiator)
47     throws HibernateException, SQLException JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc[] nextResults = new Object JavaDoc[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 JavaDoc next() {
117         if ( !hasNext ) throw new NoSuchElementException JavaDoc("No more results");
118         try {
119             currentResult = nextResult;
120             postNext();
121             log.debug("returning current results");
122             return currentResult;
123         }
124         catch (SQLException JavaDoc 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 JavaDoc("Not a single column hibernate query result set");
136         }
137         if (currentResult==null) {
138             throw new IllegalStateException JavaDoc("Called Iterator.remove() before next()");
139         }
140         if ( !( types[0] instanceof EntityType ) ) {
141             throw new UnsupportedOperationException JavaDoc("Not an entity");
142         }
143         
144         session.delete(
145                 ( (EntityType) types[0] ).getAssociatedEntityName(),
146                 currentResult,
147                 false
148             );
149     }
150
151 }
152
Popular Tags