ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@locus.apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs SQLExec.java Taskdef.java
Date Wed, 27 Sep 2000 07:42:24 GMT
bodewig     00/09/27 00:42:24

  Modified:    docs     sql.html
               src/main/org/apache/tools/ant AntClassLoader.java
               src/main/org/apache/tools/ant/taskdefs SQLExec.java
                        Taskdef.java
  Log:
  Allow the driver for <sql> to be loaded from a different CLASSPATH.
  Submitted by:	Julian M. Savage <jsavage@fisci.com>
  
  Revision  Changes    Path
  1.2       +39 -1     jakarta-ant/docs/sql.html
  
  Index: sql.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/sql.html,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- sql.html	2000/07/28 08:57:29	1.1
  +++ sql.html	2000/09/27 07:42:21	1.2
  @@ -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>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections
so you don't
   need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
  -or other special characters. For exampe:</p>
  +or other special characters. For example:</p>
   
   <blockquote><pre>&lt;sql
       driver="org.database.jdbcDriver"
  @@ -93,6 +113,24 @@
   
   ]]&gt;&lt;/sql&gt;
   </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>&lt;sql
  +    driver="org.database.jdbcDriver"
  +    url="jdbc:database-url"
  +    userid="sa"
  +    password="pass"
  +    src="data.sql"
  +    print="yes"
  +    output="outputfile.txt"
  +    &gt;
  +&lt;classpath&gt;
  +	&lt;pathelement location="/some/jdbc.jar"&gt;
  +&lt;/classpath&gt;
  +&lt;/sql&gt;
  +</pre></blockquote>
  +
   
   
   </body>
  
  
  
  1.7       +4 -0      jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java
  
  Index: AntClassLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- AntClassLoader.java	2000/09/26 11:05:43	1.6
  +++ AntClassLoader.java	2000/09/27 07:42:22	1.7
  @@ -110,6 +110,10 @@
       public AntClassLoader(Project project, Path classpath) {
           this.project = project;
           this.classpath = classpath;
  +        if (project.getJavaVersion().startsWith("1.1")) {
  +            // JDK > 1.1 adds these by default
  +            addSystemPackageRoot("java");
  +        }
       }
   
       /**
  
  
  
  1.7       +82 -29    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
  
  Index: SQLExec.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SQLExec.java	2000/09/24 09:31:59	1.6
  +++ SQLExec.java	2000/09/27 07:42:23	1.7
  @@ -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:jeff@custommonkey.org">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,34 @@
           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.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("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(InstantiationException e) {
  +            throw new BuildException("Instantiation Exception: 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 +339,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 +355,16 @@
                   }
               }
    
  - 	    // 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 +376,9 @@
                   Project.MSG_VERBOSE);
           }
   
  -	if (print) {
  -	    printResults();
  -	}
  +        if (print) {
  +            printResults();
  +        }
   
           SQLWarning warning = conn.getWarnings();
           while(warning!=null){
  @@ -344,13 +396,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 +412,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 +428,10 @@
           catch (IOException ioe) {
               throw new BuildException("Error writing " + output.getAbsolutePath(), ioe,
location);
           }
  -	finally {
  -            if (out != null) {
  +        finally {
  +            if (out != null && out != System.out) {
                   out.close();
               }
           }
       }
  -
   }
  
  
  
  1.11      +3 -4      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Taskdef.java
  
  Index: Taskdef.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Taskdef.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Taskdef.java	2000/09/26 11:05:46	1.10
  +++ Taskdef.java	2000/09/27 07:42:23	1.11
  @@ -97,11 +97,10 @@
                   if (classpath != null) {
                       AntClassLoader al = new AntClassLoader(project, classpath,
                                                              false);
  +                    // need to load Task via system classloader or the new
  +                    // task we want to define will never be a Task but always
  +                    // be wrapped into a TaskAdapter.
                       al.addSystemPackageRoot("org.apache.tools.ant");
  -                    if (project.getJavaVersion().startsWith("1.1")) {
  -                        // JDK > 1.1 adds these by default
  -                        al.addSystemPackageRoot("java");
  -                    }
                       loader = al;
                   } else {
                       loader = this.getClass().getClassLoader();
  
  
  

Mime
View raw message