reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / storage / RecordRetriever.java
1 /*\r
2  * Copyright (C) 2001-2006 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  * 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
27  */\r
28 package mir.storage;\r
29 \r
30 import java.sql.Connection;\r
31 import java.sql.ResultSet;\r
32 import java.util.*;\r
33 \r
34 /**\r
35  * Class to generate update statements\r
36  */\r
37 public class RecordRetriever extends StatementGenerator {\r
38   public RecordRetriever(String aTableName, String aPrefix) {\r
39     tableName = aTableName;\r
40     prefix = aPrefix;\r
41     if (prefix == null || "".equals(prefix)) {\r
42       prefix = null;\r
43     }\r
44     extraTables = new ArrayList();\r
45     whereClause = new StringBuffer();\r
46     orderByClause = new StringBuffer();\r
47 \r
48     appendQuery("SELECT ");\r
49   }\r
50 \r
51   public void addExtraTable(String aTableSpecification) {\r
52     extraTables.add(aTableSpecification);\r
53   }\r
54 \r
55   public void addField(String aField) {\r
56     if (firstField) {\r
57       firstField = false;\r
58     }\r
59     else {\r
60       appendQuery(", ");\r
61     }\r
62     if (prefix!=null) {\r
63       appendQuery(prefix);\r
64       appendQuery(".");\r
65     }\r
66 \r
67     appendQuery(aField);\r
68   }\r
69 \r
70   public void appendStringParameter(String aValue) {\r
71     appendParameter(aValue);\r
72   }\r
73 \r
74   public void appendWhereClause(String aPart) {\r
75     whereClause.append(aPart);\r
76   }\r
77 \r
78   public void appendOrderByClause(String aPart) {\r
79     orderByClause.append(aPart);\r
80   }\r
81 \r
82   public void setLimit(int aLimit) {\r
83     limit = aLimit;\r
84   }\r
85 \r
86   public void setOffset(int anOffset) {\r
87     offset = anOffset;\r
88   }\r
89 \r
90   /**\r
91    * Executes the statement. Returns then number of records\r
92    * changed\r
93    */\r
94   public ResultSet execute(Connection aConnection) throws DatabaseFailure {\r
95     appendQuery(CRLF);\r
96     appendQuery("FROM ");\r
97     appendQuery(tableName);\r
98     if (prefix!=null) {\r
99       appendQuery(" ");\r
100       appendQuery(prefix);\r
101     }\r
102 \r
103     Iterator i = extraTables.iterator();\r
104     while (i.hasNext()) {\r
105       appendQuery(", ");\r
106       appendQuery((String) i.next());\r
107     }\r
108 \r
109     if (whereClause.toString().trim().length()>0) {\r
110       appendQuery(CRLF);\r
111       appendQuery("WHERE ");\r
112       appendQuery(whereClause.toString());\r
113     }\r
114 \r
115     if (orderByClause.toString().trim().length()>0) {\r
116       appendQuery(CRLF);\r
117       appendQuery("ORDER BY ");\r
118       appendQuery(orderByClause.toString());\r
119     }\r
120 \r
121     if (limit > -1) {\r
122       appendQuery(CRLF);\r
123       appendQuery("LIMIT ");\r
124       appendQuery(Integer.toString(limit));\r
125     }\r
126 \r
127     if (offset > -1) {\r
128       appendQuery(CRLF);\r
129       appendQuery("OFFSET ");\r
130       appendQuery(Integer.toString(offset));\r
131     }\r
132 \r
133     return executeWithResultSet(aConnection);\r
134   }\r
135 \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
144 \r
145   private static final String CRLF = "\n\r";\r
146 }