R
- class type which contains members for columns of a row in a tablepublic abstract class SqlOperation<R>
extends java.lang.Object
implements java.lang.AutoCloseable
Constructor and Description |
---|
SqlOperation(Table<R> table)
Constructs for a table.
|
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Requests cancel for currently executing statement.
|
protected void |
cascade(R row,
boolean post)
Executes all cascade operations that were created by
prepareCascades() . |
void |
close()
Cleans up after operation is no longer needed.
|
protected void |
closeCascades()
Closes the
CascadeOperation objects for this operation. |
protected void |
closeStatement()
Closes the prepared statement for this operation.
|
protected RowField<R,?> |
createRowField(Table<R> targetTable,
java.lang.reflect.Field field)
|
abstract void |
execute()
Invokes an execute method on a prepared statement.
|
protected java.lang.String |
getBaseSql()
Gets the sql used by this operation.
|
int |
getCascadeDepth()
Gets the depth of the cascade graph relative to the root cascade operation.
|
protected java.sql.Connection |
getConnection()
Gets the JDBC connection from the
Database associated with this operation. |
java.lang.String |
getCustomSql()
Gets custom sql set with
setCustomSql(String) . |
java.util.Map<java.lang.String,java.lang.Object> |
getNamedParameterMap()
Gets map of all named parameters.
|
protected int |
getNextParameter()
Gets the next JDBC parameter number used by
PreparedStatement to set parameters. |
OperationTime |
getOperationTime()
Gets the timings for this operation that have accumulated since
setTimings(boolean)
have been enabled. |
java.lang.Object |
getParameter(java.lang.String name)
Gets value of named parameter that was set with
setParameter(String, Object) or
setNamedParameterMap(Map) . |
java.lang.Object[] |
getParameters()
Gets parameters that were set by
setParameters(Object...) . |
java.lang.String |
getPreparedSql()
Gets sql that was used in
prepare() . |
protected java.sql.PreparedStatement |
getPreparedStatement()
Gets the prepared statement used by this operation.
|
int |
getQueryTimeout()
Gets the number of seconds the driver will wait for a Statement object to execute.
|
java.lang.String[] |
getRequiredCascades()
Gets the required cascade names.
|
protected java.lang.String |
getSql()
Gets full sql statement to prepare.
|
Table<R> |
getTable()
Gets the table provided in the constructor.
|
protected Table<?> |
getTargetTable(java.lang.Class<?> targetClass)
Gets a table object from database associated with this operation.
|
java.lang.String |
getTimingId()
Gets the timing id for this operation.
|
Where |
getWhereAnnotation()
Gets the where annotation in use by this operation.
|
java.lang.String |
getWhereConditionName()
Gets where condition name supplied in
setWhere(String) |
AbstractWhereTranslator<R> |
getWhereTranslator()
Gets the translator to map row object values into where condition.
|
protected void |
initOperationTime()
Initializes
OperationTime object that will record elapsed times for this operation. |
boolean |
isCached()
Gets the caching mode.
|
boolean |
isCascade()
Gets cascade status.
|
protected boolean |
isCascading()
Indicates that cascades will occur with this operation.
|
boolean |
isIncludeIdentityColumns()
Tests if identity columns are used in this operation.
|
boolean |
isPrimaryKey()
Gets primary key status.
|
boolean |
isReadOnly()
Gets read-only indicator.
|
boolean |
isRequiredCascade(java.lang.String cascadeName)
Tests if a cascade name equals current required cascade names.
|
boolean |
isTimings() |
void |
logTimings()
Logs current timings for this operation to log.
|
protected void |
prepare()
Creates prepared statement for this operation and invokes
prepareCascades()
to prepare statements for any cascade operations. |
protected void |
prepareCascades()
Prepares cascades for all cascade annotations on row class.
|
protected abstract java.util.List<CascadeOperation<R,?>> |
prepareCascades(java.lang.reflect.Field field)
Creates and prepares cascade operations based upon cascade annotations for a field.
|
protected void |
prepareCheck()
Prepares statement with
prepare() if it is null. |
protected java.sql.PreparedStatement |
prepareStatement()
Creates the prepared statement.
|
protected void |
setBaseSql(java.lang.String sql)
Sets the base sql used by this operation.
|
void |
setCached(boolean cached)
Sets the cache mode for this operation.
|
void |
setCascade(boolean cascade)
Sets whether cascades are enabled for this operation.
|
void |
setCascadeDepth(int cascadeDepth)
Sets the cascade depth.
|
void |
setCustomSql(java.lang.String customSql)
Sets sql to be appended to base sql in operation.
|
void |
setIncludeIdentityColumns(boolean includeIdentityColumns)
Sets when to generate identity columns.
|
void |
setNamedParameterMap(java.util.Map<java.lang.String,java.lang.Object> namedParameterMap)
Sets all named parameters replacing any previously set with
setParameter(String, Object) . |
protected void |
setNextParameter(int nextParameter)
Sets the next column index to use in
PreparedStatement . |
void |
setParameter(java.lang.String name,
java.lang.Object value)
Sets a named parameter value.
|
void |
setParameters(java.lang.Object... parameters)
Sets any parameters to be used by operation.
|
void |
setQueryTimeout(int queryTimeout)
Sets the number of seconds the driver will wait for a Statement object to execute.
|
void |
setReadOnly(boolean readOnly)
Sets read-only indicator.
|
void |
setRequiredCascades(java.lang.String... cascadeNames)
Sets the name(s) of cascades that should occur with this operation.
|
void |
setTimingId(java.lang.String timingId)
Sets the id for the operation times
getOperationTime() . |
void |
setTimings(boolean on)
Enables timings for this operation.
|
void |
setWhere(java.lang.String whereConditionName)
Sets where condition from annotation name as defined in
Where.name() for row. |
void |
setWhereTranslator(AbstractWhereTranslator<R> whereTranslator)
Sets the translator to map row object values into where condition.
|
protected void |
writeColumns(R row)
Sets all column parameters from a row using the table's
RowTranslator . |
protected <T> void |
writeParameter(int parameterIndex,
T parameter)
Sets parameter on prepared statement using the appropriate
TypeTranslator . |
protected void |
writeParameters()
Prepares statement and then sets all parameters with
writeParameter(int, Object) . |
protected void |
writeWhere(R row)
Sets all where parameters from a row using the table's
WhereTranslator . |
public SqlOperation(Table<R> table) throws OperationException
The operation timings default to the current state of the database
Database.isTimings()
. If timings are enabled for database then
all operations will be timed unless explicitly disabled. Operation
will be read only setReadOnly(boolean)
if either
Database.isReadOnly()
or Table.isReadOnly()
are true.
table
- operations are performed on this tableOperationException
- if errorpublic void setParameter(java.lang.String name, java.lang.Object value)
Named parameters are used when no positional parameters are set with setParameters(Object...)
.
When no positional parameters are available, getParameter(String)
is used to get parameter values
for all fields defined by Where
using Where.fieldNames()
or WhereField.name()
as named parameter key.
Named parameters are also looked up for cascades when
SelectCascade.sourceParameterFieldNames()
contains any field names that begin with dollar ($).
symbol. Named parameter map is supplied for all cascade levels so that any level can get value from map.
name
- name of parametervalue
- value of named parameterpublic java.lang.Object getParameter(java.lang.String name)
setParameter(String, Object)
or
setNamedParameterMap(Map)
.name
- parameter namepublic void setParameters(java.lang.Object... parameters)
Column.primaryKey()
or Where
annotation
or Row.primaryKeyFields()
.parameters
- parameters as objects (not from row fields)public java.lang.Object[] getParameters()
setParameters(Object...)
.public java.util.Map<java.lang.String,java.lang.Object> getNamedParameterMap()
public void setNamedParameterMap(java.util.Map<java.lang.String,java.lang.Object> namedParameterMap)
setParameter(String, Object)
.namedParameterMap
- map of named parameters (key is parameter name); null for no named parameters
parameterspublic boolean isReadOnly()
Database.isReadOnly()
public void setReadOnly(boolean readOnly)
ReadOnlyException
if it attempts to modify the database.
By default read-only is set from the database associated with this
operation, Database.isReadOnly()
. Set to true as a safe-guard
to prevent accidental modification of database.readOnly
- true to prevent modify operationsDatabase.setReadOnly(boolean)
public int getQueryTimeout()
Statement.getQueryTimeout()
public void setQueryTimeout(int queryTimeout)
prepare()
.queryTimeout
- the new query timeout limit in seconds; zero means there is no limitStatement.setQueryTimeout(int)
public int getCascadeDepth()
public void setCascadeDepth(int cascadeDepth)
CascadeOperation
cascadeDepth
- 0..npublic boolean isCached()
public void setCached(boolean cached)
WriteOperations
when writing cache changes to the database.
Setting to true enables caching only if Cached annotation is specified for row/table.
cached
- true to allow caching; false to prevent caching of rows for this operationpublic boolean isCascade()
setCascade(boolean)
.public void setCascade(boolean cascade)
Set to false by WriteOperations
when writing cache changes to the database since
cascades for cached tables are performed at the time the row is put into cache.
cascade
- true to perform cascades; false to ignore cascadesprotected boolean isCascading()
isCascade()
and at least one cascade operation is definedprotected void writeParameters() throws OperationException
writeParameter(int, Object)
.
prepareCheck()
or prepare()
must be invoked prior to using this method.OperationException
- if errorprotected <T> void writeParameter(int parameterIndex, T parameter) throws java.lang.Exception
TypeTranslator
.T
- type of parameterparameterIndex
- jdbc prepared statement parameter indexparameter
- parameter valuejava.lang.Exception
- if no translator exists for parameter class or error writing parameterpublic abstract void execute() throws OperationException
prepareCheck()
should be invoked
prior to executing sql or use some other means of set up for prepared statement.OperationException
- if errorpublic void cancel() throws OperationException
OperationException
- if errorStatement.cancel()
public void close() throws OperationException
CascadeOperation
objects. This method should
be invoked when operation is no longer needed for proper JDBC clean up.close
in interface java.lang.AutoCloseable
OperationException
- if errorpublic java.lang.String getTimingId()
getSql()
.public void setTimingId(java.lang.String timingId)
getOperationTime()
. Typically the default id is
sufficient. The id is a key into Database.getOperationTimeMap()
which is used
to sum multiple instances of timings for this operation.
Use a custom id to force all operations that use the custom id to be summed into one
instance of OperationTime
in Database.getOperationTimeMap()
. A good
custom id to use would be the class name of the class that is using the operation,
Class.getName()
.
timingId
- unique id associated with an operation(s)public void setTimings(boolean on)
getOperationTime()
. Use logTimings()
to
write timings to log for this operation. Use Database.logTimings()
to write all operation
timings to log.
When on parameter is true, then instance of OperationTime
is created
at the start of execution for use by this operation. Use getOperationTime()
to
change default values.
on
- true to recording execution times for this operationpublic boolean isTimings()
public void logTimings()
OperationTime.logTimings()
.public OperationTime getOperationTime()
setTimings(boolean)
have been enabled.NoOperationTime
if
timings are not enabledprotected void initOperationTime()
OperationTime
object that will record elapsed times for this operation.
This method can't be invoked until timing id and/or sql is known so it is invoked by
execute()
.protected void closeStatement() throws OperationException
OperationException
- if errorprotected void closeCascades() throws OperationException
CascadeOperation
objects for this operation.OperationException
- if errorprotected void prepareCheck() throws OperationException
prepare()
if it is null. Invoke prior to using prepared statement to
insure that prepared statement has been set up through Connection.prepareStatement(String)
.
Usually invoked prior to invoking a method on PreparedStatement
.OperationException
- if errorprotected void prepare() throws OperationException
prepareCascades()
to prepare statements for any cascade operations.OperationException
- if errorprotected java.sql.PreparedStatement prepareStatement() throws java.sql.SQLException
java.sql.SQLException
- if errorprotected void prepareCascades() throws OperationException
OneToManyCascade
, OneToOneCascade
, and Cascade
.OperationException
- if errorprotected abstract java.util.List<CascadeOperation<R,?>> prepareCascades(java.lang.reflect.Field field) throws OperationException
field
- annotation is for this field of row class ROperationException
- if errorprotected void cascade(R row, boolean post) throws OperationException
prepareCascades()
.row
- instance of source rowpost
- true if post cascades are to be executed; false if pre cascades are
to be executedOperationException
- if errorprotected Table<?> getTargetTable(java.lang.Class<?> targetClass) throws OperationException
targetClass
- class that cascade is to affectOperationException
- if errorprotected RowField<R,?> createRowField(Table<R> targetTable, java.lang.reflect.Field field) throws OperationException
RowField
from a Field
and Table
. Returned RowField
will be either MethodAccessField
or DirectAccessField
based upon values
used for Column.fieldAccess()
and Row.fieldAccess()
.
Typically this method is used to create a field that will receive value from a cascade.
targetTable
- table that reads/writes rows that contain fieldfield
- create access to this fieldMethodAccessField
or DirectAccessField
annotation(s)OperationException
- if errorprotected java.lang.String getSql()
prepare()
public java.lang.String getPreparedSql()
prepare()
.public void setCustomSql(java.lang.String customSql)
customSql
- additional sql to be added to base sql or null for nonepublic java.lang.String getCustomSql()
setCustomSql(String)
.public Table<R> getTable()
protected java.sql.Connection getConnection()
Database
associated with this operation.Connection
public void setWhere(java.lang.String whereConditionName) throws OperationException
Where.name()
for row.whereConditionName
- name of where condition to use;
"primaryKey" for key defined by Column.primaryKey()
, Column.identity()
,
or Row.primaryKeyFields()
; empty string for no where conditionOperationException
- if errorpublic java.lang.String getWhereConditionName()
setWhere(String)
public Where getWhereAnnotation()
setWhere(String)
or null if nonepublic boolean isPrimaryKey()
public boolean isIncludeIdentityColumns()
Column.identity()
public void setIncludeIdentityColumns(boolean includeIdentityColumns)
includeIdentityColumns
- true to include identity columns in sqlpublic void setRequiredCascades(java.lang.String... cascadeNames)
Table.getRequiredCascades()
.
For all cascades that are executed, cascadeNames is passed on to the cascade operation so that all cascades for all levels use the same required cascade names.
The wildcard "*" parameter will result in StackOverflowError
if cascade relationships form
a cyclic graph and no termination condition exists to end the recursion.
cascadeNames
- name(s) of cascades to use; "*" for wildcard to use all cascadespublic java.lang.String[] getRequiredCascades()
public boolean isRequiredCascade(java.lang.String cascadeName)
cascadeName
- check this nameprotected void writeColumns(R row) throws OperationException
RowTranslator
.row
- get column values from this rowOperationException
- if errorprotected void writeWhere(R row) throws OperationException
WhereTranslator
.row
- get where parameters from this rowOperationException
- if errorprotected java.lang.String getBaseSql()
protected void setBaseSql(java.lang.String sql)
getBaseSql()
for details.sql
- base sql used by this operationprotected java.sql.PreparedStatement getPreparedStatement()
public AbstractWhereTranslator<R> getWhereTranslator()
public void setWhereTranslator(AbstractWhereTranslator<R> whereTranslator)
whereTranslator
- where translator or null if noneprotected int getNextParameter()
PreparedStatement
to set parameters.
Parameter number changes as column and where conditions are prepared. Parameter numbers
start at 1 and occur for every "?" in the SQL statement. The parameter numbers are used
as the first parameter in the various PreparedStatement
set methods.PreparedStatement
parameter to useprotected void setNextParameter(int nextParameter)
PreparedStatement
. See getNextParameter()
for details.nextParameter
- the next PreparedStatement
parameter to use