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