2 * Copyright (C) 2001-2006 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 * 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
28 package mir.storage;
\r
30 import mir.log.LoggerWrapper;
\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
40 * Base class for (sql) statement generators
\r
42 public class StatementGenerator {
\r
43 private static LoggerWrapper logger = new LoggerWrapper("Database.Statements");
\r
45 public StatementGenerator() {
\r
46 query = new StringBuffer();
\r
47 parameters = new ArrayList();
\r
50 protected void appendQuery(String aPart) {
\r
51 query.append(aPart);
\r
54 protected void appendParameter(Object aParameter) {
\r
55 parameters.add(aParameter);
\r
59 * Executes the statement. Returns the number of modified records.
\r
61 protected int executeWithModifiedCount(Connection aConnection) throws DatabaseFailure {
\r
62 long start = System.currentTimeMillis();
\r
65 aConnection.setAutoCommit(false);
\r
66 logQueryBefore(query.toString());
\r
68 PreparedStatement statement = aConnection.prepareStatement(query.toString());
\r
71 Iterator i = parameters.iterator();
\r
73 while (i.hasNext()) {
\r
74 statement.setObject(index, i.next());
\r
78 int result = statement.executeUpdate();
\r
80 logQueryAfter(query.toString(), System.currentTimeMillis() - start);
\r
84 catch (Throwable e) {
\r
85 logQueryError(query.toString(), System.currentTimeMillis() - start, e);
\r
86 System.out.println(e.toString());
\r
88 throw new DatabaseFailure(e);
\r
92 aConnection.setAutoCommit(true);
\r
94 catch (Exception e) {
\r
100 * Executes the statement. Returns a resultset.
\r
102 protected ResultSet executeWithResultSet(Connection aConnection) throws DatabaseFailure {
\r
103 long start = System.currentTimeMillis();
\r
106 logQueryBefore(query.toString());
\r
108 PreparedStatement statement = aConnection.prepareStatement(query.toString());
\r
111 Iterator i = parameters.iterator();
\r
113 while (i.hasNext()) {
\r
114 statement.setObject(index, i.next());
\r
118 ResultSet result = statement.executeQuery();
\r
120 logQueryAfter(query.toString(), System.currentTimeMillis() - start);
\r
124 catch (Throwable e) {
\r
125 logQueryError(query.toString(), System.currentTimeMillis() - start, e);
\r
126 System.out.println(e.toString());
\r
128 throw new DatabaseFailure(e);
\r
132 private void logQueryBefore(String aQuery) {
\r
133 logger.debug("about to perform QUERY " + aQuery);
\r
136 private void logQueryAfter(String aQuery, long aTime) {
\r
137 logger.info("QUERY " + aQuery + " took " + aTime + "ms.");
\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
145 private StringBuffer query;
\r
146 private List parameters;
\r