Source code: org/jdbf/engine/sql/SelectStatement.java
1 /*
2 * 07/05/2002 - 23:46:45
3 *
4 * SelectStatement.java - JDBF Object Relational mapping system
5 * Copyright (C) 2002 Giovanni Martone
6 * giovannimartone@hotmail.com
7 * http://jdbf.sourceforge.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 package org.jdbf.engine.sql;
25
26 import java.sql.*;
27 import java.util.ArrayList;
28 import org.jdbf.engine.basic.ObjectMapped;
29 import org.jdbf.engine.caching.CacheManager;
30 import org.jdbf.engine.criteria.Criteria;
31 import org.jdbf.engine.mapping.*;
32 import org.jdbf.engine.reflection.*;
33 import org.jdbf.engine.repository.*;
34 import org.jdbf.engine.sql.*;
35 import org.jdbf.engine.sql.connection.*;
36
37 /**
38 * <code>SelectStatement</code> is that class that represents the select sql
39 * statement.
40 * SelectStatement handles the creation of sql statement with the informations
41 * specified in a RepositoryView object and provides to execute the select statement.
42 */
43 public class SelectStatement extends SQLStatement{
44
45 /** SELECT statement */
46 protected final static String SELECT = SqlInterface.SELECT;
47 /** FROM statement */
48 protected final static String FROM = SqlInterface.FROM;
49
50
51 /**
52 * Creates the SelectStatement object and build the statement
53 *
54 * @param view repository, propertiesNames names of properties to update
55 */
56 public SelectStatement(RepositoryView view,String[] propertiesNames,
57 Criteria criteria){
58 super(view,null,criteria);
59 }
60
61
62 /**
63 * Creates the empty object
64 */
65 public SelectStatement(){
66 super();
67 }
68
69
70 /**
71 * Builds the select statement with the informations passed by repository.
72 * These informations are:
73 * <li> tableName;
74 * <li> columnTableName
75 *
76 * @param repository, propertiesNames name of properties to select
77 * @return String select statement
78 */
79 public String buildStatement(RepositoryView repository,String[] propertiesNames){
80 try{
81 StringBuffer buff = new StringBuffer(SELECT);
82 BeanDescriptor beanDescriptor = repository.getBeanDescriptor();
83 ArrayList items = beanDescriptor.getItemDescriptors();
84 String tableName = beanDescriptor.getTableName();
85 buff.append(FROM).append(tableName);
86
87 if(criteria != null){
88 int length = criteria.getCriteria().length();
89 if(length > 0){
90 buff.append(" ").append(WHERE);
91 buff.append(criteria.getCriteria());
92 }
93 ArrayList order = (ArrayList)criteria.getOrderConditions();
94 if(order.size() > 0)
95 buff.append(" ").append(criteria.getOrderByClause(items));
96 }
97 statement = buff.toString();
98
99 }
100 catch(MappingException e){
101 e.printStackTrace();
102 }
103 finally{
104 return statement;
105 }
106 }
107
108
109 public String buildStatementForCriteria(RepositoryView repository,String[] propertiesNames){
110 return buildStatement(repository,null);
111 }
112
113
114
115 /**
116 * Executes the select statement and retunathe ObjectMapped object specified in obj.
117 *
118 * @param String repositoryViewName, view contains the informations about property of object mapped,
119 * con Connection object,values arrays of values to search.
120 *
121 * @return QueryResults set of results
122 * @throws SQLException,MappingException if datatype is invalid or table not exist.
123 */
124 public QueryResults select(String repositoryViewName,RepositoryView view,
125 Connection connection)
126 throws SQLException,MappingException{
127
128
129 StringBuffer identifier = new StringBuffer(repositoryViewName);
130
131 String hashCode = null;
132 if(criteria == null){
133 hashCode = "all";
134 }
135 else{
136 hashCode = String.valueOf(criteria.hashCode());
137 }
138 identifier.append(hashCode);
139
140 //get object from cache
141 QueryResults queryResults = (QueryResults)CacheManager.getCache(
142 identifier.toString()
143 );
144 if(queryResults == null){
145
146 //Build update statement
147 PreparedStatement preStat = connection.prepareStatement(statement);
148
149 ResultSet results = preStat.executeQuery();
150
151 queryResults = new Cursor(getValuesFromResultSet(view,results),
152 identifier.toString());
153
154 CacheManager.putCache(queryResults);
155 preStat.close();
156 }
157 return queryResults;
158 }
159
160
161 /**
162 * Get results of select statement from ResultSet.
163 *
164 * It creates an instance of ObjectMapped object. Type of ObjectMapped
165 * is specified in repository, then it sets all properties of this
166 * object with the values
167 *
168 * @param view RepositoryView object, results ResultSet object
169 * @return ArrayList an collection of ObjectMapped
170 * @throws SQLException,MappingException
171 */
172 protected ArrayList getValuesFromResultSet(RepositoryView view,ResultSet results)
173 throws SQLException,MappingException{
174
175 ArrayList res = new ArrayList();
176
177 BeanDescriptor beanDesc = view.getBeanDescriptor();
178 ArrayList itemDesc = beanDesc.getItemDescriptors();
179 java.util.HashMap props = new java.util.HashMap();
180 props.put("repositoryViewName", beanDesc.getRepositoryViewName());
181
182 //loop ResultSet
183 while(results.next()){
184
185 ObjectMapped map = ReflectionManager.createBean(beanDesc.getClassName(),props);
186
187 //get values from ResultSet
188 for(int i = 0; i < itemDesc.size(); i++){
189 ItemDescriptor item = (ItemDescriptor)itemDesc.get(i);
190 String propertyName = item.getPropertyName();
191 Object propertyValue = results.getObject(propertyName);
192 map = view.setPropertyValue(map,propertyName,propertyValue);
193 }
194 res.add(map);
195 }
196 results.close();
197 return res;
198 }
199 }