org.apache.tools.ant.taskdefs
Class SQLExec

java.lang.Object
  |
  +--org.apache.tools.ant.ProjectComponent
        |
        +--org.apache.tools.ant.Task
              |
              +--org.apache.tools.ant.taskdefs.JDBCTask
                    |
                    +--org.apache.tools.ant.taskdefs.SQLExec

public class SQLExec
extends JDBCTask

Executes a series of SQL statements on a database using JDBC.

Statements can either be read in from a text file using the src attribute or from between the enclosing SQL tags.

Multiple statements can be provided, separated by semicolons (or the defined delimiter). Individual lines within the statements can be commented using either --, // or REM at the start of the line.

The autocommit attribute specifies whether auto-commit should be turned on or off whilst executing the statements. If auto-commit is turned on each statement will be executed and committed. If it is turned off the statements will all be executed as one transaction.

The onerror attribute specifies how to proceed when an error occurs during the execution of one of the statements. The possible values are: continue execution, only show the error; stop execution and commit transaction; and abort execution and transaction and fail task.

Since:
Ant 1.2

Nested Class Summary
static class SQLExec.DelimiterType
          delimiters we support, "normal" and "row"
static class SQLExec.OnError
          The action a task should perform on an error, one of "continue", "stop" and "abort"
 class SQLExec.Transaction
          Contains the definition of a new transaction element.
 
Field Summary
 
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
SQLExec()
           
 
Method Summary
 void addFileset(FileSet set)
          Adds a set of files (nested fileset attribute).
 void addText(java.lang.String sql)
          Set an inline SQL command to execute.
 SQLExec.Transaction createTransaction()
          Add a SQL transaction to execute
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
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 in lines and execute them
 void setAppend(boolean append)
          whether output should be appended to or overwrite an existing file.
 void setDelimiter(java.lang.String delimiter)
          Set the delimiter that separates SQL statements.
 void setDelimiterType(SQLExec.DelimiterType delimiterType)
          Set the delimiter type: "normal" or "row" (default "normal").
 void setEncoding(java.lang.String encoding)
          Set the file encoding to use on the SQL files read in
 void setEscapeProcessing(boolean enable)
          Set escape processing for statements.
 void setKeepformat(boolean keepformat)
          whether or not format should be preserved.
 void setOnerror(SQLExec.OnError action)
          Action to perform when statement fails: continue, stop, or abort optional; default "abort"
 void setOutput(java.io.File output)
          Set the output file; optional, defaults to the Ant log.
 void setPrint(boolean print)
          Print result sets from the statements; optional, default false
 void setShowheaders(boolean showheaders)
          Print headers for result sets from the statements; optional, default true.
 void setSrc(java.io.File srcFile)
          Set the name of the SQL file to be run.
 
Methods inherited from class org.apache.tools.ant.taskdefs.JDBCTask
createClasspath, getClasspath, getConnection, getLoader, getLoaderMap, getPassword, getRdbms, getUrl, getUserId, getVersion, isAutocommit, isCaching, isValidRdbms, setAutocommit, setCaching, setClasspath, setClasspathRef, setDriver, setPassword, setRdbms, setUrl, setUserid, setVersion
 
Methods inherited from class org.apache.tools.ant.Task
getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, reconfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
 
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
 

Constructor Detail

SQLExec

public SQLExec()
Method Detail

setSrc

public void setSrc(java.io.File srcFile)
Set the name of the SQL file to be run. Required unless statements are enclosed in the build file


addText

public void addText(java.lang.String sql)
Set an inline SQL command to execute. NB: Properties are not expanded in this text.


addFileset

public void addFileset(FileSet set)
Adds a set of files (nested fileset attribute).


createTransaction

public SQLExec.Transaction createTransaction()
Add a SQL transaction to execute


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

setDelimiter

public void setDelimiter(java.lang.String delimiter)
Set the delimiter that separates SQL statements. Defaults to ";"; optional

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


setDelimiterType

public void setDelimiterType(SQLExec.DelimiterType delimiterType)
Set the delimiter type: "normal" or "row" (default "normal").

The delimiter type takes two values - normal and row. Normal means that any occurrence 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.


setPrint

public void setPrint(boolean print)
Print result sets from the statements; optional, default false


setShowheaders

public void setShowheaders(boolean showheaders)
Print headers for result sets from the statements; optional, default true.


setOutput

public void setOutput(java.io.File output)
Set the output file; optional, defaults to the Ant log.


setAppend

public void setAppend(boolean append)
whether output should be appended to or overwrite an existing file. Defaults to false.

Since:
Ant 1.5

setOnerror

public void setOnerror(SQLExec.OnError action)
Action to perform when statement fails: continue, stop, or abort optional; default "abort"


setKeepformat

public void setKeepformat(boolean keepformat)
whether or not format should be preserved. Defaults to false.

Parameters:
keepformat - The keepformat to set

setEscapeProcessing

public void setEscapeProcessing(boolean enable)
Set escape processing for statements.

Since:
Ant 1.6

execute

public void execute()
             throws BuildException
Load the sql file and then execute it

Overrides:
execute in class Task
Throws:
BuildException - if something goes wrong with the build

runStatements

protected void runStatements(java.io.Reader reader,
                             java.io.PrintStream out)
                      throws java.sql.SQLException,
                             java.io.IOException
read in lines and execute them

java.sql.SQLException
java.io.IOException

execSQL

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

java.sql.SQLException

printResults

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

java.sql.SQLException


Copyright © 2000-2004 Apache Software Foundation. All Rights Reserved.