Class StatementImpl

java.lang.Object
com.mysql.cj.jdbc.StatementImpl
All Implemented Interfaces:
JdbcStatement, Query, java.lang.AutoCloseable, java.sql.Statement, java.sql.Wrapper
Direct Known Subclasses:
ClientPreparedStatement

public class StatementImpl
extends java.lang.Object
implements JdbcStatement
A Statement object is used for executing a static SQL statement and obtaining the results produced by it. Only one ResultSet per Statement can be open at any point in time. Therefore, if the reading of one ResultSet is interleaved with the reading of another, each must have been generated by different Statements. All statement execute methods implicitly close a statement's current ResultSet if an open one exists.
  • Field Details

  • Constructor Details

    • StatementImpl

      public StatementImpl​(JdbcConnection c, java.lang.String db) throws java.sql.SQLException
      Constructor for a Statement.
      Parameters:
      c - the Connection instance that creates us
      db - the database name in use when we were created
      Throws:
      java.sql.SQLException - if an error occurs.
  • Method Details

    • initQuery

      protected void initQuery()
    • addBatch

      public void addBatch​(java.lang.String sql) throws java.sql.SQLException
      Specified by:
      addBatch in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • addBatch

      public void addBatch​(java.lang.Object batch)
      Specified by:
      addBatch in interface Query
    • getBatchedArgs

      public java.util.List<java.lang.Object> getBatchedArgs()
      Description copied from interface: Query
      Get the batched args as added by the addBatch method(s). The list is unmodifiable and might contain any combination of String, ClientPreparedQueryBindings, or ServerPreparedQueryBindings depending on how the parameters were batched.
      Specified by:
      getBatchedArgs in interface Query
      Returns:
      an unmodifiable List of batched args
    • cancel

      public void cancel() throws java.sql.SQLException
      Specified by:
      cancel in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • checkClosed

      protected JdbcConnection checkClosed()
      Checks if closed() has been called, and throws an exception if so
      Returns:
      connection
      Throws:
      StatementIsClosedException - if this statement has been closed
    • checkForDml

      protected void checkForDml​(java.lang.String sql, char firstStatementChar) throws java.sql.SQLException
      Checks if the given SQL query with the given first non-ws char is a DML statement. Throws an exception if it is.
      Parameters:
      sql - the SQL to check
      firstStatementChar - the UC first non-ws char of the statement
      Throws:
      java.sql.SQLException - if the statement contains DML
    • checkNullOrEmptyQuery

      protected void checkNullOrEmptyQuery​(java.lang.String sql) throws java.sql.SQLException
      Method checkNullOrEmptyQuery.
      Parameters:
      sql - the SQL to check
      Throws:
      java.sql.SQLException - if query is null or empty.
    • clearBatch

      public void clearBatch() throws java.sql.SQLException
      Specified by:
      clearBatch in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • clearBatchedArgs

      public void clearBatchedArgs()
      Specified by:
      clearBatchedArgs in interface Query
    • clearWarnings

      public void clearWarnings() throws java.sql.SQLException
      Specified by:
      clearWarnings in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • close

      public void close() throws java.sql.SQLException
      In many cases, it is desirable to immediately release a Statement's database and JDBC resources instead of waiting for this to happen when it is automatically closed. The close method provides this immediate release.

      Note: A Statement is automatically closed when it is garbage collected. When a Statement is closed, its current ResultSet, if one exists, is also closed.

      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface java.sql.Statement
      Throws:
      java.sql.SQLException - if a database access error occurs
    • closeAllOpenResults

      protected void closeAllOpenResults() throws java.sql.SQLException
      Close any open result sets that have been 'held open'
      Throws:
      java.sql.SQLException - if an error occurs
    • implicitlyCloseAllOpenResults

      protected void implicitlyCloseAllOpenResults() throws java.sql.SQLException
      Close all result sets in this statement. This includes multi-results
      Throws:
      java.sql.SQLException - if a database access error occurs
    • removeOpenResultSet

      public void removeOpenResultSet​(ResultSetInternalMethods rs)
      Description copied from interface: JdbcStatement
      Callback for result set instances to remove them from the Set that tracks them per-statement
      Specified by:
      removeOpenResultSet in interface JdbcStatement
      Parameters:
      rs - result set
    • getOpenResultSetCount

      public int getOpenResultSetCount()
      Description copied from interface: JdbcStatement
      Returns the number of open result sets for this statement.
      Specified by:
      getOpenResultSetCount in interface JdbcStatement
      Returns:
      the number of open result sets for this statement
    • createStreamingResultSet

      protected boolean createStreamingResultSet()
      We only stream result sets when they are forward-only, read-only, and the fetch size has been set to Integer.MIN_VALUE
      Returns:
      true if this result set should be streamed row at-a-time, rather than read all at once.
    • enableStreamingResults

      public void enableStreamingResults() throws java.sql.SQLException
      Description copied from interface: JdbcStatement
      Workaround for containers that 'check' for sane values of Statement.setFetchSize() so that applications can use the Java variant of libmysql's mysql_use_result() behavior.
      Specified by:
      enableStreamingResults in interface JdbcStatement
      Throws:
      java.sql.SQLException - if an error occurs
    • disableStreamingResults

      public void disableStreamingResults() throws java.sql.SQLException
      Description copied from interface: JdbcStatement
      Resets this statements fetch size and result set type to the values they had before enableStreamingResults() was called.
      Specified by:
      disableStreamingResults in interface JdbcStatement
      Throws:
      java.sql.SQLException - if an error occurs
    • setupStreamingTimeout

      protected void setupStreamingTimeout​(JdbcConnection con) throws java.sql.SQLException
      Adjust net_write_timeout to a higher value if we're streaming result sets. More often than not, someone runs into an issue where they blow net_write_timeout when using this feature, and if they're willing to hold a result set open for 30 seconds or more, one more round-trip isn't going to hurt. This is reset by RowDataDynamic.close().
      Parameters:
      con - created this statement
      Throws:
      java.sql.SQLException - if a database error occurs
    • startQueryTimer

      public CancelQueryTask startQueryTimer​(Query stmtToCancel, int timeout)
      Specified by:
      startQueryTimer in interface Query
    • stopQueryTimer

      public void stopQueryTimer​(CancelQueryTask timeoutTask, boolean rethrowCancelReason, boolean checkCancelTimeout)
      Specified by:
      stopQueryTimer in interface Query
    • execute

      public boolean execute​(java.lang.String sql) throws java.sql.SQLException
      Specified by:
      execute in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • statementBegins

      public void statementBegins()
      Specified by:
      statementBegins in interface Query
    • resetCancelledState

      public void resetCancelledState()
      Specified by:
      resetCancelledState in interface Query
    • execute

      public boolean execute​(java.lang.String sql, int returnGeneratedKeys) throws java.sql.SQLException
      Specified by:
      execute in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • execute

      public boolean execute​(java.lang.String sql, int[] generatedKeyIndices) throws java.sql.SQLException
      Specified by:
      execute in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • execute

      public boolean execute​(java.lang.String sql, java.lang.String[] generatedKeyNames) throws java.sql.SQLException
      Specified by:
      execute in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeBatch

      public int[] executeBatch() throws java.sql.SQLException
      Specified by:
      executeBatch in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeBatchInternal

      protected long[] executeBatchInternal() throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • hasDeadlockOrTimeoutRolledBackTx

      protected final boolean hasDeadlockOrTimeoutRolledBackTx​(java.sql.SQLException ex)
    • processMultiCountsAndKeys

      protected int processMultiCountsAndKeys​(StatementImpl batchedStatement, int updateCountCounter, long[] updateCounts) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • handleExceptionForBatch

      protected java.sql.SQLException handleExceptionForBatch​(int endOfBatchIndex, int numValuesPerBatch, long[] updateCounts, java.sql.SQLException ex) throws java.sql.BatchUpdateException, java.sql.SQLException
      Throws:
      java.sql.BatchUpdateException
      java.sql.SQLException
    • executeQuery

      public java.sql.ResultSet executeQuery​(java.lang.String sql) throws java.sql.SQLException
      Specified by:
      executeQuery in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • doPingInstead

      protected void doPingInstead() throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • generatePingResultSet

      protected ResultSetInternalMethods generatePingResultSet() throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • executeSimpleNonQuery

      public void executeSimpleNonQuery​(JdbcConnection c, java.lang.String nonQuery) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • executeUpdate

      public int executeUpdate​(java.lang.String sql) throws java.sql.SQLException
      Specified by:
      executeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeUpdateInternal

      protected long executeUpdateInternal​(java.lang.String sql, boolean isBatch, boolean returnGeneratedKeys) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • executeUpdate

      public int executeUpdate​(java.lang.String sql, int autoGeneratedKeys) throws java.sql.SQLException
      Specified by:
      executeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeUpdate

      public int executeUpdate​(java.lang.String sql, int[] columnIndexes) throws java.sql.SQLException
      Specified by:
      executeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeUpdate

      public int executeUpdate​(java.lang.String sql, java.lang.String[] columnNames) throws java.sql.SQLException
      Specified by:
      executeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getConnection

      public java.sql.Connection getConnection() throws java.sql.SQLException
      Specified by:
      getConnection in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getFetchDirection

      public int getFetchDirection() throws java.sql.SQLException
      Specified by:
      getFetchDirection in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getFetchSize

      public int getFetchSize() throws java.sql.SQLException
      Specified by:
      getFetchSize in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getGeneratedKeys

      public java.sql.ResultSet getGeneratedKeys() throws java.sql.SQLException
      Specified by:
      getGeneratedKeys in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getGeneratedKeysInternal

      protected ResultSetInternalMethods getGeneratedKeysInternal() throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • getGeneratedKeysInternal

      protected ResultSetInternalMethods getGeneratedKeysInternal​(long numKeys) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • getLastInsertID

      public long getLastInsertID()
      getLastInsertID returns the value of the auto_incremented key after an executeQuery() or excute() call.

      This gets around the un-threadsafe behavior of "select LAST_INSERT_ID()" which is tied to the Connection that created this Statement, and therefore could have had many INSERTS performed before one gets a chance to call "select LAST_INSERT_ID()".

      Returns:
      the last update ID.
    • getLongUpdateCount

      public long getLongUpdateCount()
      getLongUpdateCount returns the current result as an update count, if the result is a ResultSet or there are no more results, -1 is returned. It should only be called once per result.

      This method returns longs as MySQL server returns 64-bit values for update counts

      Returns:
      the current update count.
    • getMaxFieldSize

      public int getMaxFieldSize() throws java.sql.SQLException
      Specified by:
      getMaxFieldSize in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getMaxRows

      public int getMaxRows() throws java.sql.SQLException
      Specified by:
      getMaxRows in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getMoreResults

      public boolean getMoreResults() throws java.sql.SQLException
      Specified by:
      getMoreResults in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getMoreResults

      public boolean getMoreResults​(int current) throws java.sql.SQLException
      Specified by:
      getMoreResults in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getQueryTimeout

      public int getQueryTimeout() throws java.sql.SQLException
      Specified by:
      getQueryTimeout in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getResultSet

      public java.sql.ResultSet getResultSet() throws java.sql.SQLException
      Specified by:
      getResultSet in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getResultSetConcurrency

      public int getResultSetConcurrency() throws java.sql.SQLException
      Specified by:
      getResultSetConcurrency in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getResultSetHoldability

      public int getResultSetHoldability() throws java.sql.SQLException
      Specified by:
      getResultSetHoldability in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getResultSetInternal

      protected ResultSetInternalMethods getResultSetInternal()
    • getResultSetType

      public int getResultSetType() throws java.sql.SQLException
      Specified by:
      getResultSetType in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getUpdateCount

      public int getUpdateCount() throws java.sql.SQLException
      Specified by:
      getUpdateCount in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getWarnings

      public java.sql.SQLWarning getWarnings() throws java.sql.SQLException
      Specified by:
      getWarnings in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • realClose

      protected void realClose​(boolean calledExplicitly, boolean closeOpenResults) throws java.sql.SQLException
      Closes this statement, and frees resources.
      Parameters:
      calledExplicitly - was this called from close()?
      closeOpenResults - should open result sets be closed?
      Throws:
      java.sql.SQLException - if an error occurs
    • setCursorName

      public void setCursorName​(java.lang.String name) throws java.sql.SQLException
      Specified by:
      setCursorName in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setEscapeProcessing

      public void setEscapeProcessing​(boolean enable) throws java.sql.SQLException
      Specified by:
      setEscapeProcessing in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setFetchDirection

      public void setFetchDirection​(int direction) throws java.sql.SQLException
      Specified by:
      setFetchDirection in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setFetchSize

      public void setFetchSize​(int rows) throws java.sql.SQLException
      Specified by:
      setFetchSize in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setHoldResultsOpenOverClose

      public void setHoldResultsOpenOverClose​(boolean holdResultsOpenOverClose)
      Specified by:
      setHoldResultsOpenOverClose in interface JdbcStatement
    • setMaxFieldSize

      public void setMaxFieldSize​(int max) throws java.sql.SQLException
      Specified by:
      setMaxFieldSize in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setMaxRows

      public void setMaxRows​(int max) throws java.sql.SQLException
      Specified by:
      setMaxRows in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setQueryTimeout

      public void setQueryTimeout​(int seconds) throws java.sql.SQLException
      Specified by:
      setQueryTimeout in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getBatchedGeneratedKeys

      protected void getBatchedGeneratedKeys​(java.sql.Statement batchedStatement) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • getBatchedGeneratedKeys

      protected void getBatchedGeneratedKeys​(int maxKeys) throws java.sql.SQLException
      Throws:
      java.sql.SQLException
    • isClosed

      public boolean isClosed() throws java.sql.SQLException
      Specified by:
      isClosed in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • isPoolable

      public boolean isPoolable() throws java.sql.SQLException
      Specified by:
      isPoolable in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setPoolable

      public void setPoolable​(boolean poolable) throws java.sql.SQLException
      Specified by:
      setPoolable in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • isWrapperFor

      public boolean isWrapperFor​(java.lang.Class<?> iface) throws java.sql.SQLException
      Specified by:
      isWrapperFor in interface java.sql.Wrapper
      Throws:
      java.sql.SQLException
    • unwrap

      public <T> T unwrap​(java.lang.Class<T> iface) throws java.sql.SQLException
      Specified by:
      unwrap in interface java.sql.Wrapper
      Throws:
      java.sql.SQLException
    • findStartOfStatement

      protected static int findStartOfStatement​(java.lang.String sql)
    • getLocalInfileInputStream

      public java.io.InputStream getLocalInfileInputStream()
      Description copied from interface: JdbcStatement
      Returns the InputStream instance that will be used to send data in response to a "LOAD DATA LOCAL INFILE" statement. This method returns NULL if no such stream has been set via setLocalInfileInputStream().
      Specified by:
      getLocalInfileInputStream in interface JdbcStatement
      Returns:
      input stream
    • setLocalInfileInputStream

      public void setLocalInfileInputStream​(java.io.InputStream stream)
      Description copied from interface: JdbcStatement
      Sets an InputStream instance that will be used to send data to the MySQL server for a "LOAD DATA LOCAL INFILE" statement rather than a FileInputStream or URLInputStream that represents the path given as an argument to the statement. This stream will be read to completion upon execution of a "LOAD DATA LOCAL INFILE" statement, and will automatically be closed by the driver, so it needs to be reset before each call to execute*() that would cause the MySQL server to request data to fulfill the request for "LOAD DATA LOCAL INFILE". If this value is set to NULL, the driver will revert to using a FileInputStream or URLInputStream as required.
      Specified by:
      setLocalInfileInputStream in interface JdbcStatement
      Parameters:
      stream - input stream
    • setPingTarget

      public void setPingTarget​(PingTarget pingTarget)
      Specified by:
      setPingTarget in interface JdbcStatement
    • getExceptionInterceptor

      public ExceptionInterceptor getExceptionInterceptor()
      Specified by:
      getExceptionInterceptor in interface JdbcStatement
    • containsOnDuplicateKeyInString

      protected boolean containsOnDuplicateKeyInString​(java.lang.String sql)
    • closeOnCompletion

      public void closeOnCompletion() throws java.sql.SQLException
      Specified by:
      closeOnCompletion in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • isCloseOnCompletion

      public boolean isCloseOnCompletion() throws java.sql.SQLException
      Specified by:
      isCloseOnCompletion in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeLargeBatch

      public long[] executeLargeBatch() throws java.sql.SQLException
      Specified by:
      executeLargeBatch in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeLargeUpdate

      public long executeLargeUpdate​(java.lang.String sql) throws java.sql.SQLException
      Specified by:
      executeLargeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeLargeUpdate

      public long executeLargeUpdate​(java.lang.String sql, int autoGeneratedKeys) throws java.sql.SQLException
      Specified by:
      executeLargeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeLargeUpdate

      public long executeLargeUpdate​(java.lang.String sql, int[] columnIndexes) throws java.sql.SQLException
      Specified by:
      executeLargeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • executeLargeUpdate

      public long executeLargeUpdate​(java.lang.String sql, java.lang.String[] columnNames) throws java.sql.SQLException
      Specified by:
      executeLargeUpdate in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getLargeMaxRows

      public long getLargeMaxRows() throws java.sql.SQLException
      Specified by:
      getLargeMaxRows in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getLargeUpdateCount

      public long getLargeUpdateCount() throws java.sql.SQLException
      Specified by:
      getLargeUpdateCount in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • setLargeMaxRows

      public void setLargeMaxRows​(long max) throws java.sql.SQLException
      Specified by:
      setLargeMaxRows in interface java.sql.Statement
      Throws:
      java.sql.SQLException
    • getCurrentDatabase

      public java.lang.String getCurrentDatabase()
      Specified by:
      getCurrentDatabase in interface Query
    • getServerStatementId

      public long getServerStatementId()
    • getResultSetFactory

      public <T extends Resultset,​ M extends Message> ProtocolEntityFactory<T,​M> getResultSetFactory()
      Specified by:
      getResultSetFactory in interface Query
    • getId

      public int getId()
      Description copied from interface: Query
      Returns the query id used when profiling
      Specified by:
      getId in interface Query
      Returns:
      id
    • setCancelStatus

      public void setCancelStatus​(Query.CancelStatus cs)
      Specified by:
      setCancelStatus in interface Query
    • checkCancelTimeout

      public void checkCancelTimeout()
      Specified by:
      checkCancelTimeout in interface Query
    • getSession

      public Session getSession()
      Specified by:
      getSession in interface Query
    • getCancelTimeoutMutex

      public java.lang.Object getCancelTimeoutMutex()
      Specified by:
      getCancelTimeoutMutex in interface Query
    • closeQuery

      public void closeQuery()
      Specified by:
      closeQuery in interface Query
    • getResultFetchSize

      public int getResultFetchSize()
      Specified by:
      getResultFetchSize in interface Query
    • setResultFetchSize

      public void setResultFetchSize​(int fetchSize)
      Specified by:
      setResultFetchSize in interface Query
    • getResultType

      public Resultset.Type getResultType()
      Specified by:
      getResultType in interface Query
    • setResultType

      public void setResultType​(Resultset.Type resultSetType)
      Specified by:
      setResultType in interface Query
    • getTimeoutInMillis

      public int getTimeoutInMillis()
      Specified by:
      getTimeoutInMillis in interface Query
    • setTimeoutInMillis

      public void setTimeoutInMillis​(int timeoutInMillis)
      Specified by:
      setTimeoutInMillis in interface Query
    • getExecuteTime

      public long getExecuteTime()
      Description copied from interface: Query
      Returns the elapsed time for the server to execute the query.
      Specified by:
      getExecuteTime in interface Query
      Returns:
      the time it took for the server to execute the query.
    • setExecuteTime

      public void setExecuteTime​(long executeTime)
      Specified by:
      setExecuteTime in interface Query
    • getStatementExecuting

      public java.util.concurrent.atomic.AtomicBoolean getStatementExecuting()
      Specified by:
      getStatementExecuting in interface Query
    • setCurrentDatabase

      public void setCurrentDatabase​(java.lang.String currentDb)
      Specified by:
      setCurrentDatabase in interface Query
    • isClearWarningsCalled

      public boolean isClearWarningsCalled()
      Specified by:
      isClearWarningsCalled in interface Query
    • setClearWarningsCalled

      public void setClearWarningsCalled​(boolean clearWarningsCalled)
      Specified by:
      setClearWarningsCalled in interface Query
    • getQuery

      public Query getQuery()
      Specified by:
      getQuery in interface JdbcStatement