org.apache.torque.task
Class TorqueSQLExec

java.lang.Object
  extended byorg.apache.tools.ant.ProjectComponent
      extended byorg.apache.tools.ant.Task
          extended byorg.apache.torque.task.TorqueSQLExec

public class TorqueSQLExec
extends org.apache.tools.ant.Task

This task uses an SQL -> Database map in the form of a properties file to insert each SQL file listed into its designated database.

Version:
$Id: TorqueSQLExec.java,v 1.15 2002/11/27 11:30:59 mpoeschl Exp $
Author:
Jeff Martin, Michael McCallum, Tim Stephenson, Jason van Zyl, Martin Poeschl

Nested Class Summary
static class TorqueSQLExec.DelimiterType
           
static class TorqueSQLExec.OnError
          Enumerated attribute with the values "continue", "stop" and "abort" for the onerror attribute.
 class TorqueSQLExec.Transaction
          Contains the definition of a new transaction element.
 
Field Summary
private  boolean autocommit
          Autocommit flag.
private  org.apache.tools.ant.types.Path classpath
           
private  java.sql.Connection conn
          Database connection
private  java.lang.String delimiter
          SQL Statement delimiter
private  java.lang.String delimiterType
          The delimiter type indicating whether the delimiter will only be recognized on a line by itself
private  java.lang.String driver
          DB driver.
private  java.lang.String encoding
          Encoding to use when reading SQL statements from a file
private  int goodSql
           
private  org.apache.tools.ant.AntClassLoader loader
           
private  java.lang.String onError
          Action to perform if an error is found
private  java.io.File output
          Results Output file.
private  java.lang.String password
          Password
private  boolean print
          Print SQL results.
private  java.lang.String rdbms
          RDBMS Product needed for this SQL.
private  boolean showheaders
          Print header columns.
private  java.lang.String sqlCommand
          SQL input command
private  java.io.File sqldbmap
          Properties file that maps an individual SQL file to a database.
private  java.lang.String srcDir
          Src directory for the files listed in the sqldbmap.
private  java.sql.Statement statement
          SQL statement
private  int totalSql
           
private  java.util.List transactions
          SQL transactions to perform
private  java.lang.String url
          DB url.
private  java.lang.String userId
          User name.
private  java.lang.String version
          RDBMS Version needed for this SQL.
 
Fields inherited from class org.apache.tools.ant.Task
description, location, target, taskName, taskType, wrapper
 
Fields inherited from class org.apache.tools.ant.ProjectComponent
project
 
Constructor Summary
TorqueSQLExec()
           
 
Method Summary
 void addText(java.lang.String sql)
          Set the sql command to execute
 org.apache.tools.ant.types.Path createClasspath()
          Create the classpath for loading the driver.
protected  void execSQL(java.lang.String sql, java.io.PrintStream out)
          Exec the sql statement.
 void execute()
          Load the sql file and then execute it
 java.io.File getSqlDbMap()
          Get the sqldbmap properties file.
 java.lang.String getSrcDir()
          Get the src directory for the sql files listed in the sqldbmap file.
private  void insertDatabaseSqlFiles(java.lang.String url, java.lang.String database, java.util.List transactions)
          Take the base url, the target database and insert a set of SQL files into the target database.
protected  boolean isValidRdbms(java.sql.Connection conn)
          Verify if connected to the correct RDBMS
protected  void printResults(java.io.PrintStream out)
          print any results in the statement.
protected  void runStatements(java.io.Reader reader, java.io.PrintStream out)
          Read the statements from the .sql file and execute them.
 void setAutocommit(boolean autocommit)
          Set the autocommit flag for the DB connection.
 void setClasspath(org.apache.tools.ant.types.Path classpath)
          Set the classpath for loading the driver.
 void setClasspathRef(org.apache.tools.ant.types.Reference r)
          Set the classpath for loading the driver using the classpath reference.
 void setDelimiter(java.lang.String delimiter)
          Set the statement delimiter.
 void setDelimiterType(TorqueSQLExec.DelimiterType delimiterType)
          Set the Delimiter type for this sql task.
 void setDriver(java.lang.String driver)
          Set the JDBC driver to be used.
 void setEncoding(java.lang.String encoding)
          Set the file encoding to use on the sql files read in
 void setOnerror(TorqueSQLExec.OnError action)
          Set the action to perform onerror
 void setOutput(java.io.File output)
          Set the output file.
 void setPassword(java.lang.String password)
          Set the password for the DB connection.
 void setPrint(boolean print)
          Set the print flag.
 void setRdbms(java.lang.String vendor)
          Set the rdbms required
 void setShowheaders(boolean showheaders)
          Set the showheaders flag.
 void setSqlDbMap(java.lang.String sqldbmap)
          Set the sqldbmap properties file.
 void setSrcDir(java.lang.String srcDir)
          Set the src directory for the sql files listed in the sqldbmap file.
 void setUrl(java.lang.String url)
          Set the DB connection url.
 void setUserid(java.lang.String userId)
          Set the user name for the DB connection.
 void setVersion(java.lang.String version)
          Set the version required
 
Methods inherited from class org.apache.tools.ant.Task
getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, handleErrorFlush, handleErrorOutput, handleFlush, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName
 
Methods inherited from class org.apache.tools.ant.ProjectComponent
getProject, setProject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

goodSql

private int goodSql

totalSql

private int totalSql

classpath

private org.apache.tools.ant.types.Path classpath

loader

private org.apache.tools.ant.AntClassLoader loader

conn

private java.sql.Connection conn
Database connection


autocommit

private boolean autocommit
Autocommit flag. Default value is false


statement

private java.sql.Statement statement
SQL statement


driver

private java.lang.String driver
DB driver.


url

private java.lang.String url
DB url.


userId

private java.lang.String userId
User name.


password

private java.lang.String password
Password


sqlCommand

private java.lang.String sqlCommand
SQL input command


transactions

private java.util.List transactions
SQL transactions to perform


delimiter

private java.lang.String delimiter
SQL Statement delimiter


delimiterType

private java.lang.String delimiterType
The delimiter type indicating whether the delimiter will only be recognized on a line by itself


print

private boolean print
Print SQL results.


showheaders

private boolean showheaders
Print header columns.


output

private java.io.File output
Results Output file.


rdbms

private java.lang.String rdbms
RDBMS Product needed for this SQL.


version

private java.lang.String version
RDBMS Version needed for this SQL.


onError

private java.lang.String onError
Action to perform if an error is found


encoding

private java.lang.String encoding
Encoding to use when reading SQL statements from a file


srcDir

private java.lang.String srcDir
Src directory for the files listed in the sqldbmap.


sqldbmap

private java.io.File sqldbmap
Properties file that maps an individual SQL file to a database.

Constructor Detail

TorqueSQLExec

public TorqueSQLExec()
Method Detail

setSqlDbMap

public void setSqlDbMap(java.lang.String sqldbmap)
Set the sqldbmap properties file.

Parameters:
sqldbmap - filename for the sqldbmap

getSqlDbMap

public java.io.File getSqlDbMap()
Get the sqldbmap properties file.

Returns:
filename for the sqldbmap

setSrcDir

public void setSrcDir(java.lang.String srcDir)
Set the src directory for the sql files listed in the sqldbmap file.

Parameters:
srcDir - sql source directory

getSrcDir

public java.lang.String getSrcDir()
Get the src directory for the sql files listed in the sqldbmap file.

Returns:
sql source directory

setClasspath

public void setClasspath(org.apache.tools.ant.types.Path classpath)
Set the classpath for loading the driver.

Parameters:
classpath - the classpath

createClasspath

public org.apache.tools.ant.types.Path createClasspath()
Create the classpath for loading the driver.

Returns:
the classpath

setClasspathRef

public void setClasspathRef(org.apache.tools.ant.types.Reference r)
Set the classpath for loading the driver using the classpath reference.

Parameters:
r - reference to the classpath

addText

public void addText(java.lang.String sql)
Set the sql command to execute

Parameters:
sql - sql command to execute

setDriver

public void setDriver(java.lang.String driver)
Set the JDBC driver to be used.

Parameters:
driver - driver class name

setUrl

public void setUrl(java.lang.String url)
Set the DB connection url.

Parameters:
url - connection url

setUserid

public void setUserid(java.lang.String userId)
Set the user name for the DB connection.

Parameters:
userId - database user

setEncoding

public void setEncoding(java.lang.String encoding)
Set the file encoding to use on the sql files read in

Parameters:
encoding - the encoding to use on the files

setPassword

public void setPassword(java.lang.String password)
Set the password for the DB connection.

Parameters:
password - database password

setAutocommit

public void setAutocommit(boolean autocommit)
Set the autocommit flag for the DB connection.

Parameters:
autocommit - the autocommit flag

setDelimiter

public void setDelimiter(java.lang.String delimiter)
Set the statement delimiter.

For example, set this to "go" and delimitertype to "ROW" for Sybase ASE or MS SQL Server.

Parameters:
delimiter -

setDelimiterType

public void setDelimiterType(TorqueSQLExec.DelimiterType delimiterType)
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.

Parameters:
delimiterType -

setPrint

public void setPrint(boolean print)
Set the print flag.

Parameters:
print -

setShowheaders

public void setShowheaders(boolean showheaders)
Set the showheaders flag.

Parameters:
showheaders -

setOutput

public void setOutput(java.io.File output)
Set the output file.

Parameters:
output -

setRdbms

public void setRdbms(java.lang.String vendor)
Set the rdbms required

Parameters:
vendor -

setVersion

public void setVersion(java.lang.String version)
Set the version required

Parameters:
version -

setOnerror

public void setOnerror(TorqueSQLExec.OnError action)
Set the action to perform onerror

Parameters:
action -

execute

public void execute()
             throws org.apache.tools.ant.BuildException
Load the sql file and then execute it

Throws:
org.apache.tools.ant.BuildException

insertDatabaseSqlFiles

private void insertDatabaseSqlFiles(java.lang.String url,
                                    java.lang.String database,
                                    java.util.List transactions)
Take the base url, the target database and insert a set of SQL files into the target database.

Parameters:
url -
database -
transactions -

runStatements

protected void runStatements(java.io.Reader reader,
                             java.io.PrintStream out)
                      throws java.sql.SQLException,
                             java.io.IOException
Read the statements from the .sql file and execute them. Lines starting with '//', '--' or 'REM ' are ignored.

Parameters:
reader -
out -
Throws:
java.sql.SQLException
java.io.IOException

isValidRdbms

protected boolean isValidRdbms(java.sql.Connection conn)
Verify if connected to the correct RDBMS

Parameters:
conn -

execSQL

protected void execSQL(java.lang.String sql,
                       java.io.PrintStream out)
                throws java.sql.SQLException
Exec the sql statement.

Parameters:
sql -
out -
Throws:
java.sql.SQLException

printResults

protected void printResults(java.io.PrintStream out)
                     throws java.sql.SQLException
print any results in the statement.

Parameters:
out -
Throws:
java.sql.SQLException


Copyright © 1999-2003 Apache Software Foundation. All Rights Reserved.