comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorBase.java @ 132:5a583cff97ea

Implementation of the Datainfrastructure for fetching Data from different DataStores. geo-backend/trunk@12 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 04 Sep 2009 08:11:30 +0000
parents
children 56655046194f
comparison
equal deleted inserted replaced
131:d8ff739b9f3b 132:5a583cff97ea
1 /**
2 *
3 */
4 package de.intevation.gnv.geobackend.base.query;
5
6 import java.sql.ResultSet;
7 import java.sql.ResultSetMetaData;
8 import java.sql.SQLException;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.List;
12
13 import de.intevation.gnv.geobackend.base.DefaultResult;
14 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
15 import de.intevation.gnv.geobackend.base.Result;
16 import de.intevation.gnv.geobackend.base.ResultDescriptor;
17
18 /**
19 * This is an abstract Basicimplementation of the Interface
20 * QueryExecutor providing several Helpermethods.
21 * @author Tim Englich <tim.englich@intevation.de>
22 *
23 */
24 public abstract class QueryExecutorBase implements QueryExecutor {
25
26 /**
27 * Constructor
28 */
29 public QueryExecutorBase() {
30 super();
31 }
32
33 /**
34 * This Method puts the Filtervalues into the Querystring
35 * @param queryString the Query which should be manipulated
36 * @param filter the values which should be put into the Query
37 * @return the manipulated Query
38 */
39 protected String setFilterValues(String queryString, String[] filter){
40 String returnValue = queryString;
41 for (int i = 0; i < filter.length; i++){
42 returnValue = returnValue.replaceFirst("?", filter[i]);
43 }
44 return returnValue;
45 }
46
47 /**
48 * This Methods converts the ResultSet into an Collection of Result-objects
49 * @param resultSet the ResultSet which should be converted
50 * @return an Collection containing Result-Objects
51 * @throws SQLException
52 */
53 protected Collection<Result> createResultCollection(ResultSet resultSet) throws SQLException{
54 Collection<Result> returnValue = new ArrayList<Result>();
55
56 ResultDescriptor resultDescriptor = null;
57 int columns = -1;
58 List<String> columnNames = null;
59 while (resultSet.next()){
60 if (resultDescriptor == null){
61 resultDescriptor = new DefaultResultDescriptor();
62 ResultSetMetaData rsmd = resultSet.getMetaData();
63 columns = rsmd.getColumnCount();
64 columnNames = new ArrayList<String>(columns);
65 for (int i = 1; i <= columns; i++){
66 resultDescriptor.addColumn(rsmd.getColumnName(i), rsmd.getColumnClassName(i));
67 columnNames.add(rsmd.getColumnName(i));
68 }
69 }
70
71 Result result = convertResult(resultSet, resultDescriptor,columnNames);
72
73 returnValue.add(result);
74 }
75 return returnValue;
76 }
77
78 /**
79 * This Method converts one Singel ResultSetEntry into an Result-Object
80 * @param resultSet the ResultSet where the Entry should be took from.
81 * @param resultDescriptor the ResultsetDescriptor which describes the Entry
82 * @param columnNames the Name of the Columns which Values should be fetched.
83 * @return an new Result-Objects containing the Values of the ResultSet-Entry
84 * @throws SQLException
85 */
86 private Result convertResult(ResultSet resultSet,
87 ResultDescriptor resultDescriptor, List<String> columnNames)
88 throws SQLException {
89 Result result = new DefaultResult(resultDescriptor);
90 for (int i = 0; i < columnNames.size(); i++){
91 String columnName = columnNames.get(i);
92 result.addColumnValue(columnName, resultSet.getObject(columnName));
93 }
94 return result;
95 }
96
97 }

http://dive4elements.wald.intevation.org