Class PreparedStatementWrapper
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(StatementKey, boolean, int, int, SqlSupplier).- Author:
- harald
-
Field Summary
Fields inherited from class StatementWrapper
canceled, 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 voiddetachSession(Db db) Detaches the given session from the connection.
Nothing happens if the session is no longer attached to the connection of this statement.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 the statement is batched.booleanReturns whether parameters have been set so that execution is pending.protected booleanReturns whether marking this statement ready again while not consumed yet is allowed.
Invoked byStatementWrapper.markReady()with the monitor held.protected booleanReturns whether this is a one-shot statement.
One-shot statements are created and closed by the application, whereas cached statements are closed by the connection.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 a short representing the scale This is because 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) voidvoidsetBytes(int p, byte[] b) Sets the designated parameter to the givenbyte[]value.voidsetChar(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 a short representing the scale This is because 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) voidvoidvoidsetLargeString(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) voidvoidvoidvoidvoidvoidsetTBinary(int p, org.tentackle.common.TBinary<?> b) Sets the designated parameter to the givenTBinaryvalue.voidvoidvoidsetTimestamp(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 StatementWrapper
assertNotReadOnly, assertOpen, cancel, consume, createResultSetWrapper, detachSession, executeQuery, executeQuery, forceDetached, forgetResultSetWrapper, getAttachedSession, getConnection, getExecutingSession, getFetchDirection, getFetchSize, getMaxRows, getSession, getSql, getStackTrace, getUsingSession, isCanceled, isClosed, isMarkedReady, isParallelOk, isRunning, markReady, 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
-
isOneShot
protected boolean isOneShot()Description copied from class:StatementWrapperReturns whether this is a one-shot statement.
One-shot statements are created and closed by the application, whereas cached statements are closed by the connection.- Overrides:
isOneShotin classStatementWrapper- Returns:
- true if one-shot (default for non-prepared statements)
-
setColumnOffset
public void setColumnOffset(int columnOffset) Sets the column offset. Useful for eager loading or joining in general.- Parameters:
columnOffset- the offset added to the column index (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
Description copied from class:StatementWrapperDetaches the given session from the connection.
Nothing happens if the session is no longer attached to the connection of this statement. This is the case if the session was force-detached while the statement was still in use, and the connection may have been attached to another session in the meantime, which must not be detached by this statement.- Overrides:
detachSessionin classStatementWrapper- Parameters:
db- the session that used this statement, null if already cleaned up
-
isMarkReadyAgainAllowed
protected boolean isMarkReadyAgainAllowed()Description copied from class:StatementWrapperReturns whether marking this statement ready again while not consumed yet is allowed.
Invoked byStatementWrapper.markReady()with the monitor held.- Overrides:
isMarkReadyAgainAllowedin classStatementWrapper- Returns:
- true if allowed, false if a programming error (default)
-
setBatch
Sets the statement batch if batched within the current transaction.- Parameters:
batch- the batch
-
getBatch
-
isBatched
protected boolean isBatched()Returns whether the statement is batched.- Returns:
- true if used in a batched transaction
-
clearParameters
public void clearParameters()Clears the current parameter values immediately.- See Also:
-
getParameters
-
getBatchParameters
-
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 the 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 the query failed
-
executeQuery
Executes the query.- Parameters:
withinTx- true to run the query within a transaction, committed when the result set is closed- 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
-
setSqlxml
- Specified by:
setSqlxmlin interfaceBackendPreparedStatement
-
setSqlxml
- Specified by:
setSqlxmlin interfaceBackendPreparedStatement
-
setJson
- Specified by:
setJsonin interfaceBackendPreparedStatement
-
setJson
- Specified by:
setJsonin 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
- a short 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
- a short 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. The driver converts it to aBLOBvalue when it sends it to the database. The implementation translates the binary into an InputStream and invokesPreparedStatement.setBinaryStream(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 (an empty binary is stored as is)
-
setBytes
public void setBytes(int p, byte[] b) Sets the designated parameter to the givenbyte[]value. The bytes are stored "as is", i.e. an empty array is not treated as null.- 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
-
setTBinary
public void setTBinary(int p, org.tentackle.common.TBinary<?> b) Sets the designated parameter to the givenTBinaryvalue. The driver converts it to aBLOBvalue when it sends it to the database. The implementation translates the binary into an InputStream and invokesPreparedStatement.setBinaryStream(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 (an empty binary is stored as is)
-