|
Ok. Here's a patch which allows the use of the
classpath to load the driver. I'm not 100% sure I did it the best way, but it
does seem to work whether using a <classpath>, <classpath refid>, or
using the system classpath.
The part I'm slightly concerned about is that I
don't use the DriverManager to get an instance of the driver, but instead
instantiate the driver using either the built in class loader or the
AntClassLoader depending on whether or not the user specifies a
classpath.
This patch also includes changes to remove tab
characters from the source, some more verbose messages, and also a patch to the
sql.html documentation.
Julian.
|
? lib
? bin
Index: docs/sql.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/sql.html,v
retrieving revision 1.1
diff -u -r1.1 sql.html
--- docs/sql.html 2000/07/28 08:57:29 1.1
+++ docs/sql.html 2000/09/27 01:09:33
@@ -47,6 +47,26 @@
<td width="78%" valign="top">Auto commit flag for database connection
(default false)</td>
<td width="10%" valign="top">No, default "false"</td>
</tr>
+<tr>
+ <td width="12%" valign="top">print</td>
+ <td width="78%" valign="top">Print result sets from the statements (default
false)</td>
+ <td width="10%" valign="top">No, default "false"</td>
+</tr>
+<tr>
+ <td width="12%" valign="top">showheaders</td>
+ <td width="78%" valign="top">Print headers for result sets from the
statements (default true)</td>
+ <td width="10%" valign="top">No, default "true"</td>
+</tr>
+<tr>
+ <td width="12%" valign="top">output</td>
+ <td width="78%" valign="top">Output file for result sets (defaults to
System.out)</td>
+ <td width="10%" valign="top">No (print to System.out by default)</td>
+</tr>
+<tr>
+ <td width="12%" valign="top">classpath</td>
+ <td width="78%" valign="top">Classpath used to load driver</td>
+ <td width="10%" valign="top">No (use system classpath)</td>
+</tr>
</table>
<h3>Examples</h3>
@@ -80,7 +100,7 @@
<p>Note that you may want to enclose your statements in
<code><![CDATA[</code> ... <code>]]></code> sections so you don't
need to escape <code><</code>, <code>></code> <code>&</code>
-or other special characters. For exampe:</p>
+or other special characters. For example:</p>
<blockquote><pre><sql
driver="org.database.jdbcDriver"
@@ -93,6 +113,24 @@
]]></sql>
</pre></blockquote>
+
+<p>The following 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, with output piped to outputfile.txt, searching
/some/jdbc.jar as well as the system classpath for the driver class.</p>
+
+<pre><blockquote><sql
+ driver="org.database.jdbcDriver"
+ url="jdbc:database-url"
+ userid="sa"
+ password="pass"
+ src="data.sql"
+ print="yes"
+ output="outputfile.txt"
+ >
+<classpath>
+ <pathelement location="/some/jdbc.jar">
+</classpath>
+</sql>
+</pre></blockquote>
+
</body>
Index: src/main/org/apache/tools/ant/taskdefs/SQLExec.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java,v
retrieving revision 1.6
diff -u -r1.6 SQLExec.java
--- src/main/org/apache/tools/ant/taskdefs/SQLExec.java 2000/09/24 09:31:59
1.6
+++ src/main/org/apache/tools/ant/taskdefs/SQLExec.java 2000/09/27 01:09:36
@@ -55,11 +55,13 @@
package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*;
+import org.apache.tools.ant.types.*;
import java.io.*;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
+import java.util.Properties;
import java.util.zip.*;
import java.sql.*;
@@ -71,6 +73,10 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Martin</a>
*/
public class SQLExec extends Task {
+
+ private Path classpath;
+
+ private AntClassLoader loader;
/**
* Database connection
@@ -131,6 +137,34 @@
* Results Output file.
*/
private File output = null;
+
+ /**
+ * Set the classpath for loading the driver.
+ */
+ public void setClasspath(Path classpath) {
+ if (this.classpath == null) {
+ this.classpath = classpath;
+ } else {
+ this.classpath.append(classpath);
+ }
+ }
+
+ /**
+ * Create the classpath for loading the driver.
+ */
+ public Path createClasspath() {
+ if (this.classpath == null) {
+ this.classpath = new Path(project);
+ }
+ return this.classpath.createPath();
+ }
+
+ /**
+ * Set the classpath for loading the driver using the classpath reference.
+ */
+ public void setClasspathRef(Reference r) {
+ createClasspath().setRefid(r);
+ }
/**
* Set the name of the sql file to be run.
@@ -185,28 +219,27 @@
* Set the print flag.
*/
public void setPrint(boolean print) {
- this.print = print;
+ this.print = print;
}
/**
* Set the showheaders flag.
*/
public void setShowheaders(boolean showheaders) {
- this.showheaders = showheaders;
+ this.showheaders = showheaders;
}
/**
* Set the output file.
*/
public void setOutput(File output) {
- this.output = output;
+ this.output = output;
}
-
+
/**
* Load the sql file and then execute it
*/
public void execute() throws BuildException {
-
sqlCommand = sqlCommand.trim();
if (srcFile == null && sqlCommand.length() == 0) {
@@ -227,16 +260,38 @@
if (srcFile != null && !srcFile.exists()) {
throw new BuildException("Source file does not exist!", location);
}
-
- try{
- Class.forName(driver);
+ Driver driverInstance = null;
+ // Load the driver using the
+ try {
+ Class dc;
+ if (classpath != null) {
+ log("Loading " + driver + " using AntClassLoader with classpath
" + classpath, Project.MSG_VERBOSE);
+ loader = new AntClassLoader(project, classpath, false);
+ dc = loader.forceLoadSystemClass(driver);
+ }
+ else {
+ log("Loading " + driver + " using system loader.",
Project.MSG_VERBOSE);
+ dc = Class.forName(driver);
+ }
+ driverInstance = (Driver) dc.getConstructor(new
Class[]{}).newInstance(new Object[]{});
}catch(ClassNotFoundException e){
- throw new BuildException("JDBC driver " + driver + " could not be
loaded", location);
+ throw new BuildException("Class Not Found: JDBC driver " + driver
+ " could not be loaded", location);
+ }catch(IllegalAccessException e){
+ throw new BuildException("Illegal Access: JDBC driver " + driver +
" could not be loaded", location);
+ }catch(java.lang.reflect.InvocationTargetException e) {
+ throw new BuildException("Invocation Targetd Exception: JDBC
driver " + driver + " could not be loaded", location);
+ }catch(InstantiationException e) {
+ throw new BuildException("Instantiation Exception: JDBC driver " +
driver + " could not be loaded", location);
+ }catch(NoSuchMethodException e) {
+ throw new BuildException("No Such Method: JDBC driver " + driver +
" could not be loaded", location);
}
try{
log("connecting to " + url, Project.MSG_VERBOSE );
- conn = DriverManager.getConnection(url, userId, password);
+ Properties info = new Properties();
+ info.put("user", userId);
+ info.put("password", password);
+ conn = driverInstance.connect(url, info);
conn.setAutoCommit(autocommit);
@@ -288,9 +343,9 @@
String sql = "";
String line = "";
- BufferedReader in = new BufferedReader(reader);
+ BufferedReader in = new BufferedReader(reader);
- try{
+ try{
while ((line=in.readLine()) != null){
if (line.trim().startsWith("//")) continue;
if (line.trim().startsWith("--")) continue;
@@ -304,15 +359,15 @@
}
}
- // Catch any statements not followed by ;
- if(!sql.equals("")){
- execSQL(sql);
- }
- }catch(SQLException e){
+ // Catch any statements not followed by ;
+ if(!sql.equals("")){
+ execSQL(sql);
+ }
+ }catch(SQLException e){
log("Failed to execute: " + sql, Project.MSG_ERR);
- throw e;
- }
- }
+ throw e;
+ }
+ }
/**
@@ -324,9 +379,9 @@
Project.MSG_VERBOSE);
}
- if (print) {
- printResults();
- }
+ if (print) {
+ printResults();
+ }
SQLWarning warning = conn.getWarnings();
while(warning!=null){
@@ -344,13 +399,15 @@
PrintStream out = System.out;
try {
if (output != null) {
- out = new PrintStream(new BufferedOutputStream(new
FileOutputStream(output)));
+ log("Opening PrintStream to output file " + output,
Project.MSG_VERBOSE);
+ out = new PrintStream(new BufferedOutputStream(new
FileOutputStream(output)));
}
while ((rs = statement.getResultSet()) != null) {
+ log("Processing new result set.", Project.MSG_VERBOSE);
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
StringBuffer line = new StringBuffer();
- if (showheaders) {
+ if (showheaders) {
for (int col = 1; col < columnCount; col++) {
line.append(md.getColumnName(col));
line.append(",");
@@ -358,7 +415,7 @@
line.append(md.getColumnName(columnCount));
out.println(line);
line.setLength(0);
- }
+ }
while (rs.next()) {
for (int col = 1; col < columnCount; col++) {
line.append(rs.getString(col).trim());
@@ -374,11 +431,10 @@
catch (IOException ioe) {
throw new BuildException("Error writing " +
output.getAbsolutePath(), ioe, location);
}
- finally {
+ finally {
if (out != null) {
out.close();
}
}
}
-
}
