2 * Copyright (C) 2005 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 * 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
30 package mir.storage;
\r
32 import mir.log.LoggerWrapper;
\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
42 * Base class for (sql) statement generators
\r
44 public class StatementGenerator {
\r
45 private static LoggerWrapper logger = new LoggerWrapper("Database.Statements");
\r
47 public StatementGenerator() {
\r
48 query = new StringBuffer();
\r
49 parameters = new ArrayList();
\r
52 protected void appendQuery(String aPart) {
\r
53 query.append(aPart);
\r
56 protected void appendParameter(Object aParameter) {
\r
57 parameters.add(aParameter);
\r
61 * Executes the statement. Returns the number of modified records.
\r
63 protected int executeWithModifiedCount(Connection aConnection) throws DatabaseFailure {
\r
64 long start = System.currentTimeMillis();
\r
67 aConnection.setAutoCommit(false);
\r
68 logQueryBefore(query.toString());
\r
70 PreparedStatement statement = aConnection.prepareStatement(query.toString());
\r
73 Iterator i = parameters.iterator();
\r
75 while (i.hasNext()) {
\r
76 statement.setObject(index, i.next());
\r
80 int result = statement.executeUpdate();
\r
82 logQueryAfter(query.toString(), System.currentTimeMillis() - start);
\r
86 catch (Throwable e) {
\r
87 logQueryError(query.toString(), System.currentTimeMillis() - start, e);
\r
88 System.out.println(e.toString());
\r
90 throw new DatabaseFailure(e);
\r
94 aConnection.setAutoCommit(true);
\r
96 catch (Exception e) {
\r
102 * Executes the statement. Returns a resultset.
\r
104 protected ResultSet executeWithResultSet(Connection aConnection) throws DatabaseFailure {
\r
105 long start = System.currentTimeMillis();
\r
108 logQueryBefore(query.toString());
\r
110 PreparedStatement statement = aConnection.prepareStatement(query.toString());
\r
113 Iterator i = parameters.iterator();
\r
115 while (i.hasNext()) {
\r
116 statement.setObject(index, i.next());
\r
120 ResultSet result = statement.executeQuery();
\r
122 logQueryAfter(query.toString(), System.currentTimeMillis() - start);
\r
126 catch (Throwable e) {
\r
127 logQueryError(query.toString(), System.currentTimeMillis() - start, e);
\r
128 System.out.println(e.toString());
\r
130 throw new DatabaseFailure(e);
\r
134 private void logQueryBefore(String aQuery) {
\r
135 logger.debug("about to perform QUERY " + aQuery);
\r
138 private void logQueryAfter(String aQuery, long aTime) {
\r
139 logger.info("QUERY " + aQuery + " took " + aTime + "ms.");
\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
147 private StringBuffer query;
\r
148 private List parameters;
\r