1 /*
2 QueryJ
3
4 Copyright (C) 2002 Jose San Leandro Armend?riz
5 jsanleandro@yahoo.es
6 chousz@yahoo.com
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Thanks to ACM S.L. for distributing this library under the GPL license.
23 Contact info: jsanleandro@yahoo.es
24 Postal Address: c/Playa de Lagoa, 1
25 Urb. Valdecaba?as
26 Boadilla del monte
27 28660 Madrid
28 Spain
29
30 ******************************************************************************
31 *
32 * Filename: $RCSfile: SelectQuery.java,v $
33 *
34 * Author: Jose San Leandro Armend?riz
35 *
36 * Description: Standard SQL select query.
37 *
38 * Last modified by: $Author: chous $ at $Date: 2003/08/29 08:15:00 $
39 *
40 * File version: $Revision: 1.1 $
41 *
42 * Project version: $Name: $
43 *
44 * $Id: SelectQuery.java,v 1.1 2003/08/29 08:15:00 chous Exp $
45 *
46 */
47 package org.acmsl.queryj;
48
49 /*
50 * Importing some ACM-SL classes.
51 */
52 import org.acmsl.queryj.Condition;
53 import org.acmsl.queryj.Field;
54 import org.acmsl.queryj.Query;
55 import org.acmsl.queryj.Table;
56
57 /*
58 * Importing some JDK classes.
59 */
60 import java.sql.PreparedStatement;
61 import java.sql.ResultSet;
62 import java.sql.SQLException;
63 import java.sql.Statement;
64 import java.util.ArrayList;
65 import java.util.List;
66
67 /***
68 * Represents standard SQL select queries.
69 * @author <a href="mailto:jsanleandro@yahoo.es"
70 >Jose San Leandro</a>
71 * @version $Revision: 1.1 $
72 */
73 public abstract class SelectQuery
74 extends Query
75 {
76 /***
77 * The fields.
78 */
79 private List m__lFields;
80
81 /***
82 * The ordering fields.
83 */
84 private List m__lOrderingFields;
85
86 /***
87 * The grouping fields.
88 */
89 private List m__lGroupingFields;
90
91 /***
92 * Constructs a query.
93 */
94 public SelectQuery()
95 {
96 super();
97 unmodifiableSetFields(new ArrayList());
98 unmodifiableSetOrderingFields(new ArrayList());
99 unmodifiableSetGroupingFields(new ArrayList());
100 }
101
102 /***
103 * Specifies new field collection.
104 * @param list the new list.
105 */
106 private void unmodifiableSetFields(List list)
107 {
108 m__lFields = list;
109 }
110
111 /***
112 * Specifies new field collection.
113 * @param list the new list.
114 */
115 protected void setFields(List list)
116 {
117 unmodifiableSetFields(list);
118 }
119
120 /***
121 * Retrieves the field collection.
122 * @return such list.
123 */
124 protected List getFields()
125 {
126 return m__lFields;
127 }
128
129 /***
130 * Adds a new field.
131 * @param field the field to add.
132 */
133 protected void addField(Field field)
134 {
135 if (field != null)
136 {
137 List t_Fields = getFields();
138
139 if (t_Fields != null)
140 {
141 t_Fields.add(field);
142 }
143 }
144 }
145
146 /***
147 * Retrieves the position of given field on the query.
148 * @param field the field to find.
149 * @return its position, or -1 if such field doesn't belong to
150 * this query.
151 */
152 protected int getFieldIndex(Field field)
153 {
154 return getIndex(getFields(), field);
155 }
156
157 /***
158 * Specifies new ordering field collection.
159 * @param list the new list.
160 */
161 private void unmodifiableSetOrderingFields(List list)
162 {
163 m__lOrderingFields = list;
164 }
165
166 /***
167 * Specifies new ordering field collection.
168 * @param list the new list.
169 */
170 protected void setOrderingFields(List list)
171 {
172 unmodifiableSetOrderingFields(list);
173 }
174
175 /***
176 * Retrieves the ordering field collection.
177 * @return such list.
178 */
179 protected List getOrderingFields()
180 {
181 return m__lOrderingFields;
182 }
183
184 /***
185 * Adds a new ordering field.
186 * @param orderingField the ordering field to add.
187 */
188 protected void addOrderingField(Field orderingField)
189 {
190 if (orderingField != null)
191 {
192 List t_OrderingFields = getOrderingFields();
193
194 if (t_OrderingFields != null)
195 {
196 t_OrderingFields.add(orderingField);
197 }
198 }
199 }
200
201 /***
202 * Retrieves the position of given ordering field on the query.
203 * @param field the field to find.
204 * @return its position, or -1 if such field doesn't belong to
205 * this query.
206 */
207 protected int getOrderingFieldIndex(Field field)
208 {
209 return getIndex(getOrderingFields(), field);
210 }
211
212 /***
213 * Specifies new grouping field collection.
214 * @param list the new list.
215 */
216 private void unmodifiableSetGroupingFields(List list)
217 {
218 m__lGroupingFields = list;
219 }
220
221 /***
222 * Specifies new grouping field collection.
223 * @param list the new list.
224 */
225 protected void setGroupingFields(List list)
226 {
227 unmodifiableSetGroupingFields(list);
228 }
229
230 /***
231 * Retrieves the grouping field collection.
232 * @return such list.
233 */
234 protected List getGroupingFields()
235 {
236 return m__lGroupingFields;
237 }
238
239 /***
240 * Adds a new grouping field.
241 * @param groupingField the grouping field to add.
242 */
243 protected void addGroupingField(Field groupingField)
244 {
245 if (groupingField != null)
246 {
247 List t_GroupingFields = getGroupingFields();
248
249 if (t_GroupingFields != null)
250 {
251 t_GroupingFields.add(groupingField);
252 }
253 }
254 }
255
256 /***
257 * Retrieves the position of given grouping field on the query.
258 * @param field the field to find.
259 * @return its position, or -1 if such field doesn't belong to
260 * this query.
261 */
262 protected int getGroupingFieldIndex(Field field)
263 {
264 return getIndex(getGroupingFields(), field);
265 }
266
267 /***
268 * Selects a field.
269 * @param field the field to select.
270 */
271 public void select(Field field)
272 {
273 if (field != null)
274 {
275 addField(field);
276 //m__Factory.add(this, field);
277 }
278 }
279
280 /***
281 * Indicates which table participates in the query.
282 * @param table the table.
283 */
284 public void from(Table table)
285 {
286 if (table != null)
287 {
288 addTable(table);
289 //m__Factory.add(this, table);
290 }
291 }
292
293 /***
294 * Indicates a query condition.
295 * @param condition such condition.
296 */
297 public void where(Condition condition)
298 {
299 if (condition != null)
300 {
301 addCondition(condition);
302 //m__Factory.add(this, condition);
303 }
304 }
305
306 /***
307 * Indicates a query variable condition.
308 * @param variableCondition such variable condition.
309 */
310 public void where(VariableCondition variableCondition)
311 {
312 if (variableCondition != null)
313 {
314 addCondition(variableCondition);
315 addVariableCondition(variableCondition);
316 //m__Factory.add(this, variableCondition);
317 }
318 }
319
320 /***
321 * Indicates a field to be used to group the results.
322 * @param groupingField such field.
323 */
324 public void groupBy(Field groupingField)
325 {
326 if (groupingField != null)
327 {
328 addGroupingField(groupingField);
329 //m__Factory.add(this, groupingField);
330 }
331 }
332
333 /***
334 * Indicates a field to be used to order the results.
335 * @param orderingField such field.
336 */
337 public void orderBy(Field orderingField)
338 {
339 if (orderingField != null)
340 {
341 addOrderingField(orderingField);
342 //m__Factory.add(this, orderingField);
343 }
344 }
345
346 /***
347 * Looks for the records that match the filter.
348 * @param sql (Taken from Sun's Javadoc) an SQL statement to be sent
349 * to the database, typically a static SQL SELECT statement.
350 * @return (Taken from Sun's Javadoc) a ResultSet object that
351 * contains the data produced by the given query; never null.
352 * @exception SQLException if an error occurs.
353 */
354 public QueryResultSet retrieveMatchingResults()
355 throws SQLException
356 {
357 QueryResultSet result = null;
358
359 Statement t_Statement = getPreparedStatement();
360
361 if (t_Statement != null)
362 {
363 result =
364 new QueryResultSet(
365 this,
366 t_Statement.executeQuery(toString())) {};
367 }
368
369 return result;
370 }
371
372 /***
373 * See java.sql.PreparedStatement#executeQuery().
374 * @see java.sql.PreparedStatement#executeQuery()
375 * @return (Taken from Sun's Javadoc) a ResultSet object
376 * that contains the data produced by the query; never null.
377 * @exception SQLException if an error occurs.
378 */
379 public ResultSet executeQuery()
380 throws SQLException
381 {
382 ResultSet result = null;
383
384 PreparedStatement t_PreparedStatement =
385 getPreparedStatement();
386
387 if (t_PreparedStatement != null)
388 {
389 result =
390 new QueryResultSet(
391 this, t_PreparedStatement.getResultSet()) {};
392 }
393
394 return result;
395 }
396
397 // New from java.sql.Statement 1.4 //
398
399 /***
400 * See Statement#getGeneratedKeys()
401 * @see java.sql.Statement#getGeneratedKeys()
402 * @return (Taken from Sun's Javadoc) a ResultSet object containing the
403 * auto-generated key(s) generated by the execution of this Statement
404 * object.
405 * @exception SQLException if an error occurs.
406 */
407 public ResultSet getGeneratedKeys()
408 throws SQLException
409 {
410 ResultSet result = null;
411
412 Statement t_Statement =
413 getPreparedStatement();
414
415 if (t_Statement != null)
416 {
417 result =
418 new QueryResultSet(
419 this, t_Statement.getGeneratedKeys()) {};
420 }
421
422 return result;
423 }
424
425 /***
426 * Executes given update operation using field references.
427 * @param sql (Taken from Sun's Javadoc) must be an SQL INSERT, UPDATE
428 * or DELETE statement or an SQL statement that returns nothing.
429 * @param fields the fields.
430 * @return (Taken from Sun's Javadoc) either the row count for INSERT,
431 * UPDATE or DELETE statements, or 0 for SQL statements that return
432 * nothing.
433 * @exception SQLException if an error occurs.
434 */
435 public int executeUpdate(String sql, Field[] fields)
436 throws SQLException
437 {
438 int result = 0;
439
440 Statement t_Statement = getPreparedStatement();
441
442 if (t_Statement != null)
443 {
444 int[] t_aiIndexes = new int[fields.length];
445
446 for (int t_iIndex = 0; t_iIndex < fields.length; t_iIndex++)
447 {
448 t_aiIndexes[t_iIndex] = getFieldIndex(fields[t_iIndex]);
449 }
450
451 result = t_Statement.executeUpdate(sql, t_aiIndexes);
452 }
453
454 return result;
455 }
456
457 /***
458 * Executes given update operation using field references.
459 * @param sql (Taken from Sun's Javadoc) must be an SQL INSERT, UPDATE
460 * or DELETE statement or an SQL statement that returns nothing.
461 * @param fields the fields.
462 * @return (Taken from Sun's Javadoc) true if the first result is a
463 * ResultSet object; false if it is an update count or there are no
464 * results.
465 * @exception SQLException if an error occurs.
466 */
467 public boolean execute(String sql, Field[] fields)
468 throws SQLException
469 {
470 boolean result = false;
471
472 Statement t_Statement = getPreparedStatement();
473
474 if (t_Statement != null)
475 {
476 int[] t_aiIndexes = new int[fields.length];
477
478 for (int t_iIndex = 0; t_iIndex < fields.length; t_iIndex++)
479 {
480 t_aiIndexes[t_iIndex] = getFieldIndex(fields[t_iIndex]);
481 }
482
483 result = t_Statement.execute(sql, t_aiIndexes);
484 }
485
486 return result;
487 }
488
489 // Serialization methods //
490
491 /***
492 * Outputs a text version of the query, in SQL format.
493 * @return the SQL query.
494 */
495 public String toString()
496 {
497 StringBuffer t_sbResult = new StringBuffer();
498
499 QueryUtils t_QueryUtils = QueryUtils.getInstance();
500
501 if (t_QueryUtils != null)
502 {
503 t_sbResult.append("SELECT ");
504
505 t_sbResult.append(
506 t_QueryUtils.concatenate(getFields(), ", "));
507
508 t_sbResult.append(" FROM ");
509
510 t_sbResult.append(
511 t_QueryUtils.concatenate(getTables(), ", "));
512
513 t_sbResult.append(" WHERE ");
514
515 t_sbResult.append(
516 t_QueryUtils.concatenate(getConditions(), " AND "));
517
518 List t_lAdditionalFields = getGroupingFields();
519
520 if ( (t_lAdditionalFields != null)
521 && (t_lAdditionalFields.size() > 0))
522 {
523 t_sbResult.append(" GROUP BY ");
524
525 t_sbResult.append(
526 t_QueryUtils.concatenate(t_lAdditionalFields, ", "));
527 }
528
529 t_lAdditionalFields = getOrderingFields();
530
531 if ( (t_lAdditionalFields != null)
532 && (t_lAdditionalFields.size() > 0))
533 {
534 t_sbResult.append(" ORDER BY ");
535
536 t_sbResult.append(
537 t_QueryUtils.concatenate(t_lAdditionalFields, ", "));
538 }
539 }
540
541 return t_sbResult.toString();
542 }
543 }
This page was automatically generated by Maven