5b3f098b539f9d644e987c580fa0b571e535c28b
[mir.git] / source / mir / storage / RecordRetriever.java
1 /*\r
2  * Copyright (C) 2005 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 package mir.storage;\r
31 \r
32 import java.sql.Connection;\r
33 import java.sql.ResultSet;\r
34 import java.util.*;\r
35 \r
36 /**\r
37  * Class to generate update statements\r
38  */\r
39 public class RecordRetriever extends StatementGenerator {\r
40   public RecordRetriever(String aTableName, String aPrefix) {\r
41     tableName = aTableName;\r
42     prefix = aPrefix;\r
43     if (prefix == null || "".equals(prefix)) {\r
44       prefix = null;\r
45     }\r
46     extraTables = new ArrayList();\r
47     whereClause = new StringBuffer();\r
48     orderByClause = new StringBuffer();\r
49 \r
50     appendQuery("SELECT ");\r
51   }\r
52 \r
53   public void addExtraTable(String aTableSpecification) {\r
54     extraTables.add(aTableSpecification);\r
55   }\r
56 \r
57   public void addField(String aField) {\r
58     if (firstField) {\r
59       firstField = false;\r
60     }\r
61     else {\r
62       appendQuery(", ");\r
63     }\r
64     if (prefix!=null) {\r
65       appendQuery(prefix);\r
66       appendQuery(".");\r
67     }\r
68 \r
69     appendQuery(aField);\r
70   }\r
71 \r
72   public void appendStringParameter(String aValue) {\r
73     appendParameter(aValue);\r
74   }\r
75 \r
76   public void appendWhereClause(String aPart) {\r
77     whereClause.append(aPart);\r
78   }\r
79 \r
80   public void appendOrderByClause(String aPart) {\r
81     orderByClause.append(aPart);\r
82   }\r
83 \r
84   public void setLimit(int aLimit) {\r
85     limit = aLimit;\r
86   }\r
87 \r
88   public void setOffset(int anOffset) {\r
89     offset = anOffset;\r
90   }\r
91 \r
92   /**\r
93    * Executes the statement. Returns then number of records\r
94    * changed\r
95    */\r
96   public ResultSet execute(Connection aConnection) throws DatabaseFailure {\r
97     appendQuery(CRLF);\r
98     appendQuery("FROM ");\r
99     appendQuery(tableName);\r
100     if (prefix!=null) {\r
101       appendQuery(" ");\r
102       appendQuery(prefix);\r
103     }\r
104 \r
105     Iterator i = extraTables.iterator();\r
106     while (i.hasNext()) {\r
107       appendQuery(", ");\r
108       appendQuery((String) i.next());\r
109     }\r
110 \r
111     if (whereClause.toString().trim().length()>0) {\r
112       appendQuery(CRLF);\r
113       appendQuery("WHERE ");\r
114       appendQuery(whereClause.toString());\r
115     }\r
116 \r
117     if (orderByClause.toString().trim().length()>0) {\r
118       appendQuery(CRLF);\r
119       appendQuery("ORDER BY ");\r
120       appendQuery(orderByClause.toString());\r
121     }\r
122 \r
123     if (limit > -1) {\r
124       appendQuery(CRLF);\r
125       appendQuery("LIMIT ");\r
126       appendQuery(Integer.toString(limit));\r
127     }\r
128 \r
129     if (offset > -1) {\r
130       appendQuery(CRLF);\r
131       appendQuery("OFFSET ");\r
132       appendQuery(Integer.toString(offset));\r
133     }\r
134 \r
135     return executeWithResultSet(aConnection);\r
136   }\r
137 \r
138   private String tableName;\r
139   private String prefix;\r
140   private List extraTables;\r
141   private boolean firstField = true;\r
142   private StringBuffer whereClause;\r
143   private StringBuffer orderByClause;\r
144   private int limit = -1;\r
145   private int offset = -1;\r
146 \r
147   private static final String CRLF = "\n\r";\r
148 }