public class SQLExec extends JDBCTask
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.
Modifier and Type | Class and Description |
---|---|
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.
|
description, location, project
Constructor and Description |
---|
SQLExec() |
Modifier and Type | Method and Description |
---|---|
void |
add(ResourceCollection rc)
Adds a collection of resources (nested element).
|
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 java.sql.Connection |
getConnection()
Caches the connection returned by the base class's getConnection method.
|
boolean |
getExpandProperties()
is property expansion inside inline text enabled?
|
protected java.sql.Statement |
getStatement()
Creates and configures a Statement instance which is then
cached for subsequent calls.
|
int |
lastDelimiterPosition(java.lang.StringBuffer buf,
java.lang.String currentLine) |
protected void |
printResults(java.io.PrintStream out)
Deprecated.
since 1.6.x.
Use
the two arg version instead. |
protected void |
printResults(java.sql.ResultSet rs,
java.io.PrintStream out)
print any results in the result set.
|
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 |
setCsvColumnSeparator(java.lang.String s)
The column separator used when printing the results.
|
void |
setCsvQuoteCharacter(java.lang.String s)
The character used to quote column values.
|
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
|
protected void |
setErrorProperty() |
void |
setErrorProperty(java.lang.String errorProperty)
Property to set to "true" if a statement throws an error.
|
void |
setEscapeProcessing(boolean enable)
Set escape processing for statements.
|
void |
setExpandProperties(boolean expandProperties)
Enable property expansion inside nested text
|
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 |
setOutput(Resource output)
Set the output Resource;
optional, defaults to the Ant log.
|
void |
setPrint(boolean print)
Print result sets from the statements;
optional, default false
|
void |
setRawBlobs(boolean rawBlobs)
Set whether to print raw BLOBs rather than their string (hex) representations.
|
protected void |
setRowCountProperty(int rowCount) |
void |
setRowCountProperty(java.lang.String rowCountProperty)
Sets a given property to the number of rows in the first
statement that returned a row count.
|
void |
setShowheaders(boolean showheaders)
Print headers for result sets from the
statements; optional, default true.
|
void |
setShowtrailers(boolean showtrailers)
Print trailing info (rows affected) for the SQL
Addresses Bug/Request #27446
|
void |
setShowWarnings(boolean b)
whether to show SQLWarnings as WARN messages.
|
void |
setSrc(java.io.File srcFile)
Set the name of the SQL file to be run.
|
void |
setStrictDelimiterMatching(boolean b)
If false, delimiters will be searched for in a case-insesitive
manner (i.e.
|
void |
setTreatWarningsAsErrors(boolean b)
Whether a warning is an error - in which case onError aplies.
|
protected void |
setWarningProperty() |
void |
setWarningProperty(java.lang.String warningProperty)
Property to set to "true" if a statement produces a warning.
|
addConnectionProperty, createClasspath, getClasspath, getLoader, getLoaderMap, getPassword, getRdbms, getUrl, getUserId, getVersion, isAutocommit, isCaching, isValidRdbms, setAutocommit, setCaching, setClasspath, setClasspathRef, setDriver, setFailOnConnectionError, setPassword, setRdbms, setUrl, setUserid, setVersion
bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
public void setSrc(java.io.File srcFile)
srcFile
- the file containing the SQL command.public void setExpandProperties(boolean expandProperties)
expandProperties
- if true expand properties.public boolean getExpandProperties()
public void addText(java.lang.String sql)
expandProperties
is set.sql
- an inline string containing the SQL command.public void addFileset(FileSet set)
set
- a set of files contains SQL commands, each File is run in
a separate transaction.public void add(ResourceCollection rc)
rc
- a collection of resources containing SQL commands,
each resource is run in a separate transaction.public SQLExec.Transaction createTransaction()
public void setEncoding(java.lang.String encoding)
encoding
- the encoding to use on the filespublic void setDelimiter(java.lang.String delimiter)
For example, set this to "go" and delimitertype to "ROW" for Sybase ASE or MS SQL Server.
delimiter
- the separator.public void setDelimiterType(SQLExec.DelimiterType delimiterType)
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.
delimiterType
- the type of delimiter - "normal" or "row".public void setPrint(boolean print)
print
- if true print result sets.public void setShowheaders(boolean showheaders)
showheaders
- if true print headers of result sets.public void setShowtrailers(boolean showtrailers)
showtrailers
- if true prints the SQL rows affectedpublic void setOutput(java.io.File output)
output
- the output file to use for logging messages.public void setOutput(Resource output)
output
- the output Resource to store results.public void setAppend(boolean append)
append
- if true append to an existing file.public void setOnerror(SQLExec.OnError action)
action
- the action to perform on statement failure.public void setKeepformat(boolean keepformat)
keepformat
- The keepformat to setpublic void setEscapeProcessing(boolean enable)
enable
- if true enable escape processing, default is true.public void setRawBlobs(boolean rawBlobs)
rawBlobs
- whether to print raw BLOBs.public void setStrictDelimiterMatching(boolean b)
public void setShowWarnings(boolean b)
public void setTreatWarningsAsErrors(boolean b)
public void setCsvColumnSeparator(java.lang.String s)
Defaults to ","
public void setCsvQuoteCharacter(java.lang.String s)
If set, columns that contain either the column separator or the quote character itself will be surrounded by the quote character. The quote character itself will be doubled if it appears inside of the column's value.
If this value is not set (the default), no column values will be quoted, not even if they contain the column separator.
Note: BLOB values will never be quoted.
Defaults to "not set"
public void setErrorProperty(java.lang.String errorProperty)
errorProperty
- the name of the property to set in the
event of an error.public void setWarningProperty(java.lang.String warningProperty)
warningProperty
- the name of the property to set in the
event of a warning.public void setRowCountProperty(java.lang.String rowCountProperty)
public void execute() throws BuildException
execute
in class Task
BuildException
- on error.protected void runStatements(java.io.Reader reader, java.io.PrintStream out) throws java.sql.SQLException, java.io.IOException
reader
- the reader contains sql lines.out
- the place to output results.java.sql.SQLException
- on sql problemsjava.io.IOException
- on io problemsprotected void execSQL(java.lang.String sql, java.io.PrintStream out) throws java.sql.SQLException
sql
- the SQL statement to executeout
- the place to put outputjava.sql.SQLException
- on SQL problemsprotected void printResults(java.io.PrintStream out) throws java.sql.SQLException
the two arg version
instead.out
- the place to print resultsjava.sql.SQLException
- on SQL problems.protected void printResults(java.sql.ResultSet rs, java.io.PrintStream out) throws java.sql.SQLException
rs
- the resultset to print information aboutout
- the place to print resultsjava.sql.SQLException
- on SQL problems.protected java.sql.Connection getConnection()
Subclasses that need to provide a different connection than the base class would, should override this method but keep in mind that this class expects to get the same connection instance on consecutive calls.
returns null if the connection does not connect to the expected RDBMS.
getConnection
in class JDBCTask
protected java.sql.Statement getStatement() throws java.sql.SQLException
Subclasses that want to provide different Statement instances, should override this method but keep in mind that this class expects to get the same connection instance on consecutive calls.
java.sql.SQLException
public int lastDelimiterPosition(java.lang.StringBuffer buf, java.lang.String currentLine)
protected final void setErrorProperty()
protected final void setWarningProperty()
protected final void setRowCountProperty(int rowCount)