1 3 package net.sf.persist; 4 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.Iterator ; 8 9 12 public final class ResultSetIterator implements Iterator { 13 14 public static final int TYPE_OBJECT = 1; 15 public static final int TYPE_MAP = 2; 16 17 private final ResultSet resultSet; 18 private final Persist persist; 19 private final Class objectClass; 20 private final int type; 21 private boolean hasNext = false; 22 23 public ResultSetIterator(final Persist persist, final Class objectClass, final ResultSet resultSet, final int type) { 24 25 if (type != TYPE_OBJECT && type != TYPE_MAP) { 26 throw new RuntimeSQLException("Invalid ResultSetIterator type: " + type); 27 } 28 29 this.persist = persist; 30 this.objectClass = objectClass; 31 this.resultSet = resultSet; 32 this.type = type; 33 34 try { 35 hasNext = resultSet.next(); 36 } catch (SQLException e) { 37 throw new RuntimeSQLException(e); 38 } 39 } 40 41 public boolean hasNext() { 42 return hasNext; 43 } 44 45 public Object next() { 46 try { 47 final Object ret; 48 if (type == TYPE_OBJECT) { 49 ret = persist.loadObject(objectClass, resultSet); 50 } else if (type == TYPE_MAP) { 51 ret = Persist.loadMap(resultSet); 52 } else { 53 ret = null; 54 } 55 56 hasNext = resultSet.next(); 57 return ret; 58 } catch (SQLException e) { 59 throw new RuntimeSQLException(e); 60 } 61 } 62 63 public void remove() { 64 try { 65 this.resultSet.deleteRow(); 66 } catch (SQLException e) { 67 throw new RuntimeSQLException(e); 68 } 69 } 70 71 } 72 | Popular Tags |