Class PgStatement
- java.lang.Object
-
- org.postgresql.jdbc.PgStatement
-
- All Implemented Interfaces:
AutoCloseable,Statement,Wrapper,BaseStatement,PGStatement
public class PgStatement extends Object implements Statement, BaseStatement
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classPgStatement.StatementResultHandlerResultHandler implementations for updates, queries, and either-or.
-
Field Summary
Fields Modifier and Type Field Description protected ArrayList<ParameterList>batchParametersprotected ArrayList<Query>batchStatementsprotected intconcurrencyprotected BaseConnectionconnectionprotected intfetchdirectionprotected intfetchSizeNumber of rows to get in a batch.protected ResultWrapperfirstUnclosedResultThe first unclosed result.protected ResultWrappergeneratedKeysResults returned by a statement that wants generated keys.protected intmaxFieldSizeprotected intmaxrowsMaximum number of rows to return, 0 = unlimited.protected intmPrepareThresholdprotected booleanreplaceProcessingEnabledprotected ResultWrapperresultThe current results.protected intresultsettypeprotected longtimeoutTimeout (in milliseconds) for a query.booleanwantsGeneratedKeysAlwaysWas this PreparedStatement created to return generated keys for every execution? This is set at creation time and never cleared by execution.protected booleanwantsGeneratedKeysOnceDoes 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 org.postgresql.jdbc.PSQLWarningWrapperwarningsThe 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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBatch(String sql)voidaddWarning(SQLWarning warn)Either initializes new warning wrapper, or adds warning onto the chain.voidcancel()protected voidcheckClosed()protected voidcheckCompletion()protected voidcheckNoResultUpdate()voidclearBatch()voidclearWarnings()Clears the warning chain.voidclose()Note: even thoughStatementis automatically closed when it is garbage collected, it is better to close it explicitly to lower resource consumption.protected voidcloseForNextExecution()protected voidcloseImpl()This is guaranteed to be called exactly once even in case of concurrentclose()calls.voidcloseOnCompletion()protected BatchResultHandlercreateBatchHandler(Query[] queries, ParameterList[] parameterLists)ResultSetcreateDriverResultSet(Field[] fields, List<Tuple> tuples)Create a synthetic resultset from data provided by the driver.ResultSetcreateResultSet(Query originalQuery, Field[] fields, List<Tuple> tuples, ResultCursor cursor)Create a resultset from data retrieved from the server.booleanexecute(String sql)booleanexecute(String sql, int autoGeneratedKeys)booleanexecute(String sql, int[] columnIndexes)booleanexecute(String sql, String[] columnNames)protected voidexecute(CachedQuery cachedQuery, ParameterList queryParameters, int flags)int[]executeBatch()long[]executeLargeBatch()longexecuteLargeUpdate(String sql)longexecuteLargeUpdate(String sql, int autoGeneratedKeys)longexecuteLargeUpdate(String sql, int[] columnIndexes)longexecuteLargeUpdate(String sql, String[] columnNames)ResultSetexecuteQuery(String sql)intexecuteUpdate(String sql)intexecuteUpdate(String sql, int autoGeneratedKeys)intexecuteUpdate(String sql, int[] columnIndexes)intexecuteUpdate(String sql, String[] columnNames)booleanexecuteWithFlags(int flags)Execute a prepared query, passing additional query flags.booleanexecuteWithFlags(String sql, int flags)Execute a query, passing additional query flags.booleanexecuteWithFlags(CachedQuery simpleQuery, int flags)Execute a query, passing additional query flags.ConnectiongetConnection()intgetFetchDirection()StringgetFetchingCursorName()intgetFetchSize()protected booleangetForceBinaryTransfer()ResultSetgetGeneratedKeys()longgetLargeMaxRows()longgetLargeUpdateCount()longgetLastOID()Returns the Last inserted/updated oid.intgetMaxFieldSize()intgetMaxRows()booleangetMoreResults()booleangetMoreResults(int current)BaseConnectiongetPGConnection()intgetPrepareThreshold()Gets the server-side prepare reuse threshold in use for this statement.intgetQueryTimeout()longgetQueryTimeoutMs()The queryTimeout limit is the number of milliseconds the driver will wait for a Statement to execute.ResultSetgetResultSet()intgetResultSetConcurrency()intgetResultSetHoldability()intgetResultSetType()protected ResultSetgetSingleResultSet()intgetUpdateCount()SQLWarninggetWarnings()booleanisClosed()booleanisCloseOnCompletion()protected booleanisOneShotQuery(CachedQuery cachedQuery)Returns true if query is unlikely to be reused.booleanisPoolable()booleanisUseServerPrepare()Checks if this statement will be executed as a server-prepared statement.booleanisWrapperFor(Class<?> iface)voidsetCursorName(String name)voidsetEscapeProcessing(boolean enable)voidsetFetchDirection(int direction)voidsetFetchSize(int rows)voidsetLargeMaxRows(long max)voidsetMaxFieldSize(int max)voidsetMaxRows(int max)voidsetPoolable(boolean poolable)voidsetPrepareThreshold(int newThreshold)Sets the reuse threshold for using server-prepared statements.voidsetQueryTimeout(int seconds)voidsetQueryTimeoutMs(long millis)Sets the queryTimeout limit.voidsetUseServerPrepare(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).protected voidtransformQueriesAndParameters()<T> Tunwrap(Class<T> iface)protected booleanwantsHoldableResultSet()protected booleanwantsScrollableResultSet()-
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 Detail
-
batchParameters
protected ArrayList<ParameterList> batchParameters
-
resultsettype
protected final int resultsettype
-
concurrency
protected final int concurrency
-
fetchdirection
protected int fetchdirection
-
wantsGeneratedKeysOnce
protected boolean wantsGeneratedKeysOnce
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.
-
wantsGeneratedKeysAlways
public boolean wantsGeneratedKeysAlways
Was this PreparedStatement created to return generated keys for every execution? This is set at creation time and never cleared by execution.
-
connection
protected final BaseConnection connection
-
warnings
protected volatile org.postgresql.jdbc.PSQLWarningWrapper warnings
The warnings chain.
-
maxrows
protected int maxrows
Maximum number of rows to return, 0 = unlimited.
-
fetchSize
protected int fetchSize
Number of rows to get in a batch.
-
timeout
protected long timeout
Timeout (in milliseconds) for a query.
-
replaceProcessingEnabled
protected boolean replaceProcessingEnabled
-
result
protected ResultWrapper result
The current results.
-
firstUnclosedResult
protected ResultWrapper firstUnclosedResult
The first unclosed result.
-
generatedKeys
protected ResultWrapper generatedKeys
Results returned by a statement that wants generated keys.
-
mPrepareThreshold
protected int mPrepareThreshold
-
maxFieldSize
protected int maxFieldSize
-
-
Method Detail
-
createResultSet
public ResultSet createResultSet(Query originalQuery, Field[] fields, List<Tuple> tuples, ResultCursor cursor) throws SQLException
Description copied from interface:BaseStatementCreate a resultset from data retrieved from the server.- Specified by:
createResultSetin 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
public BaseConnection getPGConnection()
-
getFetchingCursorName
public String getFetchingCursorName()
-
getFetchSize
public int getFetchSize()
- Specified by:
getFetchSizein interfaceStatement
-
wantsScrollableResultSet
protected boolean wantsScrollableResultSet()
-
wantsHoldableResultSet
protected boolean wantsHoldableResultSet()
-
executeQuery
public ResultSet executeQuery(String sql) throws SQLException
- Specified by:
executeQueryin interfaceStatement- Throws:
SQLException
-
getSingleResultSet
protected ResultSet getSingleResultSet() throws SQLException
- Throws:
SQLException
-
executeUpdate
public int executeUpdate(String sql) throws SQLException
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
checkNoResultUpdate
protected final void checkNoResultUpdate() throws SQLException- Throws:
SQLException
-
execute
public boolean execute(String sql) throws SQLException
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
executeWithFlags
public boolean executeWithFlags(String sql, int flags) throws SQLException
Description copied from interface:BaseStatementExecute a query, passing additional query flags.- Specified by:
executeWithFlagsin interfaceBaseStatement- Parameters:
sql- the query to execute (JDBC-style query)flags- additionalQueryExecutorflags 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
public boolean executeWithFlags(CachedQuery simpleQuery, int flags) throws SQLException
Description copied from interface:BaseStatementExecute a query, passing additional query flags.- Specified by:
executeWithFlagsin interfaceBaseStatement- Parameters:
simpleQuery- the query to execute (native to PostgreSQL)flags- additionalQueryExecutorflags 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
public boolean executeWithFlags(int flags) throws SQLExceptionDescription copied from interface:BaseStatementExecute a prepared query, passing additional query flags.- Specified by:
executeWithFlagsin interfaceBaseStatement- Parameters:
flags- additionalQueryExecutorflags for execution; these are bitwise-ORed into the default flags.- Returns:
- true if there is a result set
- Throws:
SQLException- if something goes wrong.
-
closeForNextExecution
protected void closeForNextExecution() throws SQLException- Throws:
SQLException
-
isOneShotQuery
protected boolean isOneShotQuery(CachedQuery cachedQuery)
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
-
setCursorName
public void setCursorName(String name) throws SQLException
- Specified by:
setCursorNamein interfaceStatement- Throws:
SQLException
-
getUpdateCount
public int getUpdateCount() throws SQLException- Specified by:
getUpdateCountin interfaceStatement- Throws:
SQLException
-
getMoreResults
public boolean getMoreResults() throws SQLException- Specified by:
getMoreResultsin interfaceStatement- Throws:
SQLException
-
getMaxRows
public int getMaxRows() throws SQLException- Specified by:
getMaxRowsin interfaceStatement- Throws:
SQLException
-
setMaxRows
public void setMaxRows(int max) throws SQLException- Specified by:
setMaxRowsin interfaceStatement- Throws:
SQLException
-
setEscapeProcessing
public void setEscapeProcessing(boolean enable) throws SQLException- Specified by:
setEscapeProcessingin interfaceStatement- Throws:
SQLException
-
getQueryTimeout
public int getQueryTimeout() throws SQLException- Specified by:
getQueryTimeoutin interfaceStatement- Throws:
SQLException
-
setQueryTimeout
public void setQueryTimeout(int seconds) throws SQLException- Specified by:
setQueryTimeoutin interfaceStatement- Throws:
SQLException
-
getQueryTimeoutMs
public long getQueryTimeoutMs() throws SQLExceptionThe 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
public void setQueryTimeoutMs(long millis) throws SQLExceptionSets the queryTimeout limit.- Parameters:
millis- - the new query timeout limit in milliseconds- Throws:
SQLException- if a database access error occurs
-
addWarning
public void addWarning(SQLWarning warn)
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
public SQLWarning getWarnings() throws SQLException
- Specified by:
getWarningsin interfaceStatement- Throws:
SQLException
-
getMaxFieldSize
public int getMaxFieldSize() throws SQLException- Specified by:
getMaxFieldSizein interfaceStatement- Throws:
SQLException
-
setMaxFieldSize
public void setMaxFieldSize(int max) throws SQLException- Specified by:
setMaxFieldSizein interfaceStatement- Throws:
SQLException
-
clearWarnings
public void clearWarnings() throws SQLExceptionClears 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:
clearWarningsin interfaceStatement- Throws:
SQLException
-
getResultSet
public ResultSet getResultSet() throws SQLException
- Specified by:
getResultSetin interfaceStatement- Throws:
SQLException
-
close
public final void close() throws SQLExceptionNote: even thoughStatementis automatically closed when it is garbage collected, it is better to close it explicitly to lower resource consumption.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceStatement- Throws:
SQLException
-
closeImpl
protected void closeImpl() throws SQLExceptionThis is guaranteed to be called exactly once even in case of concurrentclose()calls.- Throws:
SQLException- in case of error
-
getLastOID
public long getLastOID() throws SQLExceptionDescription copied from interface:PGStatementReturns the Last inserted/updated oid.- Specified by:
getLastOIDin interfacePGStatement- Returns:
- OID of last insert
- Throws:
SQLException- if something goes wrong
-
setPrepareThreshold
public void setPrepareThreshold(int newThreshold) throws SQLExceptionDescription copied from interface:PGStatementSets the reuse threshold for using server-prepared statements.
If
thresholdis a non-zero value N, the Nth and subsequent reuses of a PreparedStatement will use server-side prepare.If
thresholdis 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:
setPrepareThresholdin 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:PGStatementGets the server-side prepare reuse threshold in use for this statement.- Specified by:
getPrepareThresholdin interfacePGStatement- Returns:
- the current threshold
- See Also:
PGStatement.setPrepareThreshold(int)
-
setUseServerPrepare
public void setUseServerPrepare(boolean flag) throws SQLExceptionDescription copied from interface:PGStatementTurn 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:
setUseServerPreparein interfacePGStatement- Parameters:
flag- use server prepare- Throws:
SQLException- if something goes wrong
-
isUseServerPrepare
public boolean isUseServerPrepare()
Description copied from interface:PGStatementChecks if this statement will be executed as a server-prepared statement. A return value oftrueindicates that the next execution of the statement will be done as a server-prepared statement, assuming the underlying protocol supports it.- Specified by:
isUseServerPreparein interfacePGStatement- Returns:
- true if the next reuse of this statement will use a server-prepared statement
-
checkClosed
protected void checkClosed() throws SQLException- Throws:
SQLException
-
addBatch
public void addBatch(String sql) throws SQLException
- Specified by:
addBatchin interfaceStatement- Throws:
SQLException
-
clearBatch
public void clearBatch() throws SQLException- Specified by:
clearBatchin interfaceStatement- Throws:
SQLException
-
createBatchHandler
protected BatchResultHandler createBatchHandler(Query[] queries, ParameterList[] parameterLists)
-
executeBatch
public int[] executeBatch() throws SQLException- Specified by:
executeBatchin interfaceStatement- Throws:
SQLException
-
cancel
public void cancel() throws SQLException- Specified by:
cancelin interfaceStatement- Throws:
SQLException
-
getConnection
public Connection getConnection() throws SQLException
- Specified by:
getConnectionin interfaceStatement- Throws:
SQLException
-
getFetchDirection
public int getFetchDirection()
- Specified by:
getFetchDirectionin interfaceStatement
-
getResultSetConcurrency
public int getResultSetConcurrency()
- Specified by:
getResultSetConcurrencyin interfaceStatement
-
getResultSetType
public int getResultSetType()
- Specified by:
getResultSetTypein interfaceStatement
-
setFetchDirection
public void setFetchDirection(int direction) throws SQLException- Specified by:
setFetchDirectionin interfaceStatement- Throws:
SQLException
-
setFetchSize
public void setFetchSize(int rows) throws SQLException- Specified by:
setFetchSizein interfaceStatement- Throws:
SQLException
-
getForceBinaryTransfer
protected boolean getForceBinaryTransfer()
-
getLargeUpdateCount
public long getLargeUpdateCount() throws SQLException- Specified by:
getLargeUpdateCountin interfaceStatement- Throws:
SQLException
-
setLargeMaxRows
public void setLargeMaxRows(long max) throws SQLException- Specified by:
setLargeMaxRowsin interfaceStatement- Throws:
SQLException
-
getLargeMaxRows
public long getLargeMaxRows() throws SQLException- Specified by:
getLargeMaxRowsin interfaceStatement- Throws:
SQLException
-
executeLargeBatch
public long[] executeLargeBatch() throws SQLException- Specified by:
executeLargeBatchin interfaceStatement- Throws:
SQLException
-
executeLargeUpdate
public long executeLargeUpdate(String sql) throws SQLException
- Specified by:
executeLargeUpdatein interfaceStatement- Throws:
SQLException
-
executeLargeUpdate
public long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException
- Specified by:
executeLargeUpdatein interfaceStatement- Throws:
SQLException
-
executeLargeUpdate
public long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException
- Specified by:
executeLargeUpdatein interfaceStatement- Throws:
SQLException
-
executeLargeUpdate
public long executeLargeUpdate(String sql, String[] columnNames) throws SQLException
- Specified by:
executeLargeUpdatein interfaceStatement- Throws:
SQLException
-
isClosed
public boolean isClosed() throws SQLException- Specified by:
isClosedin interfaceStatement- Throws:
SQLException
-
setPoolable
public void setPoolable(boolean poolable) throws SQLException- Specified by:
setPoolablein interfaceStatement- Throws:
SQLException
-
isPoolable
public boolean isPoolable() throws SQLException- Specified by:
isPoolablein interfaceStatement- Throws:
SQLException
-
isWrapperFor
public boolean isWrapperFor(Class<?> iface) throws SQLException
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
unwrap
public <T> T unwrap(Class<T> iface) throws SQLException
- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
closeOnCompletion
public void closeOnCompletion() throws SQLException- Specified by:
closeOnCompletionin interfaceStatement- Throws:
SQLException
-
isCloseOnCompletion
public boolean isCloseOnCompletion() throws SQLException- Specified by:
isCloseOnCompletionin interfaceStatement- Throws:
SQLException
-
checkCompletion
protected void checkCompletion() throws SQLException- Throws:
SQLException
-
getMoreResults
public boolean getMoreResults(int current) throws SQLException- Specified by:
getMoreResultsin interfaceStatement- Throws:
SQLException
-
getGeneratedKeys
public ResultSet getGeneratedKeys() throws SQLException
- Specified by:
getGeneratedKeysin interfaceStatement- Throws:
SQLException
-
executeUpdate
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
executeUpdate
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
executeUpdate
public int executeUpdate(String sql, String[] columnNames) throws SQLException
- Specified by:
executeUpdatein interfaceStatement- Throws:
SQLException
-
execute
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
execute
public boolean execute(String sql, int[] columnIndexes) throws SQLException
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
execute
public boolean execute(String sql, String[] columnNames) throws SQLException
- Specified by:
executein interfaceStatement- Throws:
SQLException
-
getResultSetHoldability
public int getResultSetHoldability() throws SQLException- Specified by:
getResultSetHoldabilityin interfaceStatement- Throws:
SQLException
-
createDriverResultSet
public ResultSet createDriverResultSet(Field[] fields, List<Tuple> tuples) throws SQLException
Description copied from interface:BaseStatementCreate a synthetic resultset from data provided by the driver.- Specified by:
createDriverResultSetin interfaceBaseStatement- Parameters:
fields- the column metadata for the resultsettuples- the resultset data- Returns:
- the new ResultSet
- Throws:
SQLException- if something goes wrong
-
transformQueriesAndParameters
protected void transformQueriesAndParameters() throws SQLException- Throws:
SQLException
-
-