1 7 8 package com.hp.hpl.jena.db.impl; 11 12 import java.sql.*; 15 import java.util.*; 16 import com.hp.hpl.jena.util.iterator.*; 17 import com.hp.hpl.jena.shared.*; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 37 38 public class ResultSetIterator implements ExtendedIterator { 39 40 41 protected ResultSet m_resultSet; 42 43 44 protected SQLCache m_sqlCache; 45 46 47 protected PreparedStatement m_statement; 48 49 50 protected String m_opname; 51 52 53 protected ArrayList m_row; 54 55 56 protected int m_nCols; 57 58 59 protected boolean m_finished = false; 60 61 62 protected boolean m_prefetched = false; 63 64 protected static Log logger = LogFactory.getLog( ResultSetIterator.class ); 65 72 public ResultSetIterator() { 73 m_finished = true; } 75 76 83 public ResultSetIterator(ResultSet resultSet, PreparedStatement sourceStatement, SQLCache cache, String opname) { 84 m_resultSet = resultSet; 85 m_sqlCache = cache; 86 m_statement = sourceStatement; 87 m_opname = opname; 88 } 89 90 95 public ResultSetIterator(ResultSet resultSet, PreparedStatement sourceStatement) { 96 m_resultSet = resultSet; 97 m_statement = sourceStatement; 98 } 99 100 107 public void reset(ResultSet resultSet, PreparedStatement sourceStatement, SQLCache cache, String opname) { 108 m_resultSet = resultSet; 109 m_sqlCache = cache; 110 m_statement = sourceStatement; 111 m_opname = opname; 112 m_finished = false; 113 m_prefetched = false; 114 m_row = null; 115 } 116 117 120 public boolean hasNext() { 121 if (!m_finished && !m_prefetched) moveForward(); 122 return !m_finished; 123 } 124 125 public Object removeNext() 126 { cantRemove(); return null; } 127 128 131 public Object next() { 132 if (!m_finished && !m_prefetched) moveForward(); 133 m_prefetched = false; 134 if (m_finished) { 135 throw new NoSuchElementException(); 136 } 137 return getRow(); 138 } 139 140 143 public void remove() { 144 cantRemove(); 145 } 146 147 protected void cantRemove() { 148 throw new UnsupportedOperationException ("ResultSetIterator can't remove database rows"); 149 } 150 151 154 protected void moveForward() { 155 try { 156 if (!m_finished && m_resultSet.next()) { 157 extractRow(); 158 m_prefetched = true; 159 } else { 160 close(); 161 } 162 } catch (Exception e) { 163 logger.warn("Problem in iterator over db result set, op = " + m_opname, e); 165 throw new JenaException( e ); 167 } 168 } 169 170 174 protected void extractRow() throws Exception { 175 if (m_row == null) { 176 m_nCols = m_resultSet.getMetaData().getColumnCount(); 177 m_row = new ArrayList(m_nCols); 178 for (int i = 0; i < m_nCols; i++) m_row.add(null); 179 } 180 for (int i = 0; i < m_nCols; i++) { 181 m_row.set(i, m_resultSet.getObject(i+1)); 182 } 183 } 184 185 189 protected Object getRow() { 190 return m_row; 191 } 192 193 197 public void close() { 198 if (!m_finished) { 199 if (m_resultSet != null) { 200 try { 201 m_resultSet.close(); 202 m_resultSet = null; 203 } catch (SQLException e) { 204 logger.warn("Error while finalizing result set iterator", e); 205 } 206 } 207 if (m_sqlCache != null && m_opname != null) { 208 m_sqlCache.returnPreparedSQLStatement(m_statement); 209 } else { 210 try { 211 m_statement.close(); 212 } catch (SQLException e) { 213 logger.warn("Error while finalizing result set iterator", e); 214 } 215 } 216 } 217 m_finished = true; 218 } 219 220 224 public Object getSingleton() throws SQLException { 225 List row = (List) next(); 226 close(); 227 return row.get(0); 228 } 229 230 234 protected void finalize() throws SQLException { 235 if (!m_finished && m_resultSet != null) close(); 236 } 237 238 243 public ExtendedIterator andThen(ClosableIterator other) { 244 return NiceIterator.andThen(this, other); 245 } 246 247 250 public ExtendedIterator filterKeep(Filter f) { 251 return new FilterIterator( f, this ); 252 } 253 254 257 public ExtendedIterator filterDrop(final Filter f) { 258 Filter notF = new Filter() { public boolean accept( Object x ) { return !f.accept( x ); } }; 259 return new FilterIterator( notF, this ); 260 } 261 262 265 public ExtendedIterator mapWith(Map1 map1) { 266 return new Map1Iterator( map1, this ); 267 } 268 269 } 271 297 | Popular Tags |