1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.contrib.table.model.sql; |
16 | |
|
17 | |
import java.sql.ResultSet; |
18 | |
import java.sql.SQLException; |
19 | |
import java.util.Iterator; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class ResultSetIterator implements Iterator |
28 | |
{ |
29 | |
|
30 | 0 | private static final Log LOG = LogFactory.getLog(ResultSetIterator.class); |
31 | |
|
32 | |
private ResultSet m_objResultSet; |
33 | |
private boolean m_bFetched; |
34 | |
private boolean m_bAvailable; |
35 | |
|
36 | |
public ResultSetIterator(ResultSet objResultSet) |
37 | 0 | { |
38 | 0 | m_objResultSet = objResultSet; |
39 | 0 | m_bFetched = false; |
40 | 0 | } |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public synchronized boolean hasNext() |
46 | |
{ |
47 | 0 | if (getResultSet() == null) return false; |
48 | |
|
49 | 0 | if (!m_bFetched) |
50 | |
{ |
51 | 0 | m_bFetched = true; |
52 | |
|
53 | |
try |
54 | |
{ |
55 | 0 | m_bAvailable = !getResultSet().isLast(); |
56 | |
} |
57 | 0 | catch (SQLException e) |
58 | |
{ |
59 | 0 | LOG.warn("SQLException while testing for end of the ResultSet", |
60 | |
e); |
61 | 0 | m_bAvailable = false; |
62 | 0 | } |
63 | |
|
64 | 0 | if (!m_bAvailable) notifyEnd(); |
65 | |
} |
66 | |
|
67 | 0 | return m_bAvailable; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public synchronized Object next() |
74 | |
{ |
75 | 0 | ResultSet objResultSet = getResultSet(); |
76 | |
|
77 | |
try |
78 | |
{ |
79 | 0 | if (!objResultSet.next()) return null; |
80 | |
} |
81 | 0 | catch (SQLException e) |
82 | |
{ |
83 | 0 | LOG.warn("SQLException while iterating over the ResultSet", e); |
84 | 0 | return null; |
85 | 0 | } |
86 | |
|
87 | 0 | m_bFetched = false; |
88 | 0 | return objResultSet; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void remove() |
95 | |
{ |
96 | |
try |
97 | |
{ |
98 | 0 | getResultSet().deleteRow(); |
99 | |
} |
100 | 0 | catch (SQLException e) |
101 | |
{ |
102 | 0 | LOG.error("Cannot delete record", e); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public ResultSet getResultSet() |
112 | |
{ |
113 | 0 | return m_objResultSet; |
114 | |
} |
115 | |
|
116 | |
protected void notifyEnd() |
117 | |
{ |
118 | 0 | } |
119 | |
|
120 | |
} |