001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo.tools.ant;
012:
013: import org.apache.tools.ant.BuildException;
014:
015: import java.io.*;
016: import java.sql.Connection;
017: import java.sql.Statement;
018: import java.sql.SQLException;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021:
022: import com.versant.core.common.BindingSupportImpl;
023: import com.versant.core.jdbc.JdbcStorageManagerFactory;
024:
025: /**
026: * This task will load a sql script file or/and execute a sql
027: * command in the body of the xml file.
028: */
029: public class ExecuteScriptTask extends JdoTaskBase {
030:
031: private String scriptFile;
032: private String sqlCommand = "";
033:
034: /**
035: * Set an inline SQL command to execute.
036: * NB: Properties are not expanded in this text.
037: */
038: public void addText(String sql) {
039: this .sqlCommand += sql;
040: }
041:
042: public void setDatastore(String datastore) {
043: // ignore - property kept for compatibility with old versions
044: }
045:
046: public void setScriptFile(String scriptFile) {
047: this .scriptFile = scriptFile;
048: }
049:
050: public void execute() throws BuildException {
051: super .execute();
052: File file = null;
053:
054: if (isSqlCommandEmpty() && scriptFile == null) {
055: throw BindingSupportImpl
056: .getInstance()
057: .illegalArgument(
058: "scriptFile property or sql statement must be set!");
059: }
060:
061: if (scriptFile != null) {
062: file = new File(scriptFile);
063: if (!file.exists()) {
064: throw BindingSupportImpl.getInstance().illegalArgument(
065: "File " + file.toString() + " does not exist");
066: }
067: }
068:
069: try {
070: if (!isSqlCommandEmpty()) {
071: runScript(sqlCommand);
072: }
073:
074: if (file != null) {
075: runScript(getText(file));
076: }
077: } catch (Exception e) {
078: throw new BuildException(e);
079: }
080: }
081:
082: public void runScript(String script) throws Exception {
083: Connection con = null;
084: JdbcStorageManagerFactory smf = (JdbcStorageManagerFactory) getSmf();
085: try {
086: con = smf.getConnectionSource().getConnection(false, true);
087: SQLScriptParser shredder = new SQLScriptParser();
088: ArrayList list = shredder.parse(script, true);
089: for (Iterator iter = list.iterator(); iter.hasNext();) {
090: SQLScriptParser.SQLScriptPart scriptPart = (SQLScriptParser.SQLScriptPart) iter
091: .next();
092: log("Executing: " + scriptPart.getSql());
093: Statement stat = null;
094: try {
095: stat = con.createStatement();
096: stat.execute(scriptPart.getSql());
097: } finally {
098: try {
099: stat.close();
100: } catch (SQLException e) {
101: //hide
102: }
103: }
104: }
105: } finally {
106: if (con != null) {
107: try {
108: smf.getConnectionSource().returnConnection(con);
109: } catch (SQLException e) {
110: // ignore
111: }
112: }
113: }
114: }
115:
116: private String getText(File file) throws IOException {
117: int size = (int) file.length();
118: int chars_read = 0;
119: FileReader in = new FileReader(file);
120: char[] data = new char[size];
121: while (in.ready()) {
122: chars_read += in.read(data, chars_read, size - chars_read);
123: }
124: in.close();
125: return new String(data, 0, chars_read);
126: }
127:
128: private boolean isSqlCommandEmpty() {
129: return sqlCommand.trim().equals("");
130: }
131: }
|