You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2008/07/11 11:52:32 UTC

svn commit: r675894 - in /ant/core/trunk: CONTRIBUTORS WHATSNEW contributors.xml src/main/org/apache/tools/ant/taskdefs/SQLExec.java

Author: bodewig
Date: Fri Jul 11 02:52:31 2008
New Revision: 675894

URL: http://svn.apache.org/viewvc?rev=675894&view=rev
Log:
Allow subclasses to modify the connection and statement instances used by <sql> or to access the cached instances.  PR 27178.  Submitted by Mike Davis

Modified:
    ant/core/trunk/CONTRIBUTORS
    ant/core/trunk/WHATSNEW
    ant/core/trunk/contributors.xml
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java

Modified: ant/core/trunk/CONTRIBUTORS
URL: http://svn.apache.org/viewvc/ant/core/trunk/CONTRIBUTORS?rev=675894&r1=675893&r2=675894&view=diff
==============================================================================
Binary files - no diff available.

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=675894&r1=675893&r2=675894&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Fri Jul 11 02:52:31 2008
@@ -115,6 +115,11 @@
    running on OpenJDK.
    Bugzilla Report 44889.
 
+ * new protected getConnection and getStatement methods allow
+   subclasses of SQLExec more control - or access to the cached
+   instances when overriding other methods like runStatements.
+   Bugzilla Report 27178.
+
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================
 

Modified: ant/core/trunk/contributors.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/contributors.xml?rev=675894&r1=675893&r2=675894&view=diff
==============================================================================
--- ant/core/trunk/contributors.xml (original)
+++ ant/core/trunk/contributors.xml Fri Jul 11 02:52:31 2008
@@ -761,6 +761,10 @@
   </name>
   <name>
     <first>Mike</first>
+    <last>Davis</last>
+  </name>
+  <name>
+    <first>Mike</first>
     <last>Roberts</last>
   </name>
   <name>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java?rev=675894&r1=675893&r2=675894&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 Fri Jul 11 02:52:31 2008
@@ -435,14 +435,8 @@
             Transaction t = createTransaction();
             t.setSrc(srcFile);
             t.addText(sqlCommand);
-            conn = getConnection();
-            if (!isValidRdbms(conn)) {
-                return;
-            }
-            try {
-                statement = conn.createStatement();
-                statement.setEscapeProcessing(escapeProcessing);
 
+            try {
                 PrintStream out = System.out;
                 try {
                     if (output != null) {
@@ -458,7 +452,7 @@
                         ((Transaction) e.nextElement()).runTransaction(out);
                         if (!isAutocommit()) {
                             log("Committing transaction", Project.MSG_VERBOSE);
-                            conn.commit();
+                            getConnection().commit();
                         }
                     }
                 } finally {
@@ -476,15 +470,15 @@
                 }
             } finally {
                 try {
-                    if (statement != null) {
-                        statement.close();
+                    if (getStatement() != null) {
+                        getStatement().close();
                     }
                 } catch (SQLException ex) {
                     // ignore
                 }
                 try {
-                    if (conn != null) {
-                        conn.close();
+                    if (getConnection() != null) {
+                        getConnection().close();
                     }
                 } catch (SQLException ex) {
                     // ignore
@@ -575,9 +569,9 @@
             boolean ret;
             int updateCount = 0, updateCountTotal = 0;
 
-            ret = statement.execute(sql);
-            updateCount = statement.getUpdateCount();
-            resultSet = statement.getResultSet();
+            ret = getStatement().execute(sql);
+            updateCount = getStatement().getUpdateCount();
+            resultSet = getStatement().getResultSet();
             do {
                 if (!ret) {
                     if (updateCount != -1) {
@@ -586,10 +580,10 @@
                 } else if (print) {
                     printResults(resultSet, out);
                 }
-                ret = statement.getMoreResults();
+                ret = getStatement().getMoreResults();
                 if (ret) {
-                    updateCount = statement.getUpdateCount();
-                    resultSet = statement.getResultSet();
+                    updateCount = getStatement().getUpdateCount();
+                    resultSet = getStatement().getResultSet();
                 }
             } while (ret);
 
@@ -598,12 +592,12 @@
             if (print && showtrailers) {
                 out.println(updateCountTotal + " rows affected");
             }
-            SQLWarning warning = conn.getWarnings();
+            SQLWarning warning = getConnection().getWarnings();
             while (warning != null) {
                 log(warning + " sql warning", Project.MSG_VERBOSE);
                 warning = warning.getNextWarning();
             }
-            conn.clearWarnings();
+            getConnection().clearWarnings();
             goodSql++;
         } catch (SQLException e) {
             log("Failed to execute: " + sql, Project.MSG_ERR);
@@ -633,7 +627,7 @@
      * @throws SQLException on SQL problems.
      */
     protected void printResults(PrintStream out) throws SQLException {
-        ResultSet rs = statement.getResultSet();
+        ResultSet rs = getStatement().getResultSet();
         try {
             printResults(rs, out);
         } finally {
@@ -692,15 +686,52 @@
      * @since Ant 1.7
      */
     private void closeQuietly() {
-        if (!isAutocommit() && conn != null && onError.equals("abort")) {
+        if (!isAutocommit() && getConnection() != null && onError.equals("abort")) {
             try {
-                conn.rollback();
+                getConnection().rollback();
             } catch (SQLException ex) {
                 // ignore
             }
         }
     }
 
+
+    /**
+     * Caches the connection returned by the base class's getConnection method.
+     *
+     * <p>Subclasses that need to provide a different connection than
+     * the base class would, should override this method but keep in
+     * mind that this class expects to get the same connection
+     * instance on consecutive calls.</p>
+     */
+    protected Connection getConnection() {
+        if (conn == null) {
+            conn = super.getConnection();
+            if (!isValidRdbms(conn)) {
+                conn = null;
+            }
+        }
+        return conn;
+    }
+
+    /**
+     * Creates and configures a Statement instance which is then
+     * cached for subsequent calls.
+     *
+     * <p>Subclasses that want to provide different Statement
+     * instances, should override this method but keep in mind that
+     * this class expects to get the same connection instance on
+     * consecutive calls.</p>
+     */
+    protected Statement getStatement() throws SQLException {
+        if (statement == null) {
+            statement = getConnection().createStatement();
+            statement.setEscapeProcessing(escapeProcessing);
+        }
+
+        return statement;
+    }
+        
     /**
      * The action a task should perform on an error,
      * one of "continue", "stop" and "abort"