From 75126703fa23e343eb404024a281ecbadf7aef0b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 17 Aug 2009 15:46:09 +0000 Subject: [PATCH] allow setting of arbitrary JDBC connection properties. PR 33452 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@805014 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++ docs/manual/CoreTasks/sql.html | 38 +++++++++++++++++++ .../apache/tools/ant/taskdefs/JDBCTask.java | 37 +++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/WHATSNEW b/WHATSNEW index d16197e51..0a785878e 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -832,6 +832,10 @@ Other changes: * Ant now builds against commons-net 2.0 as well. Bugzilla Report 47669. + * A new nested element connectionProperty of allows setting of + arbitrary JDBC connection properties. + Bugzilla Report 33452. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/sql.html b/docs/manual/CoreTasks/sql.html index e00327553..923fea326 100644 --- a/docs/manual/CoreTasks/sql.html +++ b/docs/manual/CoreTasks/sql.html @@ -296,6 +296,29 @@ of transactions.

href="../using.html#path">PATH like structure and can also be set via a nested classpath element. It is used to load the JDBC classes.

+

connectionProperty

+

Since Ant 1.8.0

+

Use nested <connectionProperty> elements to + specify additional JDBC properties that need to be set when + connecting to the database.

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
nameName of the propertyYes
valueValue of the propertyYes
+

Examples

<sql
     driver="org.database.jdbcDriver"
@@ -310,6 +333,21 @@ href="../using.html#path">PATH like structure and can also be set via a nest
 org.database.jdbcDriver and executes the SQL statements contained within 
 the file data.sql

+
<sql
+    driver="org.database.jdbcDriver"
+    url="jdbc:database-url"
+    userid="sa"
+    password="pass"
+    src="data.sql">
+  <connectionProperty name="internal_logon" value="SYSDBA">
+</sql>
+
+ +

Connects to the database given in url as the sa user using +the org.database.jdbcDriver and executes the SQL statements contained +within the file data.sql. Also sets the +property internal_logon to the value SYSDBA.

+
<sql
     driver="org.database.jdbcDriver"
     url="jdbc:database-url"
diff --git a/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java b/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java
index 974c3e4bd..c0275a7cc 100644
--- a/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java
@@ -22,9 +22,12 @@ import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.Driver;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Hashtable;
-import java.util.Properties;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
+import java.util.Properties;
 
 import org.apache.tools.ant.AntClassLoader;
 import org.apache.tools.ant.BuildException;
@@ -148,6 +151,13 @@ public abstract class JDBCTask extends Task {
      */
     private boolean failOnConnectionError = true;
 
+    /**
+     * Additional properties to put into the JDBC connection string.
+     *
+     * @since Ant 1.8.0
+     */
+    private List/**/ connectionProperties = new ArrayList();
+
     /**
      * Sets the classpath for loading the driver.
      * @param classpath The classpath to set
@@ -305,6 +315,15 @@ public abstract class JDBCTask extends Task {
         return loader;
     }
 
+    /**
+     * Additional properties to put into the JDBC connection string.
+     *
+     * @since Ant 1.8.0
+     */
+    public void addConnectionProperty(Property var) {
+        connectionProperties.add(var);
+    }
+
     /**
      * Creates a new Connection as using the driver, url, userid and password
      * specified.
@@ -332,6 +351,22 @@ public abstract class JDBCTask extends Task {
             Properties info = new Properties();
             info.put("user", getUserId());
             info.put("password", getPassword());
+
+            for (Iterator props = connectionProperties.iterator();
+                 props.hasNext(); ) {
+                Property p = (Property) props.next();
+                String name = p.getName();
+                String value = p.getValue();
+                if (name == null || value == null) {
+                    log("Only name/value pairs are supported as connection"
+                        + " properties.", Project.MSG_WARN);
+                } else {
+                    log("Setting connection property " + name + " to " + value,
+                        Project.MSG_VERBOSE);
+                    info.put(name, value);
+                }
+            }
+
             Connection conn = getDriver().connect(getUrl(), info);
 
             if (conn == null) {