diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index c0d12c490..feffcba30 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -100,6 +100,8 @@ import java.sql.ResultSetMetaData; * @author Michael McCallum * @author Tim Stephenson * + * @since Ant 1.2 + * * @ant.task name="sql" category="database" */ public class SQLExec extends Task { @@ -119,6 +121,7 @@ public class SQLExec extends Task { */ private static Hashtable loaderMap = new Hashtable(3); + // XXX - why is this public? public boolean caching = true; private int goodSql = 0; @@ -351,10 +354,12 @@ public class SQLExec extends Task { } /** - * Set the Delimiter type for this sql task. The delimiter type takes - * two values - normal and row. Normal means that any occurence of the delimiter - * terminate the SQL command whereas with row, only a line containing just the - * delimiter is recognized as the end of the command. + * Set the Delimiter type for this sql task. + * + *
The delimiter type takes two values - normal and row. Normal + * means that any occurence of the delimiter terminate the SQL + * command whereas with row, only a line containing just the + * delimiter is recognized as the end of the command.
*/ public void setDelimiterType(DelimiterType delimiterType) { this.delimiterType = delimiterType.getValue(); @@ -384,7 +389,7 @@ public class SQLExec extends Task { /** * Shall we append to an existing file? * - * @since 1.36, Ant 1.5 + * @since Ant 1.5 */ public void setAppend(boolean append) { this.append = append; @@ -415,214 +420,245 @@ public class SQLExec extends Task { * Load the sql file and then execute it */ public void execute() throws BuildException { + Vector savedTransaction = (Vector) transactions.clone(); + String savedSqlCommand = sqlCommand; + sqlCommand = sqlCommand.trim(); - if (srcFile == null && sqlCommand.length()==0 && filesets.isEmpty()) { - if (transactions.size() == 0) { - throw new BuildException("Source file or fileset, transactions or sql statement must be set!", location); + try { + if (srcFile == null && sqlCommand.length()==0 + && filesets.isEmpty()) { + if (transactions.size() == 0) { + throw new BuildException("Source file or fileset, " + + "transactions or sql statement " + + "must be set!", location); + } + } + if (driver == null) { + throw new BuildException("Driver attribute must be set!", + location); } - } else { + if (userId == null) { + throw new BuildException("User Id attribute must be set!", + location); + } + if (password == null) { + throw new BuildException("Password attribute must be set!", + location); + } + if (url == null) { + throw new BuildException("Url attribute must be set!", + location); + } + if (srcFile != null && !srcFile.exists()) { + throw new BuildException("Source file does not exist!", + location); + } + Driver driverInstance = null; + try { + Class dc; + if (classpath != null) { + // check first that it is not already loaded otherwise + // consecutive runs seems to end into an OutOfMemoryError + // or it fails when there is a native library to load + // several times. + // this is far from being perfect but should work + // in most cases. + synchronized (loaderMap){ + if (caching){ + loader = (AntClassLoader)loaderMap.get(driver); + } + if (loader == null){ + log( "Loading " + driver + + " using AntClassLoader with classpath " + + classpath, + Project.MSG_VERBOSE ); + loader = new AntClassLoader(project, classpath); + if (caching){ + loaderMap.put(driver, loader); + } + } else { + log("Loading " + driver + + " using a cached AntClassLoader.", + Project.MSG_VERBOSE); + } + } + dc = loader.loadClass(driver); + } + else { + log("Loading " + driver + " using system loader.", + Project.MSG_VERBOSE); + dc = Class.forName(driver); + } + driverInstance = (Driver) dc.newInstance(); + }catch(ClassNotFoundException e){ + 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(InstantiationException e) { + throw new BuildException("Instantiation Exception: JDBC driver " + + driver + " could not be loaded", + location); + } + // deal with the filesets for (int i=0; i