Class PgStatement
- All Implemented Interfaces:
AutoCloseable
,Statement
,Wrapper
,BaseStatement
,PGStatement
- Direct Known Subclasses:
PgPreparedStatement
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
ResultHandler implementations for updates, queries, and either-or. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
protected ArrayList
<ParameterList> private static final AtomicReferenceFieldUpdater
<PgStatement, TimerTask> private TimerTask
Protects current statement from cancelTask starting, waiting for a bit, and waking up exactly on subsequent query execution.private boolean
protected final int
protected final PgConnection
private static final boolean
Default state for use or not binary transfers.protected int
protected int
Number of rows to get in a batch.protected ResultWrapper
The first unclosed result.private boolean
protected ResultWrapper
Results returned by a statement that wants generated keys.private static final AtomicIntegerFieldUpdater
<PgStatement> private int
protected final ResourceLock
protected int
protected int
Maximum number of rows to return, 0 = unlimited.protected int
private static final String[]
private boolean
protected boolean
protected ResultWrapper
The current results.protected final int
private final int
private static final AtomicReferenceFieldUpdater
<PgStatement, StatementCancelState> private StatementCancelState
Protects statement from out-of-order cancels.protected long
Timeout (in milliseconds) for a query.private TimestampUtils
boolean
Was this PreparedStatement created to return generated keys for every execution? This is set at creation time and never cleared by execution.protected boolean
Does the caller of execute/executeUpdate want generated keys for this execution? This is set by Statement methods that have generated keys arguments and cleared after execution is complete.protected PSQLWarningWrapper
The warnings chain.Fields inherited from interface org.postgresql.PGStatement
DATE_NEGATIVE_INFINITY, DATE_NEGATIVE_SMALLER_INFINITY, DATE_POSITIVE_INFINITY, DATE_POSITIVE_SMALLER_INFINITY
Fields inherited from interface java.sql.Statement
CLOSE_ALL_RESULTS, CLOSE_CURRENT_RESULT, EXECUTE_FAILED, KEEP_CURRENT_RESULT, NO_GENERATED_KEYS, RETURN_GENERATED_KEYS, SUCCESS_NO_INFO
-
Constructor Summary
ConstructorsConstructorDescriptionPgStatement
(PgConnection c, int rsType, int rsConcurrency, int rsHoldability) -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
addWarning
(SQLWarning warn) Either initializes new warning wrapper, or adds warning onto the chain.void
cancel()
(package private) void
cancelIfStillNeeded
(TimerTask timerTask) protected void
protected void
protected final void
private boolean
ClearscancelTimerTask
if any.void
void
Clears the warning chain.final void
close()
Note: even thoughStatement
is automatically closed when it is garbage collected, it is better to close it explicitly to lower resource consumption.protected void
protected void
This is guaranteed to be called exactly once even in case of concurrentclose()
calls.void
private void
protected BatchResultHandler
createBatchHandler
(Query[] queries, ParameterList[] parameterLists) createDriverResultSet
(Field[] fields, List<Tuple> tuples) Create a synthetic resultset from data provided by the driver.createResultSet
(Query originalQuery, Field[] fields, List<Tuple> tuples, ResultCursor cursor) Create a resultset from data retrieved from the server.boolean
boolean
boolean
boolean
protected final void
execute
(CachedQuery cachedQuery, ParameterList queryParameters, int flags) int[]
private boolean
executeCachedSql
(String sql, int flags, String[] columnNames) private void
executeInternal
(CachedQuery cachedQuery, ParameterList queryParameters, int flags) long[]
long
executeLargeUpdate
(String sql) long
executeLargeUpdate
(String sql, int autoGeneratedKeys) long
executeLargeUpdate
(String sql, int[] columnIndexes) long
executeLargeUpdate
(String sql, String[] columnNames) executeQuery
(String sql) int
executeUpdate
(String sql) int
executeUpdate
(String sql, int autoGeneratedKeys) int
executeUpdate
(String sql, int[] columnIndexes) int
executeUpdate
(String sql, String[] columnNames) boolean
executeWithFlags
(int flags) Execute a prepared query, passing additional query flags.boolean
executeWithFlags
(String sql, int flags) Execute a query, passing additional query flags.boolean
executeWithFlags
(CachedQuery simpleQuery, int flags) Execute a query, passing additional query flags.boolean
Get state of adaptive fetch for statement.int
int
protected boolean
long
long
long
Returns the Last inserted/updated oid.int
int
boolean
boolean
getMoreResults
(int current) int
Gets the server-side prepare reuse threshold in use for this statement.int
long
The queryTimeout limit is the number of milliseconds the driver will wait for a Statement to execute.int
int
int
protected ResultSet
protected TimestampUtils
int
private BatchResultHandler
boolean
isClosed()
boolean
protected boolean
isOneShotQuery
(CachedQuery cachedQuery) Returns true if query is unlikely to be reused.boolean
boolean
Checks if this statement will be executed as a server-prepared statement.boolean
isWrapperFor
(Class<?> iface) private void
void
setAdaptiveFetch
(boolean adaptiveFetch) Turn on/off adaptive fetch for statement.void
setCursorName
(String name) void
setEscapeProcessing
(boolean enable) void
setFetchDirection
(int direction) void
setFetchSize
(int rows) void
setLargeMaxRows
(long max) void
setMaxFieldSize
(int max) void
setMaxRows
(int max) void
setPoolable
(boolean poolable) void
setPrepareThreshold
(int newThreshold) Sets the reuse threshold for using server-prepared statements.void
setQueryTimeout
(int seconds) void
setQueryTimeoutMs
(long millis) Sets the queryTimeout limit.void
setUseServerPrepare
(boolean flag) Turn on the use of prepared statements in the server (server side prepared statements are unrelated to jdbc PreparedStatements) As of build 302, this method is equivalent tosetPrepareThreshold(1)
.private void
protected void
<T> T
protected boolean
protected boolean
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.sql.Statement
enquoteIdentifier, enquoteLiteral, enquoteNCharLiteral, isSimpleIdentifier
-
Field Details
-
NO_RETURNING_COLUMNS
-
DEFAULT_FORCE_BINARY_TRANSFERS
private static final boolean DEFAULT_FORCE_BINARY_TRANSFERSDefault state for use or not binary transfers. Can use only for testing purposes -
forceBinaryTransfers
private boolean forceBinaryTransfers -
lock
-
batchStatements
-
batchParameters
-
resultsettype
protected final int resultsettype -
concurrency
protected final int concurrency -
rsHoldability
private final int rsHoldability -
poolable
private boolean poolable -
closeOnCompletion
private boolean closeOnCompletion -
fetchdirection
protected int fetchdirection -
cancelTimerTask
Protects current statement from cancelTask starting, waiting for a bit, and waking up exactly on subsequent query execution. The idea is to atomically compare and swap the reference to the task, so the task can detect that statement executes different query than the one the cancelTask was created. Note: the field must be set/get/compareAndSet viaCANCEL_TIMER_UPDATER
as perAtomicReferenceFieldUpdater
javadoc. -
CANCEL_TIMER_UPDATER
-
statementState
Protects statement from out-of-order cancels. It protects from bothsetQueryTimeout(int)
andcancel()
induced ones.execute(String)
and friends change the field toStatementCancelState.IN_QUERY
during execute.cancel()
ignores cancel request if state isStatementCancelState.IDLE
. In caseexecute(String)
observes non-StatementCancelState.IDLE
state as it completes the query, it waits tillStatementCancelState.CANCELLED
. Note: the field must be set/get/compareAndSet viaSTATE_UPDATER
as perAtomicIntegerFieldUpdater
javadoc. -
STATE_UPDATER
-
wantsGeneratedKeysOnce
protected boolean wantsGeneratedKeysOnceDoes the caller of execute/executeUpdate want generated keys for this execution? This is set by Statement methods that have generated keys arguments and cleared after execution is complete. -
wantsGeneratedKeysAlways
public boolean wantsGeneratedKeysAlwaysWas this PreparedStatement created to return generated keys for every execution? This is set at creation time and never cleared by execution. -
connection
-
warnings
The warnings chain. -
maxrows
protected int maxrowsMaximum number of rows to return, 0 = unlimited. -
fetchSize
protected int fetchSizeNumber of rows to get in a batch. -
timeout
protected long timeoutTimeout (in milliseconds) for a query. -
replaceProcessingEnabled
protected boolean replaceProcessingEnabled -
result
The current results. -
firstUnclosedResult
The first unclosed result. -
generatedKeys
Results returned by a statement that wants generated keys. -
mPrepareThreshold
protected int mPrepareThreshold -
maxFieldSize
protected int maxFieldSize -
adaptiveFetch
protected boolean adaptiveFetch -
timestampUtils
-
isClosed
private volatile int isClosed -
IS_CLOSED_UPDATER
-
-
Constructor Details
-
PgStatement
PgStatement(PgConnection c, int rsType, int rsConcurrency, int rsHoldability) throws SQLException - Throws:
SQLException
-
-
Method Details
-
createResultSet
public ResultSet createResultSet(Query originalQuery, Field[] fields, List<Tuple> tuples, ResultCursor cursor) throws SQLException Description copied from interface:BaseStatement
Create a resultset from data retrieved from the server.- Specified by:
createResultSet
in interfaceBaseStatement
- Parameters:
originalQuery
- the query that generated this resultset; used when dealing with updateable resultsetsfields
- the column metadata for the resultsettuples
- the resultset datacursor
- the cursor to use to retrieve more data from the server; if null, no additional data is present.- Returns:
- the new ResultSet
- Throws:
SQLException
- if something goes wrong
-
getPGConnection
-
getFetchingCursorName
-
getFetchSize
public int getFetchSize()- Specified by:
getFetchSize
in interfaceStatement
-
wantsScrollableResultSet
protected boolean wantsScrollableResultSet() -
wantsHoldableResultSet
protected boolean wantsHoldableResultSet() -
executeQuery
- Specified by:
executeQuery
in interfaceStatement
- Throws:
SQLException
-
getSingleResultSet
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
checkNoResultUpdate
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
executeWithFlags
Description copied from interface:BaseStatement
Execute a query, passing additional query flags.- Specified by:
executeWithFlags
in interfaceBaseStatement
- Parameters:
sql
- the query to execute (JDBC-style query)flags
- additionalQueryExecutor
flags for execution; these are bitwise-ORed into the default flags.- Returns:
- true if there is a result set
- Throws:
SQLException
- if something goes wrong.
-
executeCachedSql
- Throws:
SQLException
-
executeWithFlags
Description copied from interface:BaseStatement
Execute a query, passing additional query flags.- Specified by:
executeWithFlags
in interfaceBaseStatement
- Parameters:
simpleQuery
- the query to execute (native to PostgreSQL)flags
- additionalQueryExecutor
flags for execution; these are bitwise-ORed into the default flags.- Returns:
- true if there is a result set
- Throws:
SQLException
- if something goes wrong.
-
executeWithFlags
Description copied from interface:BaseStatement
Execute a prepared query, passing additional query flags.- Specified by:
executeWithFlags
in interfaceBaseStatement
- Parameters:
flags
- additionalQueryExecutor
flags for execution; these are bitwise-ORed into the default flags.- Returns:
- true if there is a result set
- Throws:
SQLException
- if something goes wrong.
-
closeUnclosedProcessedResults
- Throws:
SQLException
-
closeForNextExecution
- Throws:
SQLException
-
isOneShotQuery
Returns true if query is unlikely to be reused.- Parameters:
cachedQuery
- to check (null if current query)- Returns:
- true if query is unlikely to be reused
-
execute
protected final void execute(CachedQuery cachedQuery, ParameterList queryParameters, int flags) throws SQLException - Throws:
SQLException
-
executeInternal
private void executeInternal(CachedQuery cachedQuery, ParameterList queryParameters, int flags) throws SQLException - Throws:
SQLException
-
setCursorName
- Specified by:
setCursorName
in interfaceStatement
- Throws:
SQLException
-
getUpdateCount
- Specified by:
getUpdateCount
in interfaceStatement
- Throws:
SQLException
-
getMoreResults
- Specified by:
getMoreResults
in interfaceStatement
- Throws:
SQLException
-
getMaxRows
- Specified by:
getMaxRows
in interfaceStatement
- Throws:
SQLException
-
setMaxRows
- Specified by:
setMaxRows
in interfaceStatement
- Throws:
SQLException
-
setEscapeProcessing
- Specified by:
setEscapeProcessing
in interfaceStatement
- Throws:
SQLException
-
getQueryTimeout
- Specified by:
getQueryTimeout
in interfaceStatement
- Throws:
SQLException
-
setQueryTimeout
- Specified by:
setQueryTimeout
in interfaceStatement
- Throws:
SQLException
-
getQueryTimeoutMs
The queryTimeout limit is the number of milliseconds the driver will wait for a Statement to execute. If the limit is exceeded, a SQLException is thrown.- Returns:
- the current query timeout limit in milliseconds; 0 = unlimited
- Throws:
SQLException
- if a database access error occurs
-
setQueryTimeoutMs
Sets the queryTimeout limit.- Parameters:
millis
- - the new query timeout limit in milliseconds- Throws:
SQLException
- if a database access error occurs
-
addWarning
Either initializes new warning wrapper, or adds warning onto the chain.Although warnings are expected to be added sequentially, the warnings chain may be cleared concurrently at any time via
clearWarnings()
, therefore it is possible that a warning added via this method is placed onto the end of the previous warning chain- Parameters:
warn
- warning to add
-
getWarnings
- Specified by:
getWarnings
in interfaceStatement
- Throws:
SQLException
-
getMaxFieldSize
- Specified by:
getMaxFieldSize
in interfaceStatement
- Throws:
SQLException
-
setMaxFieldSize
- Specified by:
setMaxFieldSize
in interfaceStatement
- Throws:
SQLException
-
clearWarnings
Clears the warning chain.Note that while it is safe to clear warnings while the query is executing, warnings that are added between calls to
getWarnings()
and #clearWarnings() may be missed. Therefore you should hold a reference to the tail of the previous warning chain and verify if itsSQLWarning.getNextWarning()
value is holds any new value.- Specified by:
clearWarnings
in interfaceStatement
- Throws:
SQLException
-
getResultSet
- Specified by:
getResultSet
in interfaceStatement
- Throws:
SQLException
-
close
Note: even thoughStatement
is automatically closed when it is garbage collected, it is better to close it explicitly to lower resource consumption.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceStatement
- Throws:
SQLException
-
closeImpl
This is guaranteed to be called exactly once even in case of concurrentclose()
calls.- Throws:
SQLException
- in case of error
-
getLastOID
Description copied from interface:PGStatement
Returns the Last inserted/updated oid.- Specified by:
getLastOID
in interfacePGStatement
- Returns:
- OID of last insert
- Throws:
SQLException
- if something goes wrong
-
setPrepareThreshold
Description copied from interface:PGStatement
Sets the reuse threshold for using server-prepared statements.If
threshold
is a non-zero value N, the Nth and subsequent reuses of a PreparedStatement will use server-side prepare.If
threshold
is zero, server-side prepare will not be used.The reuse threshold is only used by PreparedStatement and CallableStatement objects; it is ignored for plain Statements.
- Specified by:
setPrepareThreshold
in interfacePGStatement
- Parameters:
newThreshold
- the new threshold for this statement- Throws:
SQLException
- if an exception occurs while changing the threshold
-
getPrepareThreshold
public int getPrepareThreshold()Description copied from interface:PGStatement
Gets the server-side prepare reuse threshold in use for this statement.- Specified by:
getPrepareThreshold
in interfacePGStatement
- Returns:
- the current threshold
- See Also:
-
setUseServerPrepare
Description copied from interface:PGStatement
Turn on the use of prepared statements in the server (server side prepared statements are unrelated to jdbc PreparedStatements) As of build 302, this method is equivalent tosetPrepareThreshold(1)
.- Specified by:
setUseServerPrepare
in interfacePGStatement
- Parameters:
flag
- use server prepare- Throws:
SQLException
- if something goes wrong
-
isUseServerPrepare
public boolean isUseServerPrepare()Description copied from interface:PGStatement
Checks if this statement will be executed as a server-prepared statement. A return value oftrue
indicates that the next execution of the statement will be done as a server-prepared statement, assuming the underlying protocol supports it.- Specified by:
isUseServerPrepare
in interfacePGStatement
- Returns:
- true if the next reuse of this statement will use a server-prepared statement
-
checkClosed
- Throws:
SQLException
-
addBatch
- Specified by:
addBatch
in interfaceStatement
- Throws:
SQLException
-
clearBatch
- Specified by:
clearBatch
in interfaceStatement
- Throws:
SQLException
-
createBatchHandler
-
internalExecuteBatch
- Throws:
SQLException
-
executeBatch
- Specified by:
executeBatch
in interfaceStatement
- Throws:
SQLException
-
cancel
- Specified by:
cancel
in interfaceStatement
- Throws:
SQLException
-
getConnection
- Specified by:
getConnection
in interfaceStatement
- Throws:
SQLException
-
getFetchDirection
public int getFetchDirection()- Specified by:
getFetchDirection
in interfaceStatement
-
getResultSetConcurrency
public int getResultSetConcurrency()- Specified by:
getResultSetConcurrency
in interfaceStatement
-
getResultSetType
public int getResultSetType()- Specified by:
getResultSetType
in interfaceStatement
-
setFetchDirection
- Specified by:
setFetchDirection
in interfaceStatement
- Throws:
SQLException
-
setFetchSize
- Specified by:
setFetchSize
in interfaceStatement
- Throws:
SQLException
-
startTimer
private void startTimer() -
cancelIfStillNeeded
-
cleanupTimer
private boolean cleanupTimer()ClearscancelTimerTask
if any. Returns true if and only if "cancel" timer task would never invokecancel()
. -
killTimerTask
private void killTimerTask() -
getForceBinaryTransfer
protected boolean getForceBinaryTransfer() -
getLargeUpdateCount
- Specified by:
getLargeUpdateCount
in interfaceStatement
- Throws:
SQLException
-
setLargeMaxRows
- Specified by:
setLargeMaxRows
in interfaceStatement
- Throws:
SQLException
-
getLargeMaxRows
- Specified by:
getLargeMaxRows
in interfaceStatement
- Throws:
SQLException
-
executeLargeBatch
- Specified by:
executeLargeBatch
in interfaceStatement
- Throws:
SQLException
-
executeLargeUpdate
- Specified by:
executeLargeUpdate
in interfaceStatement
- Throws:
SQLException
-
executeLargeUpdate
- Specified by:
executeLargeUpdate
in interfaceStatement
- Throws:
SQLException
-
executeLargeUpdate
- Specified by:
executeLargeUpdate
in interfaceStatement
- Throws:
SQLException
-
executeLargeUpdate
- Specified by:
executeLargeUpdate
in interfaceStatement
- Throws:
SQLException
-
isClosed
- Specified by:
isClosed
in interfaceStatement
- Throws:
SQLException
-
setPoolable
- Specified by:
setPoolable
in interfaceStatement
- Throws:
SQLException
-
isPoolable
- Specified by:
isPoolable
in interfaceStatement
- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperFor
in interfaceWrapper
- Throws:
SQLException
-
unwrap
- Specified by:
unwrap
in interfaceWrapper
- Throws:
SQLException
-
closeOnCompletion
- Specified by:
closeOnCompletion
in interfaceStatement
- Throws:
SQLException
-
isCloseOnCompletion
- Specified by:
isCloseOnCompletion
in interfaceStatement
- Throws:
SQLException
-
checkCompletion
- Throws:
SQLException
-
getMoreResults
- Specified by:
getMoreResults
in interfaceStatement
- Throws:
SQLException
-
getGeneratedKeys
- Specified by:
getGeneratedKeys
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
executeUpdate
- Specified by:
executeUpdate
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
execute
- Specified by:
execute
in interfaceStatement
- Throws:
SQLException
-
getResultSetHoldability
- Specified by:
getResultSetHoldability
in interfaceStatement
- Throws:
SQLException
-
createDriverResultSet
Description copied from interface:BaseStatement
Create a synthetic resultset from data provided by the driver.- Specified by:
createDriverResultSet
in interfaceBaseStatement
- Parameters:
fields
- the column metadata for the resultsettuples
- the resultset data- Returns:
- the new ResultSet
- Throws:
SQLException
- if something goes wrong
-
transformQueriesAndParameters
- Throws:
SQLException
-
setAdaptiveFetch
public void setAdaptiveFetch(boolean adaptiveFetch) Description copied from interface:PGStatement
Turn on/off adaptive fetch for statement. Existing resultSets won't be affected by change here.- Specified by:
setAdaptiveFetch
in interfacePGStatement
- Parameters:
adaptiveFetch
- desired state of adaptive fetch.
-
getAdaptiveFetch
public boolean getAdaptiveFetch()Description copied from interface:PGStatement
Get state of adaptive fetch for statement.- Specified by:
getAdaptiveFetch
in interfacePGStatement
- Returns:
- state of adaptive fetch (turned on or off)
-
getTimestampUtils
-