org.apache.tools.ant.taskdefs

Class 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

UNKNOWN: name="sql" category="database"

Nested Class Summary
static classSQLExec.DelimiterType
delimiters we support, "normal" and "row"
static classSQLExec.OnError
The action a task should perform on an error, one of "continue", "stop" and "abort"
classSQLExec.Transaction
Contains the definition of a new transaction element.
Method Summary
voidaddFileset(FileSet set)
Adds a set of files (nested fileset attribute).
voidaddText(String sql)
Set an inline SQL command to execute.
SQLExec.TransactioncreateTransaction()
Add a SQL transaction to execute
protected voidexecSQL(String sql, PrintStream out)
Exec the sql statement.
voidexecute()
Load the sql file and then execute it
protected voidprintResults(PrintStream out)
print any results in the statement
protected voidprintResults(ResultSet rs, PrintStream out)
print any results in the result set.
protected voidrunStatements(Reader reader, PrintStream out)
read in lines and execute them
voidsetAppend(boolean append)
whether output should be appended to or overwrite an existing file.
voidsetDelimiter(String delimiter)
Set the delimiter that separates SQL statements.
voidsetDelimiterType(SQLExec.DelimiterType delimiterType)
Set the delimiter type: "normal" or "row" (default "normal").
voidsetEncoding(String encoding)
Set the file encoding to use on the SQL files read in
voidsetEscapeProcessing(boolean enable)
Set escape processing for statements.
voidsetKeepformat(boolean keepformat)
whether or not format should be preserved.
voidsetOnerror(SQLExec.OnError action)
Action to perform when statement fails: continue, stop, or abort optional; default "abort"
voidsetOutput(File output)
Set the output file; optional, defaults to the Ant log.
voidsetPrint(boolean print)
Print result sets from the statements; optional, default false
voidsetShowheaders(boolean showheaders)
Print headers for result sets from the statements; optional, default true.
voidsetSrc(File srcFile)
Set the name of the SQL file to be run.

Method Detail

addFileset

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

addText

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

createTransaction

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

execSQL

protected void execSQL(String sql, PrintStream out)
Exec the sql statement.

execute

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

printResults

protected void printResults(PrintStream out)

Deprecated: use {@link #printResults(java.sql.ResultSet, java.io.PrintStream) the two arg version} instead.

print any results in the statement

Parameters: out the place to print results

Throws: SQLException on SQL problems.

printResults

protected void printResults(ResultSet rs, PrintStream out)
print any results in the result set.

Parameters: rs the resultset to print information about out the place to print results

Throws: SQLException on SQL problems.

Since: Ant 1.6.3

runStatements

protected void runStatements(Reader reader, PrintStream out)
read in lines and execute them

setAppend

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

Since: Ant 1.5

setDelimiter

public void setDelimiter(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.

setEncoding

public void setEncoding(String encoding)
Set the file encoding to use on the SQL files read in

Parameters: encoding the encoding to use on the files

setEscapeProcessing

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

Since: Ant 1.6

setKeepformat

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

Parameters: keepformat The keepformat to set

setOnerror

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

setOutput

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

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.

setSrc

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