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 |
| |
28 |
| public class ResultSetIterator implements Iterator |
29 |
| { |
30 |
| 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 |
0
| public ResultSetIterator(ResultSet objResultSet)
|
37 |
| { |
38 |
0
| m_objResultSet = objResultSet;
|
39 |
0
| m_bFetched = false;
|
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
0
| 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 |
0
| try
|
54 |
| { |
55 |
0
| m_bAvailable = !getResultSet().isLast();
|
56 |
| } |
57 |
| catch (SQLException e) |
58 |
| { |
59 |
0
| LOG.warn(
|
60 |
| "SQLException while testing for end of the ResultSet", |
61 |
| e); |
62 |
0
| m_bAvailable = false;
|
63 |
| } |
64 |
| |
65 |
0
| if (!m_bAvailable)
|
66 |
0
| notifyEnd();
|
67 |
| } |
68 |
| |
69 |
0
| return m_bAvailable;
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
0
| public synchronized Object next()
|
76 |
| { |
77 |
0
| ResultSet objResultSet = getResultSet();
|
78 |
| |
79 |
0
| try
|
80 |
| { |
81 |
0
| if (!objResultSet.next())
|
82 |
0
| return null;
|
83 |
| } |
84 |
| catch (SQLException e) |
85 |
| { |
86 |
0
| LOG.warn("SQLException while iterating over the ResultSet", e);
|
87 |
0
| return null;
|
88 |
| } |
89 |
| |
90 |
0
| m_bFetched = false;
|
91 |
0
| return objResultSet;
|
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
0
| public void remove()
|
98 |
| { |
99 |
0
| try
|
100 |
| { |
101 |
0
| getResultSet().deleteRow();
|
102 |
| } |
103 |
| catch (SQLException e) |
104 |
| { |
105 |
0
| LOG.error("Cannot delete record", e);
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
0
| public ResultSet getResultSet()
|
114 |
| { |
115 |
0
| return m_objResultSet;
|
116 |
| } |
117 |
| |
118 |
0
| protected void notifyEnd()
|
119 |
| { |
120 |
| } |
121 |
| |
122 |
| } |