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, stmt
-
Constructor Summary
ConstructorDescriptionPreparedStatementWrapper
(ManagedConnection con, PreparedStatement stmt, StatementKey statementKey, String sql) Creates a wrapper for a prepared statement. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBatch()
Adds a set of parameters to thisPreparedStatement
's batch of commands.void
void
Empties thisStatement
object's current list of batched SQL commands.void
Clears the current parameter values immediately.void
close()
Closes this statement.protected void
Detach the session from the connection.
Statements detach the session on executeUpdate or on close() in theResultSetWrapper
after executeQuery.int[]
executeBatch
(boolean finish) Submits a batch of commands.Executes the query.executeQuery
(boolean withinTx) Executes the query.protected ResultSet
executeQueryImpl
(String sql) Implementation of executeQuery.int
Executes the update.int
executeUpdate
(String sql) Method must not be invoked on aPreparedStatementWrapper
.protected int
executeUpdateImpl
(String sql) Implementation of executeUpdate.protected DbBatch
getBatch()
Gets the statement batch.int
Gets the number of pending addBatch invocations.Gets the parameter map for a batched statement.int
Gets the column offset.Gets the parameter map.Gets the wrapped prepared statement.Gets the statement key.protected boolean
Returns whether statement is batched.boolean
Returns whether parameters have been set so that execution is pending.void
Marks the statement to be ready for being consumed by aDb
attached 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 void
rememberParameter
(int p, Object value) Sets the parameter to be remembered for diagnostics.<T> int
<T> void
void
void
setArray
(int p, Class<?> type, int columnIndex, Collection<?> elements, String operator) protected void
Sets the statement batch if batched within the current transaction.void
setBigDecimal
(int p, BigDecimal d) void
setBinary
(int p, org.tentackle.common.Binary<?> b) Sets the designated parameter to the givenBinary
value.void
setBMoney
(int p, org.tentackle.common.BMoney m) Sets the designated parameter to aBMoney
value.
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.void
setBoolean
(int p, boolean b) void
setBoolean
(int p, Boolean b) void
setByte
(int p, byte b) void
void
setChar
(int p, char c) void
setCharacter
(int p, Character c) void
setCharacter
(int p, Character c, boolean mapNull) void
setColumnOffset
(int columnOffset) Sets the column offset.void
void
void
void
void
setDMoney
(int p, org.tentackle.common.DMoney m) Sets the designated parameter to aDMoney
value.
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.void
setDouble
(int p, double d) void
void
setFloat
(int p, float f) void
void
setInstant
(int p, Instant ts) void
setInstant
(int p, Instant ts, boolean mapNull) void
setInt
(int p, int i) void
setInteger
(int p, Integer i) void
setLargeString
(int p, String s) void
setLargeString
(int p, String s, boolean mapNull) void
setLocalDate
(int p, LocalDate d) void
setLocalDate
(int p, LocalDate d, boolean mapNull) void
setLocalDateTime
(int p, LocalDateTime ts) void
setLocalDateTime
(int p, LocalDateTime ts, boolean mapNull) void
setLocalTime
(int p, LocalTime t) void
setLong
(int p, long l) void
void
setNull
(int p, int type) void
setOffsetDateTime
(int p, OffsetDateTime ts) void
setOffsetDateTime
(int p, OffsetDateTime ts, boolean mapNull) void
setOffsetTime
(int p, OffsetTime t) void
setShort
(int p, short s) void
void
void
void
void
void
setTimestamp
(int p, Timestamp ts) void
setTimestamp
(int p, Timestamp ts, boolean mapNull) void
setTimestamp
(int p, Timestamp ts, Calendar timezone) void
setTimestamp
(int p, Timestamp ts, Calendar timezone, boolean mapNull) void
setZonedDateTime
(int p, ZonedDateTime ts) void
setZonedDateTime
(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, isCancelled, isClosed, isMarkedReady, isParallelOk, isRunning, setFetchDirection, setFetchSize, setMaxRows, setParallelOk, setRunning, 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:
toString
in classStatementWrapper
-
close
public void close()Description copied from class:StatementWrapper
Closes this statement.- Specified by:
close
in interfaceAutoCloseable
- Overrides:
close
in classStatementWrapper
-
getStatement
Gets the wrapped prepared statement.- Overrides:
getStatement
in 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:StatementWrapper
Detach the session from the connection.
Statements detach the session on executeUpdate or on close() in theResultSetWrapper
after executeQuery.- Overrides:
detachSession
in classStatementWrapper
-
markReady
public void markReady()Description copied from class:StatementWrapper
Marks the statement to be ready for being consumed by aDb
attached 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
Statement
say: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:
markReady
in 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:
executeUpdateImpl
in 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:
executeUpdate
in classStatementWrapper
- Parameters:
sql
- must be null, any non-null results in aPersistenceException
- Returns:
- the row count
-
addBatch
- Overrides:
addBatch
in 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:StatementWrapper
Empties thisStatement
object's current list of batched SQL commands.- Overrides:
clearBatch
in classStatementWrapper
-
executeBatch
public int[] executeBatch(boolean finish) Description copied from class:StatementWrapper
Submits a batch of commands.- Overrides:
executeBatch
in 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:
executeQueryImpl
in 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:
setNull
in interfaceBackendPreparedStatement
-
set
- Specified by:
set
in interfaceBackendPreparedStatement
-
set
public <T> void set(DataType<T> dataType, int p, T object, int index, boolean mapNull, Integer size) - Specified by:
set
in interfaceBackendPreparedStatement
-
set
- Specified by:
set
in interfaceBackendPreparedStatement
-
setArray
public void setArray(int p, Class<?> type, int columnIndex, Collection<?> elements, String operator) - Specified by:
setArray
in interfaceBackendPreparedStatement
-
setString
- Specified by:
setString
in interfaceBackendPreparedStatement
-
setString
- Specified by:
setString
in interfaceBackendPreparedStatement
-
setLargeString
- Specified by:
setLargeString
in interfaceBackendPreparedStatement
-
setLargeString
- Specified by:
setLargeString
in interfaceBackendPreparedStatement
-
setBoolean
public void setBoolean(int p, boolean b) - Specified by:
setBoolean
in interfaceBackendPreparedStatement
-
setBoolean
- Specified by:
setBoolean
in interfaceBackendPreparedStatement
-
setByte
public void setByte(int p, byte b) - Specified by:
setByte
in interfaceBackendPreparedStatement
-
setByte
- Specified by:
setByte
in interfaceBackendPreparedStatement
-
setChar
public void setChar(int p, char c) - Specified by:
setChar
in interfaceBackendPreparedStatement
-
setCharacter
- Specified by:
setCharacter
in interfaceBackendPreparedStatement
-
setCharacter
- Specified by:
setCharacter
in interfaceBackendPreparedStatement
-
setShort
public void setShort(int p, short s) - Specified by:
setShort
in interfaceBackendPreparedStatement
-
setShort
- Specified by:
setShort
in interfaceBackendPreparedStatement
-
setInt
public void setInt(int p, int i) - Specified by:
setInt
in interfaceBackendPreparedStatement
-
setInteger
- Specified by:
setInteger
in interfaceBackendPreparedStatement
-
setLocalDate
- Specified by:
setLocalDate
in interfaceBackendPreparedStatement
-
setLocalDate
- Specified by:
setLocalDate
in interfaceBackendPreparedStatement
-
setLocalDateTime
- Specified by:
setLocalDateTime
in interfaceBackendPreparedStatement
-
setLocalDateTime
- Specified by:
setLocalDateTime
in interfaceBackendPreparedStatement
-
setOffsetDateTime
- Specified by:
setOffsetDateTime
in interfaceBackendPreparedStatement
-
setOffsetDateTime
- Specified by:
setOffsetDateTime
in interfaceBackendPreparedStatement
-
setZonedDateTime
- Specified by:
setZonedDateTime
in interfaceBackendPreparedStatement
-
setZonedDateTime
- Specified by:
setZonedDateTime
in interfaceBackendPreparedStatement
-
setInstant
- Specified by:
setInstant
in interfaceBackendPreparedStatement
-
setInstant
- Specified by:
setInstant
in interfaceBackendPreparedStatement
-
setLocalTime
- Specified by:
setLocalTime
in interfaceBackendPreparedStatement
-
setOffsetTime
- Specified by:
setOffsetTime
in interfaceBackendPreparedStatement
-
setLong
public void setLong(int p, long l) - Specified by:
setLong
in interfaceBackendPreparedStatement
-
setLong
- Specified by:
setLong
in interfaceBackendPreparedStatement
-
setFloat
public void setFloat(int p, float f) - Specified by:
setFloat
in interfaceBackendPreparedStatement
-
setFloat
- Specified by:
setFloat
in interfaceBackendPreparedStatement
-
setDouble
public void setDouble(int p, double d) - Specified by:
setDouble
in interfaceBackendPreparedStatement
-
setDouble
- Specified by:
setDouble
in interfaceBackendPreparedStatement
-
setBigDecimal
- Specified by:
setBigDecimal
in interfaceBackendPreparedStatement
-
setBMoney
public void setBMoney(int p, org.tentackle.common.BMoney m) Sets the designated parameter to aBMoney
value.
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 aDMoney
value.
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:
setDate
in interfaceBackendPreparedStatement
-
setDate
- Specified by:
setDate
in interfaceBackendPreparedStatement
-
setDate
- Specified by:
setDate
in interfaceBackendPreparedStatement
-
setDate
- Specified by:
setDate
in interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestamp
in interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestamp
in interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestamp
in interfaceBackendPreparedStatement
-
setTimestamp
- Specified by:
setTimestamp
in interfaceBackendPreparedStatement
-
setTime
- Specified by:
setTime
in interfaceBackendPreparedStatement
-
setTime
- Specified by:
setTime
in interfaceBackendPreparedStatement
-
setBinary
public void setBinary(int p, org.tentackle.common.Binary<?> b) Sets the designated parameter to the givenBinary
value. will The driver converts this to an SQLBLOB
value 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
-