01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.rdbms.iteration.base;
07:
08: import info.aduna.iteration.CloseableIteration;
09:
10: import java.sql.PreparedStatement;
11: import java.sql.ResultSet;
12: import java.sql.SQLException;
13:
14: /**
15: * Base class for Iteration of a {@link ResultSet}.
16: *
17: * @author James Leigh
18: *
19: */
20: public abstract class RdbmIterationBase<T, X extends Exception>
21: implements CloseableIteration<T, X> {
22: private PreparedStatement stmt;
23: private ResultSet rs;
24: private boolean advanced;
25: private boolean hasNext;
26:
27: public RdbmIterationBase(PreparedStatement stmt)
28: throws SQLException {
29: super ();
30: this .stmt = stmt;
31: if (stmt != null) {
32: this .rs = stmt.executeQuery();
33: }
34: }
35:
36: public void close() throws X {
37: try {
38: rs.close();
39: stmt.close();
40: } catch (SQLException e) {
41: throw convertSQLException(e);
42: }
43: }
44:
45: public boolean hasNext() throws X {
46: if (advanced)
47: return hasNext;
48: advanced = true;
49: try {
50: return hasNext = rs.next();
51: } catch (SQLException e) {
52: throw convertSQLException(e);
53: }
54: }
55:
56: public T next() throws X {
57: try {
58: if (!advanced) {
59: hasNext = rs.next();
60: }
61: advanced = false;
62: return convert(rs);
63: } catch (SQLException e) {
64: throw convertSQLException(e);
65: }
66: }
67:
68: public void remove() throws X {
69: try {
70: rs.rowDeleted();
71: } catch (SQLException e) {
72: throw convertSQLException(e);
73: }
74: }
75:
76: protected abstract T convert(ResultSet rs) throws SQLException;
77:
78: protected abstract X convertSQLException(SQLException e);
79:
80: }
|