java.lang.Object
org.tentackle.dbms.StatementWrapper
org.tentackle.dbms.PreparedStatementWrapper
- All Implemented Interfaces:
AutoCloseable,BackendPreparedStatement
A wrapper for prepared statements.
Will catch and report SQLExceptions and keep track of being used only once after
Will catch and report SQLExceptions and keep track of being used only once after
Db.getPreparedStatement(org.tentackle.dbms.StatementKey, boolean, int, int, org.tentackle.dbms.SqlSupplier).- Author:
- harald
-
Field Summary
Fields inherited from class org.tentackle.dbms.StatementWrapper
cancelled, con, openResultSet, parallelOk, ready, running, sql, stackTrace, stmt -
Constructor Summary
ConstructorsConstructorDescriptionPreparedStatementWrapper(ManagedConnection con, PreparedStatement stmt, StatementKey statementKey, String sql) Creates a wrapper for a prepared statement. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBatch()Adds a set of parameters to thisPreparedStatement's batch of commands.voidvoidEmpties thisStatementobject's current list of batched SQL commands.voidClears the current parameter values immediately.voidclose()Closes this statement.protected voidDetach the session from the connection.
Statements detach the session on executeUpdate or on close() in theResultSetWrapperafter executeQuery.int[]executeBatch(boolean finish) Submits a batch of commands.Executes the query.executeQuery(boolean withinTx) Executes the query.protected ResultSetexecuteQueryImpl(String sql) Implementation of executeQuery.intExecutes the update.intexecuteUpdate(String sql) Method must not be invoked on aPreparedStatementWrapper.protected intexecuteUpdateImpl(String sql) Implementation of executeUpdate.protected DbBatchgetBatch()Gets the statement batch.intGets the number of pending addBatch invocations.Gets the parameter map for a batched statement.intGets the column offset.Gets the parameter map.Gets the wrapped prepared statement.Gets the statement key.protected booleanReturns whether statement is batched.booleanReturns whether parameters have been set so that execution is pending.voidMarks the statement to be ready for being consumed by aDbattached to aConnectionManager.
This is an additional measure to enforce the programming rule that a statement is being used only once afterDb.createStatement()(for non-prepared statements) or afterDb.getPreparedStatement(org.tentackle.dbms.StatementKey, boolean, int, int, org.tentackle.dbms.SqlSupplier)for prepared statements.protected voidrememberParameter(int p, Object value) Sets the parameter to be remembered for diagnostics.<T> int<T> voidvoidvoidsetArray(int p, Class<?> type, int columnIndex, Collection<?> elements, String operator) protected voidSets the statement batch if batched within the current transaction.voidsetBigDecimal(int p, BigDecimal d) voidsetBinary(int p, org.tentackle.common.Binary<?> b) Sets the designated parameter to the givenBinaryvalue.voidsetBMoney(int p, org.tentackle.common.BMoney m) Sets the designated parameter to aBMoneyvalue.
A BMoney will not be stored as a single field but as two fields: a double representing the value an int representing the scale This is due to most DBMS can't store arbitrary scaled decimals in a single column, i.e. all values in the column must have the same scale.voidsetBoolean(int p, boolean b) voidsetBoolean(int p, Boolean b) voidsetByte(int p, byte b) voidvoidsetChar(int p, char c) voidsetCharacter(int p, Character c) voidsetCharacter(int p, Character c, boolean mapNull) voidsetColumnOffset(int columnOffset) Sets the column offset.voidvoidvoidvoidvoidsetDMoney(int p, org.tentackle.common.DMoney m) Sets the designated parameter to aDMoneyvalue.
A DMoney will not be stored as a single field but as two fields: a BigDecimal with a scale of 0 representing the value an int representing the scale This is due to most DBMS can't store arbitrary scaled decimals in a single column, i.e. all values in the column must have the same scale.voidsetDouble(int p, double d) voidvoidsetFloat(int p, float f) voidvoidsetInstant(int p, Instant ts) voidsetInstant(int p, Instant ts, boolean mapNull) voidsetInt(int p, int i) voidsetInteger(int p, Integer i) voidsetLargeString(int p, String s) voidsetLargeString(int p, String s, boolean mapNull) voidsetLocalDate(int p, LocalDate d) voidsetLocalDate(int p, LocalDate d, boolean mapNull) voidsetLocalDateTime(int p, LocalDateTime ts) voidsetLocalDateTime(int p, LocalDateTime ts, boolean mapNull) voidsetLocalTime(int p, LocalTime t) voidsetLong(int p, long l) voidvoidsetNull(int p, int type) voidsetOffsetDateTime(int p, OffsetDateTime ts) voidsetOffsetDateTime(int p, OffsetDateTime ts, boolean mapNull) voidsetOffsetTime(int p, OffsetTime t) voidsetShort(int p, short s) voidvoidvoidvoidvoidvoidsetTimestamp(int p, Timestamp ts) voidsetTimestamp(int p, Timestamp ts, boolean mapNull) voidsetTimestamp(int p, Timestamp ts, Calendar timezone) voidsetTimestamp(int p, Timestamp ts, Calendar timezone, boolean mapNull) voidsetZonedDateTime(int p, ZonedDateTime ts) voidsetZonedDateTime(int p, ZonedDateTime ts, boolean mapNull) toString()Methods inherited from class org.tentackle.dbms.StatementWrapper
assertNotReadOnly, assertOpen, cancel, consume, executeQuery, executeQuery, forceDetached, getAttachedSession, getConnection, getFetchDirection, getFetchSize, getMaxRows, getSession, getSql, getStackTrace, isCancelled, isClosed, isMarkedReady, isParallelOk, isRunning, setFetchDirection, setFetchSize, setMaxRows, setParallelOk, setRunning, setStackTrace, unmarkReady
-
Constructor Details
-
PreparedStatementWrapper
public PreparedStatementWrapper(ManagedConnection con, PreparedStatement stmt, StatementKey statementKey, String sql) Creates a wrapper for a prepared statement.- Parameters:
con- the connectionstmt- the jdbc statementstatementKey- the statement key if not a one-shotsql- the original sql string that created the stmt
-
-
Method Details
-
toString
- Overrides:
toStringin classStatementWrapper
-
close
public void close()Description copied from class:StatementWrapperCloses this statement.- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classStatementWrapper
-
getStatement
Gets the wrapped prepared statement.- Overrides:
getStatementin classStatementWrapper- Returns:
- the prepared statement, null if closed
-
getStatementKey
Gets the statement key.- Returns:
- the key, null if one-shot
-
setColumnOffset
public void setColumnOffset(int columnOffset) Sets the column offset. Useful for eager loading or joining in general.- Parameters:
columnOffset- (default is 0)
-
getColumnOffset
public int getColumnOffset()Gets the column offset.- Returns:
- the current columnOffset
-
rememberParameter
Sets the parameter to be remembered for diagnostics.- Parameters:
p- the effective sql positionvalue- the value of the parameter
-
detachSession
protected void detachSession()Description copied from class:StatementWrapperDetach the session from the connection.
Statements detach the session on executeUpdate or on close() in theResultSetWrapperafter executeQuery.- Overrides:
detachSessionin classStatementWrapper
-
markReady
public void markReady()Description copied from class:StatementWrapperMarks the statement to be ready for being consumed by aDbattached to aConnectionManager.
This is an additional measure to enforce the programming rule that a statement is being used only once afterDb.createStatement()(for non-prepared statements) or afterDb.getPreparedStatement(org.tentackle.dbms.StatementKey, boolean, int, int, org.tentackle.dbms.SqlSupplier)for prepared statements.If a statement is marked ready more than once, i.e. an open result exists (which would be closed according to the JDBC specs), a PersistenceException is thrown. The specs in
Statementsay:By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statement's current ResultSet object if an open one exists.
Without this additional measure a "ResultSet closed" exception will be thrown by the JDBC-driver on the next usage of the first result set, and you wouldn't have any clue which result set forced the closing.- Overrides:
markReadyin classStatementWrapper
-
setBatch
Sets the statement batch if batched within the current transaction.- Parameters:
batch- the batch
-
getBatch
Gets the statement batch.- Returns:
- null if not batched
-
isBatched
protected boolean isBatched()Returns whether statement is batched.- Returns:
- true if used in a batched transaction
-
clearParameters
public void clearParameters()Clears the current parameter values immediately.- See Also:
-
getParameters
Gets the parameter map.- Returns:
- the parameters
-
getBatchParameters
Gets the parameter map for a batched statement.- Returns:
- the parameter batch list, null if not batched
-
executeUpdateImpl
Implementation of executeUpdate.
Overridden because sql-string isn't used.
- Overrides:
executeUpdateImplin classStatementWrapper- Parameters:
sql- the sql string- Returns:
- the number of affected rows
- Throws:
SQLException- if update failed
-
executeUpdate
public int executeUpdate()Executes the update.- Returns:
- the row count
-
executeUpdate
Method must not be invoked on aPreparedStatementWrapper.- Overrides:
executeUpdatein classStatementWrapper- Parameters:
sql- must be null, any non-null results in aPersistenceException- Returns:
- the row count
-
addBatch
- Overrides:
addBatchin classStatementWrapper- Parameters:
sql- ignored
-
addBatch
public void addBatch()Adds a set of parameters to thisPreparedStatement's batch of commands. -
clearBatch
public void clearBatch()Description copied from class:StatementWrapperEmpties thisStatementobject's current list of batched SQL commands.- Overrides:
clearBatchin classStatementWrapper
-
executeBatch
public int[] executeBatch(boolean finish) Description copied from class:StatementWrapperSubmits a batch of commands.- Overrides:
executeBatchin classStatementWrapper- Parameters:
finish- true if this is the last invocation- Returns:
- an array of update counts containing one element for each command in the batch
-
isExecutionPending
public boolean isExecutionPending()Returns whether parameters have been set so that execution is pending.- Returns:
- true if pending, false otherwise
-
getBatchCount
public int getBatchCount()Gets the number of pending addBatch invocations.- Returns:
- 0 if nothing pending
-
executeQueryImpl
Implementation of executeQuery.
Overridden because sql-string isn't used.
- Overrides:
executeQueryImplin classStatementWrapper- Parameters:
sql- the sql string- Returns:
- the result set
- Throws:
SQLException- if query failed
-
executeQuery
Executes the query.- Parameters:
withinTx- is true if start a transaction for this query.- Returns:
- the result set as a ResultSetWrapper
-
executeQuery
Executes the query.- Returns:
- the result set as a ResultSetWrapper
-
setNull
public void setNull(int p, int type) - Specified by:
setNullin interfaceBackendPreparedStatement
-
set
- Specified by:
setin interfaceBackendPreparedStatement
-
set
public <T> void set(DataType<T> dataType, int p, T object, int index, boolean mapNull, Integer size) - Specified by:
setin interfaceBackendPreparedStatement
-
set
- Specified by:
setin interfaceBackendPreparedStatement
-
setArray
public void setArray(int p, Class<?> type, int columnIndex, Collection<?> elements, String operator) - Specified by:
setArrayin interfaceBackendPreparedStatement
-
setString
- Specified by:
setStringin interfaceBackendPreparedStatement
-
setString
- Specified by:
setStringin interfaceBackendPreparedStatement
-
setLargeString
- Specified by:
setLargeStringin interfaceBackendPreparedStatement
-
setLargeString
- Specified by:
setLargeStringin interfaceBackendPreparedStatement
-
setBoolean
public void setBoolean(int p, boolean b) - Specified by:
setBooleanin interfaceBackendPreparedStatement
-
setBoolean
- Specified by:
setBooleanin interfaceBackendPreparedStatement
-
setByte
public void setByte(int p, byte b) - Specified by:
setBytein interfaceBackendPreparedStatement
-
setByte
- Specified by:
setBytein interfaceBackendPreparedStatement
-
setChar
public void setChar(int p, char c) - Specified by:
setCharin interfaceBackendPreparedStatement
-
setCharacter
- Specified by:
setCharacterin interfaceBackendPreparedStatement
-
setCharacter
- Specified by:
setCharacterin interfaceBackendPreparedStatement
-
setShort
public void setShort(int p, short s) - Specified by:
setShortin interfaceBackendPreparedStatement
-
setShort
- Specified by:
setShortin interfaceBackendPreparedStatement
-
setInt
public void setInt(int p, int i) - Specified by:
setIntin interfaceBackendPreparedStatement
-
setInteger
- Specified by:
setIntegerin interfaceBackendPreparedStatement
-
setLocalDate
- Specified by:
setLocalDatein interfaceBackendPreparedStatement
-
setLocalDate
- Specified by:
setLocalDatein interfaceBackendPreparedStatement
-
setLocalDateTime
- Specified by:
setLocalDateTimein interfaceBackendPreparedStatement
-
setLocalDateTime
- Specified by:
setLocalDateTimein interfaceBackendPreparedStatement
-
setOffsetDateTime
- Specified by:
setOffsetDateTimein interfaceBackendPreparedStatement
-
setOffsetDateTime
- Specified by:
setOffsetDateTimein interfaceBackendPreparedStatement
-
setZonedDateTime
- Specified by:
setZonedDateTimein interfaceBackendPreparedStatement
-
setZonedDateTime
- Specified by:
setZonedDateTimein interfaceBackendPreparedStatement
-
setInstant
- Specified by:
setInstantin interfaceBackendPreparedStatement
-
setInstant
- Specified by:
setInstantin interfaceBackendPreparedStatement
-
setLocalTime
- Specified by:
setLocalTimein interfaceBackendPreparedStatement
-
setOffsetTime
- Specified by:
setOffsetTimein interfaceBackendPreparedStatement
-
setLong
public void setLong(int p, long l) - Specified by:
setLongin interfaceBackendPreparedStatement
-
setLong
- Specified by:
setLongin interfaceBackendPreparedStatement
-
setFloat
public void setFloat(int p, float f) - Specified by:
setFloatin interfaceBackendPreparedStatement
-
setFloat
- Specified by:
setFloatin interfaceBackendPreparedStatement
-
setDouble
public void setDouble(int p, double d) - Specified by:
setDoublein interfaceBackendPreparedStatement
-
setDouble
- Specified by:
setDoublein interfaceBackendPreparedStatement
-
setBigDecimal
- Specified by:
setBigDecimalin interfaceBackendPreparedStatement
-
setBMoney
public void setBMoney(int p, org.tentackle.common.BMoney m) Sets the designated parameter to aBMoneyvalue.
A BMoney will not be stored as a single field but as two fields:- a double representing the value
- an int representing the scale
- Parameters:
p- the sql positionm- the money value, null to set SQL NULL- See Also:
-
setDMoney
public void setDMoney(int p, org.tentackle.common.DMoney m) Sets the designated parameter to aDMoneyvalue.
A DMoney will not be stored as a single field but as two fields:- a BigDecimal with a scale of 0 representing the value
- an int representing the scale
- Parameters:
p- the sql positionm- the money value, null to set SQL NULL- See Also:
-
setDate
- Specified by:
setDatein interfaceBackendPreparedStatement
-
setDate
- Specified by:
setDatein interfaceBackendPreparedStatement
-
setDate
- Specified by:
setDatein interfaceBackendPreparedStatement
-
setDate
- Specified by:
setDatein interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestampin interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestampin interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestampin interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestampin interfaceBackendPreparedStatement
-
setTime
- Specified by:
setTimein interfaceBackendPreparedStatement
-
setTime
- Specified by:
setTimein interfaceBackendPreparedStatement
-
setBinary
public void setBinary(int p, org.tentackle.common.Binary<?> b) Sets the designated parameter to the givenBinaryvalue. will The driver converts this to an SQLBLOBvalue when it sends it to the database. The implementation translates the Binary into an InputStream and invokesPreparedStatement.setBlob(int, java.io.InputStream, long).- Parameters:
p- the first parameter is 1, the second is 2, ...b- the parameter value, null if the value should be set to SQL NULL
-