ResultSetCollection.java

biz/hammurapi/sql/ResultSetCollection.java

Violations

Inspector Message Severity Location
Java Inspector 002 Empty catch block. 1 315:43
Java Inspector 002 Empty catch block. 1 333:51
Java Inspector 002 Empty catch block. 1 365:51
Java Inspector 039 Avoid "for", "do", "while", "if" and "if ... else" statements with empty bodies 1 310:49
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 068 Do not use System.out and System.err to output logging messages. Use loggers instead. 2 390:59
Java Inspector 082 Parenthesis are redundant. 2 310:74
Java Inspector 082 Parenthesis are redundant. 2 419:42
Java Inspector 083 Do not use printStackTrace() for exception logging. 2 396:58
Java Inspector 089 Undocumented top level type 2 42:7
Java Inspector 089 Constructor is not properly documented 2 57:9
Java Inspector 089 Parameter processor is not documented 2 57:9
Java Inspector 089 Parameter sql is not documented 2 57:9
Java Inspector 089 Parameter parameterizer is not documented 2 57:9
Java Inspector 089 Parameter projector is not documented 2 57:9
Java Inspector 089 Constructor is not properly documented 2 72:9
Java Inspector 089 Undocumented parameter processor 2 72:9
Java Inspector 089 Undocumented parameter sql 2 72:9
Java Inspector 089 Undocumented parameter parameterizer 2 72:9
Java Inspector 089 Undocumented parameter projector 2 72:9
Java Inspector 089 Parameter pageNum is not documented 2 72:9
Java Inspector 089 Parameter pageSize is not documented 2 72:9
Java Inspector 089 Javadoc contains tag for non-existent parameter processor2 2 72:9
Java Inspector 089 Javadoc contains tag for non-existent parameter string 2 72:9
Java Inspector 089 Javadoc contains tag for non-existent parameter parameterizer2 2 72:9
Java Inspector 089 Javadoc contains tag for non-existent parameter projector2 2 72:9
Java Inspector 089 Undocumented method 2 78:9
Java Inspector 089 Undocumented method 2 86:49
Java Inspector 089 Undocumented method 2 99:9
Java Inspector 089 Undocumented method 2 103:9
Java Inspector 089 Undocumented method 2 111:49
Java Inspector 089 Undocumented method 2 125:9
Java Inspector 089 Undocumented method 2 134:9
Java Inspector 089 Undocumented method 2 138:9
Java Inspector 089 Undocumented method 2 146:49
Java Inspector 089 Undocumented method 2 161:9
Java Inspector 089 Undocumented method 2 169:49
Java Inspector 089 Undocumented method 2 185:9
Java Inspector 089 Undocumented method 2 189:9
Java Inspector 089 Undocumented method 2 197:49
Java Inspector 089 Undocumented method 2 211:9
Java Inspector 089 Undocumented method 2 219:49
Java Inspector 089 Undocumented method 2 235:9
Java Inspector 089 Undocumented method 2 243:49
Java Inspector 089 Undocumented method 2 259:9
Java Inspector 089 Undocumented method 2 266:25
Java Inspector 089 Undocumented method 2 326:25
Java Inspector 089 Undocumented method 2 347:25
Java Inspector 089 Undocumented method 2 376:25
Java Inspector 089 Undocumented method 2 388:25
Java Inspector 089 Undocumented method 2 405:9
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 310:131
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 340:73
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 390:60
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 390:95
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 390:132
Java Inspector 046 Empty statements 3 310:131
Java Inspector 046 Empty statements 3 315:67
Java Inspector 046 Empty statements 3 333:75
Java Inspector 046 Empty statements 3 365:75
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 57:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 72:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.sql;
24
25import java.sql.Connection;
26import java.sql.PreparedStatement;
27import java.sql.ResultSet;
28import java.sql.SQLException;
29import java.sql.Statement;
30import java.util.Collection;
31import java.util.Iterator;
32import java.util.LinkedList;
33import java.util.NoSuchElementException;
34
35/**
36 * Collection backed by the database. It doesn't hold resultsets open
37 * but executes SQL statements for every method call. Iterators produced
38 * by this collection are backed by open ResultSet.
39 * @author Pavel Vlasov
40 * @version $Revision: 1.6 $
41 */
42class ResultSetCollection implements Collection {
43 private SQLProcessor processor;
44 private String sql;
45 private Parameterizer parameterizer;
46 private Projector projector;
47 private Integer pageSize;
48 private int pageNum;
49
50
51 /**
52 * @param processor
53 * @param sql
54 * @param parameterizer
55 * @param projector
56 */
57 public ResultSetCollection(SQLProcessor processor, String sql, Parameterizer parameterizer, Projector projector) {
58 this.processor=processor;
59 this.sql=sql;
60 this.parameterizer=parameterizer;
61 this.projector=projector;
62 }
63
64 /**
65 * @param processor2
66 * @param string
67 * @param parameterizer2
68 * @param projector2
69 * @param pageNum
70 * @param pageSize
71 */
72 public ResultSetCollection(SQLProcessor processor, String sql, Parameterizer parameterizer, Projector projector, int pageNum, int pageSize) {
73 this(processor, sql, parameterizer, projector);
74 this.pageSize=new Integer(pageSize);
75 this.pageNum=pageNum;
76 }
77
78 public int size() {
79 try {
80 final int[] ret={0};
81 final int[] counter={0};
82 processor.processSelect(
83 sql,
84 parameterizer,
85 new RowProcessor() {
86 public boolean process(ResultSet rs) {
87 if (onPage(counter)) {
88 ++ret[0];
89 }
90 return true;
91 }
92 });
93 return ret[0];
94 } catch (SQLException e) {
95 throw new SQLRuntimeException(e);
96 }
97 }
98
99 public void clear() {
100 throw new UnsupportedOperationException();
101 }
102
103 public boolean isEmpty() {
104 try {
105 final boolean[] ret={true};
106 final int[] counter={0};
107 processor.processSelect(
108 sql,
109 parameterizer,
110 new RowProcessor() {
111 public boolean process(ResultSet rs) {
112 if (onPage(counter)) {
113 ret[0]=false;
114 return false;
115 }
116 return true;
117 }
118 });
119 return ret[0];
120 } catch (SQLException e) {
121 throw new SQLRuntimeException(e);
122 }
123 }
124
125 public Object[] toArray() {
126 LinkedList ll = new LinkedList();
127 Iterator it = iterator();
128 while (it.hasNext()) {
129 ll.add(it.next());
130 }
131 return ll.toArray();
132 }
133
134 public boolean add(Object o) {
135 throw new UnsupportedOperationException();
136 }
137
138 public boolean contains(final Object o) {
139 try {
140 final boolean[] ret={false};
141 final int[] counter={0};
142 processor.processSelect(
143 sql,
144 parameterizer,
145 new RowProcessor() {
146 public boolean process(ResultSet rs) throws SQLException {
147 if (onPage(counter)) {
148 if (o.equals(projector.project(rs))) {
149 ret[0]=true;
150 }
151 }
152 return !ret[0];
153 }
154 });
155 return ret[0];
156 } catch (SQLException e) {
157 throw new SQLRuntimeException(e);
158 }
159 }
160
161 public boolean remove(final Object o) {
162 try {
163 final boolean[] ret={false};
164 final int[] counter={0};
165 processor.processSelect(
166 sql,
167 parameterizer,
168 new RowProcessor() {
169 public boolean process(ResultSet rs) throws SQLException {
170 if (onPage(counter)) {
171 if (o.equals(projector.project(rs))) {
172 rs.deleteRow();
173 ret[0]=true;
174 }
175 }
176 return true;
177 }
178 });
179 return ret[0];
180 } catch (SQLException e) {
181 throw new SQLRuntimeException(e);
182 }
183 }
184
185 public boolean addAll(Collection c) {
186 throw new UnsupportedOperationException();
187 }
188
189 public boolean containsAll(Collection c) {
190 try {
191 final Collection param=new LinkedList(c);
192 final int[] counter={0};
193 processor.processSelect(
194 sql,
195 parameterizer,
196 new RowProcessor() {
197 public boolean process(ResultSet rs) throws SQLException {
198 if (onPage(counter)) {
199 Object o=projector.project(rs);
200 param.remove(o);
201 }
202 return !param.isEmpty();
203 }
204 });
205 return param.isEmpty();
206 } catch (SQLException e) {
207 throw new SQLRuntimeException(e);
208 }
209 }
210
211 public boolean removeAll(final Collection c) {
212 try {
213 final boolean[] ret={false};
214 final int[] counter={0};
215 processor.processSelect(
216 sql,
217 parameterizer,
218 new RowProcessor() {
219 public boolean process(ResultSet rs) throws SQLException {
220 if (onPage(counter)) {
221 if (c.contains(projector.project(rs))) {
222 rs.deleteRow();
223 ret[0]=true;
224 }
225 }
226 return true;
227 }
228 });
229 return ret[0];
230 } catch (SQLException e) {
231 throw new SQLRuntimeException(e);
232 }
233 }
234
235 public boolean retainAll(final Collection c) {
236 try {
237 final boolean[] ret={false};
238 final int[] counter={0};
239 processor.processSelect(
240 sql,
241 parameterizer,
242 new RowProcessor() {
243 public boolean process(ResultSet rs) throws SQLException {
244 if (onPage(counter)) {
245 if (!c.contains(projector.project(rs))) {
246 rs.deleteRow();
247 ret[0]=true;
248 }
249 }
250 return true;
251 }
252 });
253 return ret[0];
254 } catch (SQLException e) {
255 throw new SQLRuntimeException(e);
256 }
257 }
258
259 public Iterator iterator() {
260 return new DataIterator() {
261 private Connection connection;
262 private Statement statement;
263 private ResultSet rs;
264 private int counter;
265
266 public void close() throws SQLException {
267 //System.out.println("Iterator close: "+ --openIteratorCounter);
268
269 try {
270 if (rs!=null) {
271 rs.close();
272 }
273 } finally {
274 rs=null;
275 try {
276 if (statement!=null) {
277 statement.close();
278 }
279 } finally {
280 statement=null;
281 try {
282 if (connection!=null) {
283 processor.releaseConnection(connection);
284 }
285 } finally {
286 connection=null;
287 }
288 }
289 }
290 }
291
292 private Object o;
293
294 {
295 //System.out.println("Iterator open: "+ ++openIteratorCounter);
296 //new Exception("Iterator stack trace for debugging").printStackTrace(System.out);
297
298 long start = processor.getTimeIntervalCategory()==null ? 0 : processor.getTimeIntervalCategory().getTime();
299 try {
300 connection=processor.getConnection();
301 if (parameterizer==null) {
302 statement=connection.createStatement();
303 rs=statement.executeQuery(sql);
304 } else {
305 PreparedStatement ps=connection.prepareStatement(sql);
306 statement=ps;
307 parameterizer.parameterize(ps, 1);
308 rs=ps.executeQuery();
309 // moving to the first record in page.
310 while (pageSize!=null && (++counter<pageSize.intValue()*(pageNum-1)) && rs.next());
311 }
312 } catch (SQLException e) {
313 try {
314 close();
315 } catch (SQLException ee) {
316 // We already have an exception, so we ignore cleanup one.
317 }
318 throw new SQLRuntimeException(e);
319 } finally {
320 if (processor.getTimeIntervalCategory()!=null) {
321 processor.getTimeIntervalCategory().addInterval(sql, start);
322 }
323 }
324 }
325
326 public void remove() {
327 if (nextCalled) {
328 try {
329 rs.deleteRow();
330 } catch (SQLException e) {
331 try {
332 close();
333 } catch (SQLException ee) {
334 // Nothing
335 }
336
337 throw new SQLRuntimeException(e);
338 }
339 } else {
340 throw new IllegalStateException("Call next() first");
341 }
342 }
343
344 private boolean nextCalled=true;
345 private boolean lastResult=false;
346
347 public boolean hasNext() {
348 if (nextCalled) {
349 nextCalled=false;
350 try {
351 if ((pageSize==null || counter<pageSize.intValue()*pageNum) && rs.next()) {
352 o=projector.project(rs);
353 if (o instanceof DataAccessObject) {
354 ((DataAccessObject) o).setSQLProcessor(processor);
355 }
356
357 lastResult=true;
358 } else {
359 lastResult=false;
360 close();
361 }
362 } catch (SQLException e) {
363 try {
364 close();
365 } catch (SQLException ee) {
366 // Nothing
367 }
368
369 throw new SQLRuntimeException(e);
370 }
371 }
372
373 return lastResult;
374 }
375
376 public Object next() {
377 if (nextCalled) {
378 if (!hasNext()) {
379 throw new NoSuchElementException();
380 }
381 }
382
383 nextCalled=true;
384 ++counter;
385 return o;
386 }
387
388 protected void finalize() throws Throwable {
389 if (connection!=null) {
390 System.err.println("WARN ["+this.getClass().getName()+"]: Connection leak for query '"+sql+"'");
391 }
392
393 try {
394 close();
395 } catch (SQLException e) {
396 e.printStackTrace();
397 }
398 super.finalize();
399 }
400
401 };
402 }
403
404
405 public Object[] toArray(Object[] a) {
406 LinkedList ll = new LinkedList();
407 Iterator it = iterator();
408 while (it.hasNext()) {
409 ll.add(it.next());
410 }
411 return ll.toArray(a);
412 }
413
414 /**
415 * @param counter
416 * @return
417 */
418 private boolean onPage(final int[] counter) {
419 return pageSize==null || (++counter[0]>(pageNum-1)*pageSize.intValue() && counter[0]<=pageNum*pageSize.intValue());
420 }
421
422}
423