Package org.oddjob.sql
Class SQLJob
java.lang.Object
org.oddjob.sql.SQLJob
- All Implemented Interfaces:
Serializable,Runnable,ArooaSessionAware,Stoppable
- Author:
- rob and Ant.
- See Also:
Description
Runs one or more SQL statements.
Parsing
The SQL will be parsed and broken into individual statements before being executed using JDBC. The statements are broken according to thedelimiter and delimiterType properties.
Setting the expandProperties property to true will cause
Oddjob to expand ${} expressions within the SQL. Comments are achieved
by starting a line with -- or // or
REM. Note that /* */ is not yet supported.
Result Processing
An optional result processor may be provided.SQLResultsSheet
allows the results to be displayed on a result sheet in a similar style
to an SQL query tool. SQLResultsBean allows results to be
captured as beans who's properties can be used elsewhere in Oddjob.
Errors and Auto Commit
TheonError property controls what to do if a statement fails.
By default it is ABORT. Auto commit is false by default so the changes
are rolled back. If auto commit is true the ABORT has the same affect as
STOP which commits statements already executed.
Parameterised Statements and Procedures
SQL statements can be parameterised, and can be stored procedure or function calls. Out parameter values can also be accessed and used elsewhere in Oddjob by wrapping them with anIdentifiableValueType. See example 2 for an example of this.
Caveats
SQLServer stored procedures with parameters must be made using the JDBC style call. E.g. { call sp_help(?) } otherwise an exception is thrown fromgetParameterMetaData.Example
A simple example shows first the execution of multiple statements, then a simple parameterised query.
<oddjob>
<job>
<sequential>
<jobs>
<variables id="vars">
<connection>
<connection driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:mem:test" username="sa"/>
</connection>
</variables>
<sql name="Create table">
<connection>
<value value="${vars.connection}"/>
</connection>
<input>
<buffer>
create table GREETINGS(STYLE varchar(20),
TEXT varchar(20))
insert into GREETINGS values('nice', 'Hello')
insert into GREETINGS values('grumpy', 'Bah Humbug')
</buffer>
</input>
</sql>
<sql id="query">
<connection>
<value value="${vars.connection}"/>
</connection>
<input>
<buffer>
select TEXT from GREETINGS where STYLE = ?
</buffer>
</input>
<parameters>
<value value="nice"/>
</parameters>
<results>
<sql-results-bean/>
</results>
</sql>
<echo name="Single Row Result">${query.results.row.TEXT}</echo>
<echo name="Result By Row Index">${query.results.rows[0].TEXT}</echo>
</jobs>
</sequential>
</job>
</oddjob>
The results are made available to the echo jobs using a
SQLResultsBean.An Callable Statement example. Showing support for IN, INOUT, and OUT
parameters. Note that declaring the stored procedure requires a change
in delimiter otherwise the semicolon is interrupted as an end of
statement.
<oddjob>
<job>
<sequential>
<jobs>
<variables id="vars">
<connection>
<connection driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:mem:test" username="sa"/>
</connection>
</variables>
<sql callable="true" delimiterType="ROW">
<connection>
<value value="${vars.connection}"/>
</connection>
<input>
<buffer>
create procedure TEST (out a int, inout b int, in c int)
MODIFIES SQL DATA
begin atomic
set a = b;
set b = c;
end
</buffer>
</input>
</sql>
<sql id="sql-call" callable="true">
<connection>
<value value="${vars.connection}"/>
</connection>
<parameters>
<identify id="a">
<value>
<value/>
</value>
</identify>
<identify id="b">
<value>
<value value="2"/>
</value>
</identify>
<value value="3"/>
</parameters>
<input>
<buffer>
call TEST (?, ?, ?)
</buffer>
</input>
</sql>
<echo>a=${a}, b=${b}.</echo>
</jobs>
</sequential>
</job>
</oddjob>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumdelimiter type between SQL statements.static enumThe action a task should perform on an error. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the statement delimiter.Get the delimiter type.Getter for dialect.Get the input encoding name.intbooleanIs property expansion inside inline text enabled?getName()Get the name.Get on error action.getParameters(int index) Indexed getter for parameter types.Getter for results.intbooleanGetter for autocommit.booleanIs the statement a stored procedure.booleanGetter for escapeProcessing.booleanGet if SQL keeps input format.voidrun()voidsetArooaSession(ArooaSession session) Accept the current Arooa Session.voidsetAutocommit(boolean autocommit) voidsetCallable(boolean callable) voidsetConnection(Connection connection) voidsetDelimiter(String delimiter) voidsetDelimiterType(SQLJob.DelimiterType delimiterType) voidsetDialect(DatabaseDialect dialect) voidsetEncoding(String encoding) voidsetEscapeProcessing(boolean enable) voidsetExpandProperties(boolean expandProperties) voidsetInput(InputStream sql) voidsetKeepFormat(boolean keepFormat) voidSet the namevoidsetOnError(SQLJob.OnError action) voidsetParameters(int index, ValueType parameter) voidsetResults(Consumer<Object> results) Setter for results.voidstop()Stop executing.toString()
-
Constructor Details
-
SQLJob
public SQLJob()Constructor.
-
-
Method Details
-
setArooaSession
Description copied from interface:ArooaSessionAwareAccept the current Arooa Session.- Specified by:
setArooaSessionin interfaceArooaSessionAware- Parameters:
session- The Arooa Session. Never null.
-
getName
Get the name.- Returns:
- The name.
-
setName
Set the name- Parameters:
name- The name.
-
run
public void run() -
stop
public void stop()Description copied from interface:StoppableStop executing. This method should not return until the Stoppable has actually stopped. -
getResults
Getter for results.- Returns:
- Result Handler. May be null.
-
setResults
Setter for results.- Parameters:
results- Result Handler. May be null.
-
setInput
- Reference Property:
- input
- Required:
- Yes.
Description
The input from where to read the SQL query or DML statement(s) to run. Probably eitherFileTypefor reading the SQL from a file orBufferTypefor configuring the SQL in line. -
setExpandProperties
public void setExpandProperties(boolean expandProperties) - Reference Property:
- expandProperties
- Required:
- No, defaults to false.
Description
Enable property expansion inside the SQL statements read from the input. -
getExpandProperties
public boolean getExpandProperties()Is property expansion inside inline text enabled?- Returns:
- true if properties are to be expanded.
-
setEncoding
- Reference Property:
- encoding
- Required:
- No.
Description
Set the string encoding to use on the SQL read in. -
getEncoding
Get the input encoding name.- Returns:
-
setDelimiter
- Parameters:
delimiter- the separator.- Reference Property:
- delimiter
- Required:
- No. Defaults to ;
Description
Set the delimiter that separates SQL statements. Defaults to a semicolon.For scripts that use a separate line delimiter like "GO" also set the
delimiterTypeto "ROW".The delimiter is case insensitive so either "GO" or "go" can be used interchangeably.
-
getDelimiter
Get the statement delimiter.- Returns:
-
setDelimiterType
- Parameters:
delimiterType- the type of delimiter - "NORMAL" or "ROW".- Reference Property:
- delimiterType
- Required:
- No, defaults to NORMAL.
Description
Set the delimiter type: NORMAL or ROW.NORMAL means that any occurrence of the delimiter terminates the SQL command whereas with ROW, only a line containing just the delimiter is recognised as the end of the command.
ROW is used with delimiters such as GO.
-
getDelimiterType
Get the delimiter type.- Returns:
-
setKeepFormat
public void setKeepFormat(boolean keepFormat) - Parameters:
keepFormat- The true or false to keep the format of the SQL.- Reference Property:
- keepFormat
- Required:
- No. Defaults to false.
Description
Whether or not the format of the SQL should be preserved. -
isKeepFormat
public boolean isKeepFormat()Get if SQL keeps input format.- Returns:
-
setConnection
- Reference Property:
- connection
- Required:
- Yes.
Description
The connection to use. This can be provided by aConnectionTypeor by some other means such as custom data source. This SQL job will always close the connection once it has run. -
setAutocommit
public void setAutocommit(boolean autocommit) - Parameters:
autocommit-- Reference Property:
- autocommit
- Required:
- No, defaults to false.
Description
Autocommit statements once executed. -
isAutocommit
public boolean isAutocommit()Getter for autocommit.- Returns:
-
getParameters
Indexed getter for parameter types.- Parameters:
index-- Returns:
-
setParameters
- Reference Property:
- parameters
- Required:
- No.
Description
Parameters to be bound to statement(s). This is either aValueTypeor anIdentifiableValueTypeif the parameter is an out parameter that is to be identifiable by an id for other jobs to access. -
setCallable
public void setCallable(boolean callable) - Reference Property:
- callable
- Required:
- No, defaults to false.
Description
If the statement calls a stored procedure. -
isCallable
public boolean isCallable()Is the statement a stored procedure.- Returns:
-
setEscapeProcessing
public void setEscapeProcessing(boolean enable) - Parameters:
enable- if true enable escape processing, default is true.- Reference Property:
- escapeProcessing
- Required:
- No, defaults to false.
Description
Set escape processing for statements. See the java doc forStatement.setEscapeProcessingfor more information. -
isEscapeProcessing
public boolean isEscapeProcessing()Getter for escapeProcessing.- Returns:
-
setDialect
- Parameters:
dialect- The Database Dialect.- Reference Property:
- dialect
- Required:
- No. A default is used.
Description
Allows aDatabaseDialectto be provided that can tune the way the result set is processed. -
getDialect
Getter for dialect.- Returns:
-
setOnError
- Parameters:
action- the action to perform on statement failure.- Reference Property:
- onError
- CONTINUE
- Ignore the failure and continue executing.
- STOP
- Commit what has been executed but don't execute any more.
- ABORT
- Rollback what has been executed and don't execute any more.
- Required:
- No, defaults to ABORT.
Description
What to do when a statement fails:autocommitis true then ABORT behaves like STOP as no roll back is possible. -
getOnError
Get on error action.- Returns:
-
getExecutedSQLCount
public int getExecutedSQLCount()- Returns:
- The number.
- Reference Property:
- executedSQLCount
Description
The number of SQL statements executed. -
getSuccessfulSQLCount
public int getSuccessfulSQLCount()- Returns:
- The number.
- Reference Property:
- successfulSQLCount
Description
The number of SQL statements successfully executed. -
toString
-