ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r349640 - in /ant/core/trunk: docs/manual/CoreTasks/sql.html src/main/org/apache/tools/ant/taskdefs/SQLExec.java
Date Tue, 29 Nov 2005 04:46:51 GMT
Author: bodewig
Date: Mon Nov 28 20:46:45 2005
New Revision: 349640

URL: http://svn.apache.org/viewcvs?rev=349640&view=rev
Log:
support for arbitrary resource collections in <sql>

Modified:
    ant/core/trunk/docs/manual/CoreTasks/sql.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java

Modified: ant/core/trunk/docs/manual/CoreTasks/sql.html
URL: http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/CoreTasks/sql.html?rev=349640&r1=349639&r2=349640&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/sql.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/sql.html Mon Nov 28 20:46:45 2005
@@ -171,12 +171,20 @@
     <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
   </tr>
 </table>
-<h4>fileset</h4>
-<p>You can specify multiple source files via nested <a
-href="../CoreTypes/fileset.html">fileset</a> 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.</p>
+<p>The <code>&lt;transaction&gt;</code> element supports any <a
+href="../CoreTypes/resources.html">resource</a> or single element
+resource collection as nested element to specify the resource
+containing the SQL statements.</p>
+
+<h4>any <a href="../CoreTypes/resources.html">resource</a> or resource
+collection</h4>
+
+<p>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. </p>
+
 <h4>classpath</h4>
 <p><code>Sql</code>'s <em>classpath</em> attribute is a <a
 href="../using.html#path">PATH like structure</a> and can also be set via a nested
@@ -260,9 +268,11 @@
     url=&quot;jdbc:database-url&quot;
     userid=&quot;sa&quot;
     password=&quot;pass&quot;&gt;
-  &lt;fileset dir=&quot;.&quot;&gt;
-    &lt;include name=&quot;data*.sql&quot;/&gt;
-  &lt;/fileset&gt;
+  &lt;path&gt;
+    &lt;fileset dir=&quot;.&quot;&gt;
+      &lt;include name=&quot;data*.sql&quot;/&gt;
+    &lt;/fileset&gt;
+  &lt;path&gt;
   &lt;transaction&gt;
     truncate table some_other_table;
   &lt;/transaction&gt;

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java?rev=349640&r1=349639&r2=349640&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java Mon Nov 28 20:46:45
2005
@@ -20,9 +20,14 @@
 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.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 @@
     /**
      * files to load
      */
-    private Vector filesets = new Vector();
+    private Union resources = new Union();
 
     /**
      * SQL statement
@@ -200,9 +205,18 @@
      *            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 @@
 
         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 @@
                 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 @@
      * operation in between.
      */
     public class Transaction {
-        private File tSrcFile = null;
+        private Resource tSrcResource = null;
         private String tSqlCommand = "";
 
         /**
@@ -656,7 +665,19 @@
          * @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;
         }
 
         /**
@@ -668,6 +689,18 @@
         }
 
         /**
+         * 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());
+        }
+
+        /**
          *
          */
         private void runTransaction(PrintStream out)
@@ -677,18 +710,20 @@
                 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);
                 }
             }
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message