001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.ant;
024:
025: import java.io.File;
026: import java.io.FileReader;
027: import java.io.IOException;
028: import java.io.StringReader;
029: import java.sql.SQLException;
030:
031: import org.apache.tools.ant.BuildException;
032: import org.apache.tools.ant.Task;
033:
034: import biz.hammurapi.sql.SQLProcessor;
035:
036: /**
037: * Executes SQL script
038: * @author Pavel Vlasov
039: *
040: * @version $Revision: 1.1 $
041: */
042: public class Script extends Task {
043: private String script = "";
044: private String delimiter = ";";
045: private ConnectionEntry connectionEntry;
046:
047: /**
048: * Connection
049: * @ant.non-required
050: * @param connectionEntry
051: */
052: public void addConfiguredConnection(ConnectionEntry connectionEntry) {
053: this .connectionEntry = connectionEntry;
054: }
055:
056: /**
057: * Statements delimiter (single character). Defaults to ';'
058: * @ant.non-required.
059: * @param delimiter
060: */
061: public void setDelimiter(String delimiter) {
062: this .delimiter = delimiter;
063: }
064:
065: /**
066: * Script text. Required if file is not set.
067: * @ant.non-required
068: * @param text
069: */
070: public void addText(String text) {
071: script += text;
072: }
073:
074: private File file;
075:
076: /**
077: * Script file. Required if there is no nested text.
078: * File content is executed after text.
079: * @ant.non-required
080: * @param text
081: */
082: public void setFile(File file) {
083: this .file = file;
084: }
085:
086: public void execute(SQLProcessor processor) throws IOException,
087: SQLException {
088: if (script.trim().length() > 0) {
089: processor.executeScript(new StringReader(script), delimiter
090: .charAt(0));
091: }
092:
093: if (file != null) {
094: processor.executeScript(new FileReader(file), delimiter
095: .charAt(0));
096: }
097: }
098:
099: public void execute() throws BuildException {
100: try {
101: execute(new SQLProcessor(connectionEntry.getDataSource(),
102: null));
103: } catch (IOException e) {
104: throw new BuildException(e);
105: } catch (SQLException e) {
106: throw new BuildException(e);
107: }
108: }
109: }
|