001: /*
002: * $Id: BatchSqlCommandRunner.java,v 1.6 2006/01/10 21:02:37 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.tools;
042:
043: import java.io.BufferedReader;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047: import java.io.PrintWriter;
048: import java.sql.Connection;
049: import java.sql.ResultSet;
050: import java.sql.SQLException;
051: import java.sql.Statement;
052:
053: import org.apache.commons.logging.Log;
054: import org.apache.commons.logging.LogFactory;
055:
056: /**
057: * @author Chuck Burdick
058: * @author Jim Burke
059: */
060: public class BatchSqlCommandRunner {
061: public BatchSqlCommandRunner(Connection conn) throws SQLException {
062: this (conn.createStatement());
063: }
064:
065: public BatchSqlCommandRunner(Connection conn, PrintWriter pw)
066: throws SQLException {
067: this (conn.createStatement(), pw);
068: }
069:
070: public BatchSqlCommandRunner(Statement stmt) {
071: this (stmt, null);
072:
073: }
074:
075: public BatchSqlCommandRunner(Statement stmt, PrintWriter pw) {
076: _stmt = stmt;
077: if (pw == null) {
078: _writer = new PrintWriter(System.out, true);
079: ;
080: } else {
081: _writer = pw;
082: }
083: _report = new BaseReport(_writer);
084: }
085:
086: public void runCommands(BufferedReader reader) throws IOException,
087: SQLException {
088: try {
089: String cmd = null;
090: while (!(cmd = readCommand(reader)).equals("")) {
091: if (_log.isDebugEnabled()) {
092: _log.debug("executing command: " + cmd);
093: }
094: long startTime = System.currentTimeMillis();
095: boolean hasResultSet = _stmt.execute(cmd);
096: long endTime = System.currentTimeMillis();
097: if (hasResultSet) {
098: ResultSet rset = _stmt.getResultSet();
099: _report.reportResultSet(rset);
100: rset.close();
101: } else {
102: int ct = _stmt.getUpdateCount();
103: _report.reportUpdateCount(ct);
104: }
105: _writer.println("Execution time: "
106: + (endTime - startTime) + " ms.");
107: }
108: } catch (SQLException e) {
109: _report.reportException(e);
110: throw e;
111: } finally {
112: reader.close();
113: }
114: }
115:
116: public void runCommands(InputStream stream) throws IOException,
117: SQLException {
118: runCommands(new BufferedReader(new InputStreamReader(stream,
119: "UTF8")));
120: }
121:
122: public void close() {
123: try {
124: _stmt.close();
125: } catch (Exception e) {
126: }
127: }
128:
129: String readLine(BufferedReader reader) throws IOException {
130: String result = reader.readLine();
131: if (result != null) {
132: result.trim();
133: }
134: return result;
135: }
136:
137: String readCommand(BufferedReader reader) throws IOException {
138: _buf.setLength(0);
139: String line = null;
140: boolean done = false;
141: boolean inQuote = false;
142: while (!done && (line = readLine(reader)) != null) {
143: if (line.indexOf("/*") == -1) {
144: _buf.append(line);
145: _buf.append(' ');
146: inQuote = isInQuotes(line, inQuote);
147: done = (!inQuote && line.trim().endsWith(";"));
148: }
149: }
150: return _buf.toString().trim();
151: }
152:
153: /**
154: * loop through all the quotes in the line to see if we are within
155: * a string literal
156: */
157: boolean isInQuotes(String line, boolean inQuotes) {
158: boolean result = inQuotes;
159: int quotePos = -1;
160: int startPos = 0;
161: while ((quotePos = line.indexOf("'", startPos)) > -1) {
162: result = !result;
163: startPos = quotePos + 1;
164: }
165: return result;
166: }
167:
168: private static Log _log = LogFactory
169: .getLog(BatchSqlCommandRunner.class);
170: private StringBuffer _buf = new StringBuffer();
171: private Statement _stmt = null;
172: private PrintWriter _writer = null;
173: private BaseReport _report = null;
174: }
|