Author: bodewig
Date: Mon Aug 17 15:46:09 2009
New Revision: 805014
URL: http://svn.apache.org/viewvc?rev=805014&view=rev
Log:
allow setting of arbitrary JDBC connection properties. PR 33452
Modified:
ant/core/trunk/WHATSNEW
ant/core/trunk/docs/manual/CoreTasks/sql.html
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java
Modified: ant/core/trunk/WHATSNEW
URL:
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=805014&r1=805013&r2=805014&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Mon Aug 17 15:46:09 2009
@@ -832,6 +832,10 @@
* Ant now builds against commons-net 2.0 as well.
Bugzilla Report 47669.
+ * A new nested element connectionProperty of <sql> allows setting of
+ arbitrary JDBC connection properties.
+ Bugzilla Report 33452.
+
Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================
Modified: ant/core/trunk/docs/manual/CoreTasks/sql.html
URL:
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/sql.html?rev=805014&r1=805013&r2=805014&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/sql.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/sql.html Mon Aug 17 15:46:09 2009
@@ -296,6 +296,29 @@
href="../using.html#path">PATH like structure</a> and can also be set via a
nested
<em>classpath</em> element. It is used to load the JDBC classes.</p>
+<h4>connectionProperty</h4>
+<p><em>Since Ant 1.8.0</em></p>
+<p>Use nested <code><connectionProperty></code> elements to
+ specify additional JDBC properties that need to be set when
+ connecting to the database.</p>
+<table border="1" cellpadding="2" cellspacing="0">
+ <tr>
+ <td valign="top"><b>Attribute</b></td>
+ <td valign="top"><b>Description</b></td>
+ <td align="center" valign="top"><b>Required</b></td>
+ </tr>
+ <tr>
+ <td valign="top">name</td>
+ <td valign="top">Name of the property</td>
+ <td valign="top" align="center">Yes</td>
+ </tr>
+ <tr>
+ <td valign="top">value</td>
+ <td valign="top">Value of the property</td>
+ <td valign="top" align="center">Yes</td>
+ </tr>
+</table>
+
<h3>Examples</h3>
<blockquote><pre><sql
driver="org.database.jdbcDriver"
@@ -315,6 +338,21 @@
url="jdbc:database-url"
userid="sa"
password="pass"
+ src="data.sql">
+ <connectionProperty name="internal_logon"
value="SYSDBA">
+</sql>
+</pre></blockquote>
+
+<p>Connects to the database given in <i>url</i> as the sa user using
+the org.database.jdbcDriver and executes the SQL statements contained
+within the file data.sql. Also sets the
+property <i>internal_logon</i> to the value <i>SYSDBA</i>.</p>
+
+<blockquote><pre><sql
+ driver="org.database.jdbcDriver"
+ url="jdbc:database-url"
+ userid="sa"
+ password="pass"
>
insert
into table some_table
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java?rev=805014&r1=805013&r2=805014&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java Mon Aug
17 15:46:09 2009
@@ -22,9 +22,12 @@
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;
@@ -149,6 +152,13 @@
private boolean failOnConnectionError = true;
/**
+ * Additional properties to put into the JDBC connection string.
+ *
+ * @since Ant 1.8.0
+ */
+ private List/*<Property>*/ connectionProperties = new ArrayList();
+
+ /**
* Sets the classpath for loading the driver.
* @param classpath The classpath to set
*/
@@ -306,6 +316,15 @@
}
/**
+ * 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 @@
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) {