You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by mp...@apache.org on 2002/02/16 17:58:38 UTC

cvs commit: jakarta-turbine-torque/src/java/org/apache/torque/task TorqueSQLExec.java

mpoeschl    02/02/16 08:58:38

  Modified:    src/java/org/apache/torque/task TorqueSQLExec.java
  Log:
  fix path handling
  
  Revision  Changes    Path
  1.3       +179 -177  jakarta-turbine-torque/src/java/org/apache/torque/task/TorqueSQLExec.java
  
  Index: TorqueSQLExec.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/task/TorqueSQLExec.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TorqueSQLExec.java	1 Nov 2001 02:21:40 -0000	1.2
  +++ TorqueSQLExec.java	16 Feb 2002 16:58:38 -0000	1.3
  @@ -3,7 +3,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -11,7 +11,7 @@
    * are met:
    *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  + *    notice, this list of conditions and the following disclaimer.
    *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
  @@ -19,15 +19,15 @@
    *    distribution.
    *
    * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowlegement:  
  - *       "This product includes software developed by the 
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowlegement may appear in the software itself,
    *    if and wherever such third-party acknowlegements normally appear.
    *
    * 4. The names "The Jakarta Project", "Ant", and "Apache Software
    *    Foundation" must not be used to endorse or promote products derived
  - *    from this software without prior written permission. For written 
  + *    from this software without prior written permission. For written
    *    permission, please contact apache@apache.org.
    *
    * 5. Products derived from this software may not be called "Apache"
  @@ -93,26 +93,28 @@
   /**
    * This task uses an SQL -> Database map in the form of a properties
    * file to insert each SQL file listed into its designated database.
  - * 
  + *
    * @author <a href="mailto:jeff@custommonkey.org">Jeff Martin</a>
    * @author <a href="mailto:gholam@xtra.co.nz">Michael McCallum</A>
    * @author <a href="mailto:tim.stephenson@sybase.com">Tim Stephenson</A>
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</A>
  + * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
  + * @version $Id: TorqueSQLExec.java,v 1.3 2002/02/16 16:58:38 mpoeschl Exp $
    */
  -public class TorqueSQLExec 
  -    extends Task 
  +public class TorqueSQLExec extends Task
   {
  -    static public class DelimiterType 
  -        extends EnumeratedAttribute 
  +
  +    static public class DelimiterType extends EnumeratedAttribute
       {
           static public final String NORMAL = "normal";
           static public final String ROW = "row";
  -        public String[] getValues() 
  +
  +        public String[] getValues()
           {
               return new String[] {NORMAL, ROW};
           }
       }
  -    
  +
       private int goodSql = 0, totalSql = 0;
       private Path classpath;
       private AntClassLoader loader;
  @@ -121,12 +123,12 @@
        * Database connection
        */
       private Connection conn = null;
  -    
  +
       /**
        * Autocommit flag. Default value is false
        */
  -    private boolean autocommit=false;
  -    
  +    private boolean autocommit = false;
  +
       /**
        * SQL statement
        */
  @@ -166,13 +168,13 @@
        * SQL Statement delimiter
        */
       private String delimiter = ";";
  -    
  +
       /**
        * The delimiter type indicating whether the delimiter will
        * only be recognized on a line by itself
        */
       private String delimiterType = DelimiterType.NORMAL;
  -    
  +
       /**
        * Print SQL results.
        */
  @@ -202,12 +204,12 @@
        * Action to perform if an error is found
        **/
       private String onError = "abort";
  -    
  +
       /**
        * Encoding to use when reading SQL statements from a file
        */
       private String encoding = null;
  -    
  +
       /**
        * Src directory for the files listed in the sqldbmap.
        */
  @@ -218,32 +220,32 @@
        * file to a database.
        */
       private File sqldbmap;
  -    
  +
       /**
        * Set the sqldbmap properties file.
        */
  -    public void setSqlDbMap(File sqldbmap)
  +    public void setSqlDbMap(String sqldbmap)
       {
  -        this.sqldbmap = sqldbmap;
  +        this.sqldbmap = project.resolveFile(sqldbmap);
       }
  -    
  +
       /**
        * Get the sqldbmap properties file.
        */
       public File getSqlDbMap()
       {
           return sqldbmap;
  -    }        
  -    
  +    }
  +
       /**
        * Set the src directory for the sql files
        * listed in the sqldbmap file.
        */
       public void setSrcDir(String srcDir)
       {
  -        this.srcDir = srcDir;
  +        this.srcDir = project.resolveFile(srcDir).toString();
       }
  -    
  +
       /**
        * Get the src directory for the sql files
        * listed in the sqldbmap file.
  @@ -251,18 +253,18 @@
       public String getSrcDir()
       {
           return srcDir;
  -    }        
  +    }
   
       /**
        * Set the classpath for loading the driver.
        */
  -    public void setClasspath(Path classpath) 
  +    public void setClasspath(Path classpath)
       {
  -        if (this.classpath == null) 
  +        if (this.classpath == null)
           {
               this.classpath = classpath;
  -        } 
  -        else 
  +        }
  +        else
           {
               this.classpath.append(classpath);
           }
  @@ -271,9 +273,9 @@
       /**
        * Create the classpath for loading the driver.
        */
  -    public Path createClasspath() 
  +    public Path createClasspath()
       {
  -        if (this.classpath == null) 
  +        if (this.classpath == null)
           {
               this.classpath = new Path(project);
           }
  @@ -283,39 +285,39 @@
       /**
        * Set the classpath for loading the driver using the classpath reference.
        */
  -    public void setClasspathRef(Reference r) 
  +    public void setClasspathRef(Reference r)
       {
           createClasspath().setRefid(r);
       }
  -    
  +
       /**
        * Set the sql command to execute
        */
  -    public void addText(String sql) 
  +    public void addText(String sql)
       {
           this.sqlCommand += sql;
       }
  -    
  +
       /**
        * Set the JDBC driver to be used.
        */
  -    public void setDriver(String driver) 
  +    public void setDriver(String driver)
       {
           this.driver = driver;
       }
  -    
  +
       /**
        * Set the DB connection url.
        */
  -    public void setUrl(String url) 
  +    public void setUrl(String url)
       {
           this.url = url;
       }
  -    
  +
       /**
        * Set the user name for the DB connection.
        */
  -    public void setUserid(String userId) 
  +    public void setUserid(String userId)
       {
           this.userId = userId;
       }
  @@ -325,23 +327,23 @@
        *
        * @param encoding the encoding to use on the files
        */
  -    public void setEncoding(String encoding) 
  +    public void setEncoding(String encoding)
       {
           this.encoding = encoding;
       }
  -    
  +
       /**
        * Set the password for the DB connection.
        */
  -    public void setPassword(String password) 
  +    public void setPassword(String password)
       {
           this.password = password;
       }
  -    
  +
       /**
        * Set the autocommit flag for the DB connection.
        */
  -    public void setAutocommit(boolean autocommit) 
  +    public void setAutocommit(boolean autocommit)
       {
           this.autocommit = autocommit;
       }
  @@ -352,34 +354,34 @@
        * <p>For example, set this to "go" and delimitertype to "ROW" for
        * Sybase ASE or MS SQL Server.</p>
        */
  -    public void setDelimiter(String delimiter) 
  +    public void setDelimiter(String delimiter)
       {
           this.delimiter = delimiter;
       }
   
       /**
  -     * 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) 
  +    public void setDelimiterType(DelimiterType delimiterType)
       {
           this.delimiterType = delimiterType.getValue();
       }
  -    
  +
       /**
        * Set the print flag.
        */
  -    public void setPrint(boolean print) 
  +    public void setPrint(boolean print)
       {
           this.print = print;
       }
  -    
  +
       /**
        * Set the showheaders flag.
        */
  -    public void setShowheaders(boolean showheaders) 
  +    public void setShowheaders(boolean showheaders)
       {
           this.showheaders = showheaders;
       }
  @@ -387,7 +389,7 @@
       /**
        * Set the output file.
        */
  -    public void setOutput(File output) 
  +    public void setOutput(File output)
       {
           this.output = output;
       }
  @@ -395,7 +397,7 @@
       /**
        * Set the rdbms required
        */
  -    public void setRdbms(String vendor) 
  +    public void setRdbms(String vendor)
       {
           this.rdbms = vendor.toLowerCase();
       }
  @@ -403,7 +405,7 @@
       /**
        * Set the version required
        */
  -    public void setVersion(String version) 
  +    public void setVersion(String version)
       {
           this.version = version.toLowerCase();
       }
  @@ -411,7 +413,7 @@
       /**
        * Set the action to perform onerror
        */
  -    public void setOnerror(OnError action) 
  +    public void setOnerror(OnError action)
       {
           this.onError = action.getValue();
       }
  @@ -419,36 +421,36 @@
       /**
        * Load the sql file and then execute it
        */
  -    public void execute() 
  -        throws BuildException 
  +    public void execute()
  +        throws BuildException
       {
           sqlCommand = sqlCommand.trim();
   
  -        if (sqldbmap == null || getSqlDbMap().exists() == false) 
  +        if (sqldbmap == null || getSqlDbMap().exists() == false)
           {
               throw new BuildException("You haven't provided an sqldbmap, or " +
                   "the one you specified doesn't exist: " + sqldbmap);
  -        }            
  -        
  -        if (driver == null) 
  +        }
  +
  +        if (driver == null)
           {
               throw new BuildException("Driver attribute must be set!", location);
           }
  -        if (userId == null) 
  +        if (userId == null)
           {
               throw new BuildException("User Id attribute must be set!", location);
           }
  -        if (password == null) 
  +        if (password == null)
           {
               throw new BuildException("Password attribute must be set!", location);
           }
  -        if (url == null) 
  +        if (url == null)
           {
               throw new BuildException("Url attribute must be set!", location);
           }
  -        
  +
           Properties p = new Properties();
  -        
  +
           try
           {
               FileInputStream fis = new FileInputStream(getSqlDbMap());
  @@ -458,7 +460,7 @@
           catch (IOException ioe)
           {
               throw new BuildException("Cannot open and process the sqldbmap!");
  -        }                        
  +        }
   
           Hashtable h = new Hashtable();
   
  @@ -466,15 +468,15 @@
           {
               String sqlfile = (String) e.nextElement();
               String database = p.getProperty(sqlfile);
  -            
  +
               ArrayList x = (ArrayList) h.get(database);
  -            
  +
               if (x == null)
               {
                   x = new ArrayList();
                   h.put(database, x);
               }
  -            
  +
               // We want to make sure that the base schemas
               // are inserted first.
               if (sqlfile.indexOf("schema.sql") != -1)
  @@ -484,11 +486,11 @@
               else
               {
                   x.add(sqlfile);
  -            }                
  +            }
           }
  -        
  +
           Iterator k = h.keySet().iterator();
  -        
  +
           while (k.hasNext())
           {
               String db = (String) k.next();
  @@ -501,12 +503,12 @@
                   Transaction t = new Transaction();
                   t.setSrc(new File(srcDir,s));
                   ts.addElement(t);
  -            }                
  -            
  +            }
  +
               insertDatabaseSqlFiles(url, db, ts);
           }
  -    }        
  -    
  +    }
  +
       /**
        * Take the base url, the target database and insert a set of SQL
        * files into the target database.
  @@ -515,20 +517,20 @@
       {
           url = StringUtils.replace(url, "@DB@", database);
           System.out.println("Our new url -> " + url);
  -    
  +
           Driver driverInstance = null;
  -        try 
  +        try
           {
               Class dc;
  -            if (classpath != null) 
  +            if (classpath != null)
               {
  -                log( "Loading " + driver + " using AntClassLoader with classpath " + classpath, 
  +                log( "Loading " + driver + " using AntClassLoader with classpath " + classpath,
                        Project.MSG_VERBOSE );
   
                   loader = new AntClassLoader(project, classpath);
                   dc = loader.loadClass(driver);
               }
  -            else 
  +            else
               {
                   log("Loading " + driver + " using system loader.", Project.MSG_VERBOSE);
                   dc = Class.forName(driver);
  @@ -537,17 +539,17 @@
           }
           catch(ClassNotFoundException e)
           {
  -            throw new BuildException("Class Not Found: JDBC driver " + driver + 
  +            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 + 
  +            throw new BuildException("Illegal Access: JDBC driver " + driver +
                   " could not be loaded", location);
           }
  -        catch(InstantiationException e) 
  +        catch(InstantiationException e)
           {
  -            throw new BuildException("Instantiation Exception: JDBC driver " + 
  +            throw new BuildException("Instantiation Exception: JDBC driver " +
                   driver + " could not be loaded", location);
           }
   
  @@ -559,7 +561,7 @@
               info.put("password", password);
               conn = driverInstance.connect(url, info);
   
  -            if (conn == null) 
  +            if (conn == null)
               {
                   // Driver doesn't understand the URL
                   throw new SQLException("No suitable Driver for "+url);
  @@ -570,91 +572,91 @@
               conn.setAutoCommit(autocommit);
               statement = conn.createStatement();
               PrintStream out = System.out;
  -            try 
  +            try
               {
  -                if (output != null) 
  +                if (output != null)
                   {
                       log("Opening PrintStream to output file " + output, Project.MSG_VERBOSE);
                       out = new PrintStream(new BufferedOutputStream(new FileOutputStream(output)));
                   }
  -                        
  +
                   // Process all transactions
  -                for (Enumeration e = transactions.elements(); e.hasMoreElements();) 
  +                for (Enumeration e = transactions.elements(); e.hasMoreElements();)
                   {
                       ((Transaction) e.nextElement()).runTransaction(out);
  -                    if (!autocommit) 
  +                    if (!autocommit)
                       {
                           log("Commiting transaction", Project.MSG_VERBOSE);
                           conn.commit();
                       }
                   }
               }
  -            finally 
  +            finally
               {
  -                if (out != null && out != System.out) 
  +                if (out != null && out != System.out)
                   {
                       out.close();
                   }
               }
  -        } 
  +        }
           catch(IOException e)
           {
  -            if (!autocommit && conn != null && onError.equals("abort")) 
  +            if (!autocommit && conn != null && onError.equals("abort"))
               {
  -                try 
  +                try
                   {
                       conn.rollback();
  -                } 
  -                catch (SQLException ex) 
  +                }
  +                catch (SQLException ex)
                   {
                       // do nothing.
                   }
               }
               throw new BuildException(e, location);
  -        } 
  +        }
           catch(SQLException e)
           {
  -            if (!autocommit && conn != null && onError.equals("abort")) 
  +            if (!autocommit && conn != null && onError.equals("abort"))
               {
  -                try 
  +                try
                   {
                       conn.rollback();
  -                } 
  -                catch (SQLException ex) 
  +                }
  +                catch (SQLException ex)
                   {
                       // do nothing.
                   }
               }
               throw new BuildException(e, location);
           }
  -        finally 
  +        finally
           {
  -            try 
  +            try
               {
  -                if (statement != null) 
  +                if (statement != null)
                   {
                       statement.close();
                   }
  -                if (conn != null) 
  +                if (conn != null)
                   {
                       conn.close();
                   }
               }
               catch (SQLException e) {}
           }
  -          
  -        log(goodSql + " of " + totalSql + 
  +
  +        log(goodSql + " of " + totalSql +
               " SQL statements executed successfully");
       }
   
  -    protected void runStatements(Reader reader, PrintStream out) 
  -        throws SQLException, IOException 
  +    protected void runStatements(Reader reader, PrintStream out)
  +        throws SQLException, IOException
       {
           String sql = "";
           String line = "";
  - 
  +
           BufferedReader in = new BufferedReader(reader);
  - 
  +
           try
           {
               while ((line=in.readLine()) != null)
  @@ -682,7 +684,7 @@
                       sql = "";
                   }
               }
  - 
  +
               // Catch any statements not followed by ;
               if(!sql.equals(""))
               {
  @@ -694,83 +696,83 @@
               throw e;
           }
       }
  - 
  +
       /**
        * Verify if connected to the correct RDBMS
        **/
  -    protected boolean isValidRdbms(Connection conn) 
  +    protected boolean isValidRdbms(Connection conn)
       {
           if (rdbms == null && version == null)
           {
               return true;
  -        }            
  -        
  -        try 
  +        }
  +
  +        try
           {
               DatabaseMetaData dmd = conn.getMetaData();
  -            
  -            if (rdbms != null) 
  +
  +            if (rdbms != null)
               {
                   String theVendor = dmd.getDatabaseProductName().toLowerCase();
  -                
  +
                   log("RDBMS = " + theVendor, Project.MSG_VERBOSE);
  -                if (theVendor == null || theVendor.indexOf(rdbms) < 0) 
  +                if (theVendor == null || theVendor.indexOf(rdbms) < 0)
                   {
                       log("Not the required RDBMS: "+rdbms, Project.MSG_VERBOSE);
                       return false;
                   }
               }
  -            
  -            if (version != null) 
  +
  +            if (version != null)
               {
                   String theVersion = dmd.getDatabaseProductVersion().toLowerCase();
  -                
  +
                   log("Version = " + theVersion, Project.MSG_VERBOSE);
  -                if (theVersion == null || 
  -                    !(theVersion.startsWith(version) || 
  -                      theVersion.indexOf(" " + version) >= 0)) 
  +                if (theVersion == null ||
  +                    !(theVersion.startsWith(version) ||
  +                      theVersion.indexOf(" " + version) >= 0))
                   {
                       log("Not the required version: \""+ version +"\"", Project.MSG_VERBOSE);
                       return false;
                   }
               }
           }
  -        catch (SQLException e) 
  +        catch (SQLException e)
           {
               // Could not get the required information
               log("Failed to obtain required RDBMS information", Project.MSG_ERR);
               return false;
           }
  -        
  +
           return true;
       }
  -    
  +
       /**
        * Exec the sql statement.
        */
  -    protected void execSQL(String sql, PrintStream out) throws SQLException 
  +    protected void execSQL(String sql, PrintStream out) throws SQLException
       {
           // Check and ignore empty statements
           if ("".equals(sql.trim())) return;
  -        
  -        try 
  +
  +        try
           {
               totalSql++;
  -            if (!statement.execute(sql)) 
  +            if (!statement.execute(sql))
               {
  -                log(statement.getUpdateCount()+" rows affected", 
  +                log(statement.getUpdateCount() + " rows affected",
                       Project.MSG_VERBOSE);
               }
  -            else 
  +            else
               {
  -                if (print) 
  +                if (print)
                   {
                       printResults(out);
                   }
               }
  -            
  +
               SQLWarning warning = conn.getWarnings();
  -            while(warning!=null)
  +            while (warning != null)
               {
                   log(warning + " sql warning", Project.MSG_VERBOSE);
                   warning=warning.getNextWarning();
  @@ -778,35 +780,35 @@
               conn.clearWarnings();
               goodSql++;
           }
  -        catch (SQLException e) 
  +        catch (SQLException e)
           {
               log("Failed to execute: " + sql, Project.MSG_ERR);
  -            if (!onError.equals("continue")) 
  -            { 
  +            if (!onError.equals("continue"))
  +            {
                   throw e;
  -            }                
  +            }
               log(e.toString(), Project.MSG_ERR);
           }
       }
  -    
  +
       /**
        * print any results in the statement.
        */
  -    protected void printResults(PrintStream out) throws java.sql.SQLException 
  +    protected void printResults(PrintStream out) throws java.sql.SQLException
       {
           ResultSet rs = null;
  -        do 
  +        do
           {
               rs = statement.getResultSet();
  -            if (rs != null) 
  +            if (rs != 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++) 
  +                    for (int col = 1; col < columnCount; col++)
                       {
                            line.append(md.getColumnName(col));
                            line.append(",");
  @@ -815,22 +817,22 @@
                       out.println(line);
                       line.setLength(0);
                   }
  -                while (rs.next()) 
  +                while (rs.next())
                   {
                       boolean first = true;
  -                    for (int col = 1; col <= columnCount; col++) 
  +                    for (int col = 1; col <= columnCount; col++)
                       {
                           String columnValue = rs.getString(col);
  -                        if (columnValue != null) 
  +                        if (columnValue != null)
                           {
                               columnValue = columnValue.trim();
                           }
  -                         
  -                        if (first) 
  +
  +                        if (first)
                           {
                               first = false;
                           }
  -                        else 
  +                        else
                           {
                               line.append(",");
                           }
  @@ -847,11 +849,11 @@
   
       /**
        * Enumerated attribute with the values "continue", "stop" and "abort"
  -     * for the onerror attribute.  
  +     * for the onerror attribute.
        */
  -    public static class OnError extends EnumeratedAttribute 
  +    public static class OnError extends EnumeratedAttribute
       {
  -        public String[] getValues() 
  +        public String[] getValues()
           {
               return new String[] {"continue", "stop", "abort"};
           }
  @@ -863,33 +865,33 @@
        * to be executed using the same JDBC connection and commit
        * operation in between.
        */
  -    public class Transaction 
  +    public class Transaction
       {
           private File tSrcFile = null;
           private String tSqlCommand = "";
   
  -        public void setSrc(File src) 
  +        public void setSrc(File src)
           {
               this.tSrcFile = src;
           }
   
  -        public void addText(String sql) 
  +        public void addText(String sql)
           {
               this.tSqlCommand += sql;
           }
   
  -        private void runTransaction(PrintStream out) 
  -            throws IOException, SQLException 
  +        private void runTransaction(PrintStream out)
  +            throws IOException, SQLException
           {
  -            if (tSqlCommand.length() != 0) 
  +            if (tSqlCommand.length() != 0)
               {
                   log("Executing commands", Project.MSG_INFO);
                   runStatements(new StringReader(tSqlCommand), out);
               }
  -      
  -            if (tSrcFile != null) 
  +
  +            if (tSrcFile != null)
               {
  -                log("Executing file: " + tSrcFile.getAbsolutePath(), 
  +                log("Executing file: " + tSrcFile.getAbsolutePath(),
                       Project.MSG_INFO);
                   Reader reader = (encoding == null) ? new FileReader(tSrcFile)
                                                      : new InputStreamReader(new FileInputStream(tSrcFile), encoding);
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>