2 * Copyright (C) 2001-2006 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * and distribute linked combinations including the two. You must obey the
\r
23 * GNU General Public License in all respects for all of the code used other than
\r
24 * the above mentioned libraries. If you modify this file, you may extend this
\r
25 * exception to your version of the file, but you are not obligated to do so.
\r
26 * If you do not wish to do so, delete this exception statement from your version.
\r
28 package mir.storage;
\r
30 import java.sql.Connection;
\r
31 import java.sql.ResultSet;
\r
35 * Class to generate update statements
\r
37 public class RecordRetriever extends StatementGenerator {
\r
38 public RecordRetriever(String aTableName, String aPrefix) {
\r
39 tableName = aTableName;
\r
41 if (prefix == null || "".equals(prefix)) {
\r
44 extraTables = new ArrayList();
\r
45 whereClause = new StringBuffer();
\r
46 orderByClause = new StringBuffer();
\r
48 appendQuery("SELECT ");
\r
51 public void addExtraTable(String aTableSpecification) {
\r
52 extraTables.add(aTableSpecification);
\r
55 public void addField(String aField) {
\r
63 appendQuery(prefix);
\r
67 appendQuery(aField);
\r
70 public void appendStringParameter(String aValue) {
\r
71 appendParameter(aValue);
\r
74 public void appendWhereClause(String aPart) {
\r
75 whereClause.append(aPart);
\r
78 public void appendOrderByClause(String aPart) {
\r
79 orderByClause.append(aPart);
\r
82 public void setLimit(int aLimit) {
\r
86 public void setOffset(int anOffset) {
\r
91 * Executes the statement. Returns then number of records
\r
94 public ResultSet execute(Connection aConnection) throws DatabaseFailure {
\r
96 appendQuery("FROM ");
\r
97 appendQuery(tableName);
\r
100 appendQuery(prefix);
\r
103 Iterator i = extraTables.iterator();
\r
104 while (i.hasNext()) {
\r
106 appendQuery((String) i.next());
\r
109 if (whereClause.toString().trim().length()>0) {
\r
111 appendQuery("WHERE ");
\r
112 appendQuery(whereClause.toString());
\r
115 if (orderByClause.toString().trim().length()>0) {
\r
117 appendQuery("ORDER BY ");
\r
118 appendQuery(orderByClause.toString());
\r
123 appendQuery("LIMIT ");
\r
124 appendQuery(Integer.toString(limit));
\r
129 appendQuery("OFFSET ");
\r
130 appendQuery(Integer.toString(offset));
\r
133 return executeWithResultSet(aConnection);
\r
136 private String tableName;
\r
137 private String prefix;
\r
138 private List extraTables;
\r
139 private boolean firstField = true;
\r
140 private StringBuffer whereClause;
\r
141 private StringBuffer orderByClause;
\r
142 private int limit = -1;
\r
143 private int offset = -1;
\r
145 private static final String CRLF = "\n\r";
\r