From 36e5ac64fd1e76c49877ce8bb5596589a2c55ddf Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 29 Nov 2005 04:46:45 +0000 Subject: [PATCH] support for arbitrary resource collections in git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@349640 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/sql.html | 28 ++++-- .../apache/tools/ant/taskdefs/SQLExec.java | 93 +++++++++++++------ 2 files changed, 83 insertions(+), 38 deletions(-) diff --git a/docs/manual/CoreTasks/sql.html b/docs/manual/CoreTasks/sql.html index ef55e6b1f..cd4a5373e 100644 --- a/docs/manual/CoreTasks/sql.html +++ b/docs/manual/CoreTasks/sql.html @@ -171,12 +171,20 @@ on the same schema.

Yes, unless statements enclosed within tags -

fileset

-

You can specify multiple source files via nested fileset elements. Each file of -the fileset will be run in a transaction of its own, the order by -which the files of a single fileset will be executed is not -defined.

+

The <transaction> element supports any resource or single element +resource collection as nested element to specify the resource +containing the SQL statements.

+ +

any resource or resource +collection

+ +

You can specify multiple sources via nested resource collection +elements. Each resource of the collection will be run in a +transaction of its own. Prior to Ant 1.7 only filesets were +supported. Use a sort resource collection to get a predictable order +of transactions.

+

classpath

Sql's classpath attribute is a PATH like structure and can also be set via a nested @@ -260,9 +268,11 @@ run before data2.sql.

url="jdbc:database-url" userid="sa" password="pass"> - <fileset dir="."> - <include name="data*.sql"/> - </fileset> + <path> + <fileset dir="."> + <include name="data*.sql"/> + </fileset> + <path> <transaction> truncate table some_other_table; </transaction> diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index aaf18a3b6..0106a1a73 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -20,9 +20,14 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; +import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.StringUtils; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.FileResource; +import org.apache.tools.ant.types.resources.Union; import java.io.File; import java.io.PrintStream; @@ -32,10 +37,10 @@ import java.io.IOException; import java.io.Reader; import java.io.BufferedReader; import java.io.StringReader; -import java.io.FileReader; +import java.io.InputStream; import java.io.InputStreamReader; -import java.io.FileInputStream; import java.util.Enumeration; +import java.util.Iterator; import java.util.StringTokenizer; import java.util.Vector; @@ -100,7 +105,7 @@ public class SQLExec extends JDBCTask { /** * files to load */ - private Vector filesets = new Vector(); + private Union resources = new Union(); /** * SQL statement @@ -200,9 +205,18 @@ public class SQLExec extends JDBCTask { * a separate transaction. */ public void addFileset(FileSet set) { - filesets.addElement(set); + add(set); } + /** + * Adds a collection of resources (nested element). + * @param set a collection of resources containing SQL commands, + * each resource is run in a separate transaction. + * @since Ant 1.7 + */ + public void add(ResourceCollection rc) { + resources.add(rc); + } /** * Add a SQL transaction to execute @@ -327,9 +341,10 @@ public class SQLExec extends JDBCTask { try { if (srcFile == null && sqlCommand.length() == 0 - && filesets.isEmpty()) { + && resources.size() == 0) { if (transactions.size() == 0) { - throw new BuildException("Source file or fileset, " + throw new BuildException("Source file or resource " + + "collection, " + "transactions or sql statement " + "must be set!", getLocation()); } @@ -339,19 +354,13 @@ public class SQLExec extends JDBCTask { throw new BuildException("Source file does not exist!", getLocation()); } - // deal with the filesets - for (int i = 0; i < filesets.size(); i++) { - FileSet fs = (FileSet) filesets.elementAt(i); - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - File srcDir = fs.getDir(getProject()); - - String[] srcFiles = ds.getIncludedFiles(); - - // Make a transaction for each file - for (int j = 0; j < srcFiles.length; j++) { - Transaction t = createTransaction(); - t.setSrc(new File(srcDir, srcFiles[j])); - } + // deal with the resources + Iterator iter = resources.iterator(); + while (iter.hasNext()) { + Resource r = (Resource) iter.next(); + // Make a transaction for each resource + Transaction t = createTransaction(); + t.setSrcResource(r); } // Make a transaction group for the outer command @@ -648,7 +657,7 @@ public class SQLExec extends JDBCTask { * operation in between. */ public class Transaction { - private File tSrcFile = null; + private Resource tSrcResource = null; private String tSqlCommand = ""; /** @@ -656,7 +665,19 @@ public class SQLExec extends JDBCTask { * @param src the source file */ public void setSrc(File src) { - this.tSrcFile = src; + setSrcResource(new FileResource(src)); + } + + /** + * Set the source file attribute. + * @param src the source file + * @since Ant 1.7 + */ + public void setSrcResource(Resource src) { + if (tSrcResource != null) { + throw new BuildException("only one resource per transaction"); + } + tSrcResource = src; } /** @@ -667,6 +688,18 @@ public class SQLExec extends JDBCTask { this.tSqlCommand += sql; } + /** + * Set the source resource. + * @since Ant 1.7 + */ + public void addConfigured(ResourceCollection a) { + if (a.size() != 1) { + throw new BuildException("only single argument resource " + + "collections are supported."); + } + setSrcResource((Resource) a.iterator().next()); + } + /** * */ @@ -677,18 +710,20 @@ public class SQLExec extends JDBCTask { runStatements(new StringReader(tSqlCommand), out); } - if (tSrcFile != null) { - log("Executing file: " + tSrcFile.getAbsolutePath(), + if (tSrcResource != null) { + log("Executing resource: " + tSrcResource.toString(), Project.MSG_INFO); - Reader reader = - (encoding == null) ? new FileReader(tSrcFile) - : new InputStreamReader( - new FileInputStream(tSrcFile), - encoding); + InputStream is = null; + Reader reader = null; try { + is = tSrcResource.getInputStream(); + reader = + (encoding == null) ? new InputStreamReader(is) + : new InputStreamReader(is, encoding); runStatements(reader, out); } finally { - reader.close(); + FileUtils.close(is); + FileUtils.close(reader); } } }