reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / storage / StatementGenerator.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 mir.log.LoggerWrapper;\r
31 \r
32 import java.sql.Connection;\r
33 import java.sql.PreparedStatement;\r
34 import java.sql.ResultSet;\r
35 import java.util.ArrayList;\r
36 import java.util.Iterator;\r
37 import java.util.List;\r
38 \r
39 /**\r
40  * Base class for (sql) statement generators\r
41  */\r
42 public class StatementGenerator {\r
43   private static LoggerWrapper logger = new LoggerWrapper("Database.Statements");\r
44 \r
45   public StatementGenerator() {\r
46     query = new StringBuffer();\r
47     parameters = new ArrayList();\r
48   }\r
49 \r
50   protected void appendQuery(String aPart) {\r
51     query.append(aPart);\r
52   }\r
53 \r
54   protected void appendParameter(Object aParameter) {\r
55     parameters.add(aParameter);\r
56   }\r
57 \r
58   /**\r
59     * Executes the statement. Returns the number of modified records.\r
60     */\r
61   protected int executeWithModifiedCount(Connection aConnection) throws DatabaseFailure {\r
62     long start = System.currentTimeMillis();\r
63 \r
64     try {\r
65       aConnection.setAutoCommit(false);\r
66       logQueryBefore(query.toString());\r
67 \r
68       PreparedStatement statement = aConnection.prepareStatement(query.toString());\r
69 \r
70       int index = 1;\r
71       Iterator i = parameters.iterator();\r
72 \r
73       while (i.hasNext()) {\r
74         statement.setObject(index, i.next());\r
75         index++;\r
76       }\r
77 \r
78       int result = statement.executeUpdate();\r
79 \r
80       logQueryAfter(query.toString(), System.currentTimeMillis() - start);\r
81 \r
82       return result;\r
83     }\r
84     catch (Throwable e) {\r
85       logQueryError(query.toString(), System.currentTimeMillis() - start, e);\r
86       System.out.println(e.toString());\r
87 \r
88       throw new DatabaseFailure(e);\r
89     }\r
90     finally {\r
91       try {\r
92         aConnection.setAutoCommit(true);\r
93       }\r
94       catch (Exception e) {\r
95       }\r
96     }\r
97   }\r
98 \r
99   /**\r
100     * Executes the statement. Returns a resultset.\r
101     */\r
102   protected ResultSet executeWithResultSet(Connection aConnection) throws DatabaseFailure {\r
103     long start = System.currentTimeMillis();\r
104 \r
105     try {\r
106       logQueryBefore(query.toString());\r
107 \r
108       PreparedStatement statement = aConnection.prepareStatement(query.toString());\r
109 \r
110       int index = 1;\r
111       Iterator i = parameters.iterator();\r
112 \r
113       while (i.hasNext()) {\r
114         statement.setObject(index, i.next());\r
115         index++;\r
116       }\r
117 \r
118       ResultSet result = statement.executeQuery();\r
119 \r
120       logQueryAfter(query.toString(), System.currentTimeMillis() - start);\r
121 \r
122       return result;\r
123     }\r
124     catch (Throwable e) {\r
125       logQueryError(query.toString(), System.currentTimeMillis() - start, e);\r
126       System.out.println(e.toString());\r
127 \r
128       throw new DatabaseFailure(e);\r
129     }\r
130   }\r
131 \r
132   private void logQueryBefore(String aQuery) {\r
133     logger.debug("about to perform QUERY " + aQuery);\r
134   }\r
135 \r
136   private void logQueryAfter(String aQuery, long aTime) {\r
137     logger.info("QUERY " + aQuery + " took " + aTime + "ms.");\r
138   }\r
139 \r
140   private void logQueryError(String aQuery, long aTime, Throwable anException) {\r
141     logger.error("QUERY " + aQuery + " took " + aTime + "ms, but threw exception " + anException.toString());\r
142   }\r
143 \r
144 \r
145   private StringBuffer query;\r
146   private List parameters;\r
147 }\r