Class MysqlIO

java.lang.Object
com.mysql.jdbc.MysqlIO

public class MysqlIO extends Object
This class is used by Connection for communicating with the MySQL server.
  • Field Details

    • NULL_LENGTH

      protected static final int NULL_LENGTH
      See Also:
    • COMP_HEADER_LENGTH

      protected static final int COMP_HEADER_LENGTH
      See Also:
    • MIN_COMPRESS_LEN

      protected static final int MIN_COMPRESS_LEN
      See Also:
    • HEADER_LENGTH

      protected static final int HEADER_LENGTH
      See Also:
    • AUTH_411_OVERHEAD

      protected static final int AUTH_411_OVERHEAD
      See Also:
    • SEED_LENGTH

      public static final int SEED_LENGTH
      See Also:
    • CLIENT_CONNECT_WITH_DB

      protected static final int CLIENT_CONNECT_WITH_DB
      See Also:
    • CLIENT_SSL

      protected static final int CLIENT_SSL
      See Also:
    • CLIENT_RESERVED

      protected static final int CLIENT_RESERVED
      See Also:
    • CLIENT_SECURE_CONNECTION

      protected static final int CLIENT_SECURE_CONNECTION
      See Also:
    • SERVER_MORE_RESULTS_EXISTS

      static final int SERVER_MORE_RESULTS_EXISTS
      See Also:
    • MAX_QUERY_SIZE_TO_LOG

      protected static final int MAX_QUERY_SIZE_TO_LOG
      See Also:
    • MAX_QUERY_SIZE_TO_EXPLAIN

      protected static final int MAX_QUERY_SIZE_TO_EXPLAIN
      See Also:
    • INITIAL_PACKET_SIZE

      protected static final int INITIAL_PACKET_SIZE
      See Also:
    • ZERO_DATE_VALUE_MARKER

      protected static final String ZERO_DATE_VALUE_MARKER
      We need to have a 'marker' for all-zero datetimes so that ResultSet can decide what to do based on connection setting
      See Also:
    • ZERO_DATETIME_VALUE_MARKER

      protected static final String ZERO_DATETIME_VALUE_MARKER
      See Also:
    • serverCharsetIndex

      protected int serverCharsetIndex
    • mysqlOutput

      protected BufferedOutputStream mysqlOutput
      Data to the server
    • connection

      protected MySQLConnection connection
    • mysqlInput

      protected InputStream mysqlInput
    • mysqlConnection

      public Socket mysqlConnection
      The connection to the server
    • socketFactory

      protected SocketFactory socketFactory
    • host

      protected String host
    • seed

      protected String seed
    • maxThreeBytes

      protected int maxThreeBytes
    • port

      protected int port
    • serverCapabilities

      protected int serverCapabilities
    • clientParam

      protected long clientParam
    • lastPacketSentTimeMs

      protected long lastPacketSentTimeMs
    • lastPacketReceivedTimeMs

      protected long lastPacketReceivedTimeMs
  • Constructor Details

    • MysqlIO

      public MysqlIO(String host, int port, Properties props, String socketFactoryClassName, MySQLConnection conn, int socketTimeout, int useBufferRowSizeThreshold) throws IOException, SQLException
      Constructor: Connect to the MySQL server and setup a stream connection.
      Parameters:
      host - the hostname to connect to
      port - the port number that the server is listening on
      props - the Properties from DriverManager.getConnection()
      socketFactoryClassName - the socket factory to use
      conn - the Connection that is creating us
      socketTimeout - the timeout to set for the socket (0 means no timeout)
      Throws:
      IOException - if an IOException occurs during connect.
      SQLException - if a database access error occurs.
  • Method Details

    • hasLongColumnInfo

      public boolean hasLongColumnInfo()
      Does the server send back extra column info?
      Returns:
      true if so
    • isDataAvailable

      protected boolean isDataAvailable() throws SQLException
      Throws:
      SQLException
    • getLastPacketSentTimeMs

      protected long getLastPacketSentTimeMs()
      Returns:
      Returns the lastPacketSentTimeMs.
    • getLastPacketReceivedTimeMs

      protected long getLastPacketReceivedTimeMs()
    • getResultSet

      protected ResultSetImpl getResultSet(StatementImpl callingStatement, long columnCount, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, boolean isBinaryEncoded, Field[] metadataFromCache) throws SQLException
      Build a result set. Delegates to buildResultSetWithRows() to build a JDBC-version-specific ResultSet, given rows as byte data, and field information.
      Parameters:
      callingStatement -
      columnCount - the number of columns in the result set
      maxRows - the maximum number of rows to read (-1 means all rows)
      resultSetType - (TYPE_FORWARD_ONLY, TYPE_SCROLL_????)
      resultSetConcurrency - the type of result set (CONCUR_UPDATABLE or READ_ONLY)
      streamResults - should the result set be read all at once, or streamed?
      catalog - the database name in use when the result set was created
      isBinaryEncoded - is this result set in native encoding?
      unpackFieldInfo - should we read MYSQL_FIELD info (if available)?
      Returns:
      a result set
      Throws:
      SQLException - if a database access error occurs
    • getNetworkResources

      protected NetworkResources getNetworkResources()
    • forceClose

      protected final void forceClose()
      Forcibly closes the underlying socket to MySQL.
    • skipPacket

      protected final void skipPacket() throws SQLException
      Reads and discards a single MySQL packet from the input stream.
      Throws:
      SQLException - if the network fails while skipping the packet.
    • readPacket

      protected final Buffer readPacket() throws SQLException
      Read one packet from the MySQL server
      Returns:
      the packet from the server.
      Throws:
      SQLException
      CommunicationsException
    • unpackField

      protected final Field unpackField(Buffer packet, boolean extractDefaultValues) throws SQLException
      Unpacks the Field information from the given packet. Understands pre 4.1 and post 4.1 server version field packet structures.
      Parameters:
      packet - the packet containing the field information
      extractDefaultValues - should default values be extracted?
      Returns:
      the unpacked field
      Throws:
      SQLException
    • isSetNeededForAutoCommitMode

      protected boolean isSetNeededForAutoCommitMode(boolean autoCommitFlag)
    • inTransactionOnServer

      protected boolean inTransactionOnServer()
    • changeUser

      protected void changeUser(String userName, String password, String database) throws SQLException
      Re-authenticates as the given user and password
      Parameters:
      userName -
      password -
      database -
      Throws:
      SQLException
    • checkErrorPacket

      protected Buffer checkErrorPacket() throws SQLException
      Checks for errors in the reply packet, and if none, returns the reply packet, ready for reading
      Returns:
      a packet ready for reading.
      Throws:
      SQLException - is the packet is an error packet
    • checkForCharsetMismatch

      protected void checkForCharsetMismatch()
      Determines if the database charset is the same as the platform charset
    • clearInputStream

      protected void clearInputStream() throws SQLException
      Throws:
      SQLException
    • resetReadPacketSequence

      protected void resetReadPacketSequence()
    • dumpPacketRingBuffer

      protected void dumpPacketRingBuffer() throws SQLException
      Throws:
      SQLException
    • explainSlowQuery

      protected void explainSlowQuery(byte[] querySQL, String truncatedQuery) throws SQLException
      Runs an 'EXPLAIN' on the given query and dumps the results to the log
      Parameters:
      querySQL -
      truncatedQuery -
      Throws:
      SQLException
    • getMaxBuf

      static int getMaxBuf()
    • getServerMajorVersion

      final int getServerMajorVersion()
      Get the major version of the MySQL server we are talking to.
    • getServerMinorVersion

      final int getServerMinorVersion()
      Get the minor version of the MySQL server we are talking to.
    • getServerSubMinorVersion

      final int getServerSubMinorVersion()
      Get the sub-minor version of the MySQL server we are talking to.
    • getServerVersion

      String getServerVersion()
      Get the version string of the server we are talking to
    • doHandshake

      void doHandshake(String user, String password, String database) throws SQLException
      Initialize communications with the MySQL server. Handles logging on, and handling initial connection errors.
      Parameters:
      user -
      password -
      database -
      Throws:
      SQLException
      CommunicationsException
    • nextRow

      final ResultSetRow nextRow(Field[] fields, int columnCount, boolean isBinaryEncoded, int resultSetConcurrency, boolean useBufferRowIfPossible, boolean useBufferRowExplicit, boolean canReuseRowPacketForBufferRow, Buffer existingRowPacket) throws SQLException
      Retrieve one row from the MySQL server. Note: this method is not thread-safe, but it is only called from methods that are guarded by synchronizing on this object.
      Parameters:
      fields -
      columnCount -
      isBinaryEncoded -
      resultSetConcurrency -
      b -
      Throws:
      SQLException
    • nextRowFast

      final ResultSetRow nextRowFast(Field[] fields, int columnCount, boolean isBinaryEncoded, int resultSetConcurrency, boolean useBufferRowIfPossible, boolean useBufferRowExplicit, boolean canReuseRowPacket) throws SQLException
      Throws:
      SQLException
    • quit

      final void quit() throws SQLException
      Log-off of the MySQL server and close the socket.
      Throws:
      SQLException
    • getSharedSendPacket

      Buffer getSharedSendPacket()
      Returns the packet used for sending data (used by PreparedStatement) Guarded by external synchronization on a mutex.
      Returns:
      A packet to send data with
    • closeStreamer

      void closeStreamer(RowData streamer) throws SQLException
      Throws:
      SQLException
    • tackOnMoreStreamingResults

      boolean tackOnMoreStreamingResults(ResultSetImpl addingTo) throws SQLException
      Throws:
      SQLException
    • readAllResults

      ResultSetImpl readAllResults(StatementImpl callingStatement, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Buffer resultPacket, boolean isBinaryEncoded, long preSentColumnCount, Field[] metadataFromCache) throws SQLException
      Throws:
      SQLException
    • resetMaxBuf

      void resetMaxBuf()
      Sets the buffer size to max-buf
    • sendCommand

      final Buffer sendCommand(int command, String extraData, Buffer queryPacket, boolean skipCheck, String extraDataCharEncoding, int timeoutMillis) throws SQLException
      Send a command to the MySQL server If data is to be sent with command, it should be put in extraData. Raw packets can be sent by setting queryPacket to something other than null.
      Parameters:
      command - the MySQL protocol 'command' from MysqlDefs
      extraData - any 'string' data for the command
      queryPacket - a packet pre-loaded with data for the protocol (i.e. from a client-side prepared statement).
      skipCheck - do not call checkErrorPacket() if true
      extraDataCharEncoding - the character encoding of the extraData parameter.
      Returns:
      the response packet from the server
      Throws:
      SQLException - if an I/O error or SQL error occurs
    • shouldIntercept

      protected boolean shouldIntercept()
    • sqlQueryDirect

      final ResultSetInternalMethods sqlQueryDirect(StatementImpl callingStatement, String query, String characterEncoding, Buffer queryPacket, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Field[] cachedMetadata) throws Exception
      Send a query stored in a packet directly to the server.
      Parameters:
      callingStatement -
      characterEncoding -
      queryPacket -
      maxRows -
      resultSetType -
      resultSetConcurrency -
      streamResults -
      catalog -
      conn -
      resultSetConcurrency -
      unpackFieldInfo - should we read MYSQL_FIELD info (if available)?
      Throws:
      Exception
    • invokeStatementInterceptorsPre

      ResultSetInternalMethods invokeStatementInterceptorsPre(String sql, Statement interceptedStatement, boolean forceExecute) throws SQLException
      Throws:
      SQLException
    • invokeStatementInterceptorsPost

      ResultSetInternalMethods invokeStatementInterceptorsPost(String sql, Statement interceptedStatement, ResultSetInternalMethods originalResultSet, boolean forceExecute, SQLException statementException) throws SQLException
      Throws:
      SQLException
    • getCurrentTimeNanosOrMillis

      protected long getCurrentTimeNanosOrMillis()
    • getHost

      String getHost()
      Returns the host this IO is connected to
    • isVersion

      boolean isVersion(int major, int minor, int subminor)
      Is the version of the MySQL server we are connected to the given version?
      Parameters:
      major - the major version
      minor - the minor version
      subminor - the subminor version
      Returns:
      true if the version of the MySQL server we are connected is the given version
    • versionMeetsMinimum

      boolean versionMeetsMinimum(int major, int minor, int subminor)
      Does the version of the MySQL server we are connected to meet the given minimums?
      Parameters:
      major -
      minor -
      subminor -
    • readResultsForQueryOrUpdate

      protected final ResultSetImpl readResultsForQueryOrUpdate(StatementImpl callingStatement, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Buffer resultPacket, boolean isBinaryEncoded, long preSentColumnCount, Field[] metadataFromCache) throws SQLException
      Reads one result set off of the wire, if the result is actually an update count, creates an update-count only result set.
      Parameters:
      callingStatement -
      maxRows - the maximum rows to return in the result set.
      resultSetType - scrollability
      resultSetConcurrency - updatability
      streamResults - should the driver leave the results on the wire, and read them only when needed?
      catalog - the catalog in use
      resultPacket - the first packet of information in the result set
      isBinaryEncoded - is this result set from a prepared statement?
      preSentColumnCount - do we already know the number of columns?
      unpackFieldInfo - should we unpack the field information?
      Returns:
      a result set that either represents the rows, or an update count
      Throws:
      SQLException - if an error occurs while reading the rows
    • useBufferRowExplicit

      public static boolean useBufferRowExplicit(Field[] fields)
    • enableMultiQueries

      void enableMultiQueries() throws SQLException
      Throws:
      SQLException
    • disableMultiQueries

      void disableMultiQueries() throws SQLException
      Throws:
      SQLException
    • hadWarnings

      boolean hadWarnings()
    • scanForAndThrowDataTruncation

      void scanForAndThrowDataTruncation() throws SQLException
      Throws:
      SQLException
    • secureAuth411

      void secureAuth411(Buffer packet, int packLength, String user, String password, String database, boolean writeClientParams, boolean forChangeUser) throws SQLException
      Secure authentication for 4.1.1 and newer servers.
      Parameters:
      packet -
      packLength -
      user -
      password -
      database -
      writeClientParams -
      forChangeUser -
      Throws:
      SQLException
    • isSSLEstablished

      public boolean isSSLEstablished()
    • getServerStatus

      protected int getServerStatus()
    • fetchRowsViaCursor

      protected List<ResultSetRow> fetchRowsViaCursor(List<ResultSetRow> fetchedRows, long statementId, Field[] columnTypes, int fetchSize, boolean useBufferRowExplicit) throws SQLException
      Throws:
      SQLException
    • getThreadId

      protected long getThreadId()
    • useNanosForElapsedTime

      protected boolean useNanosForElapsedTime()
    • getSlowQueryThreshold

      protected long getSlowQueryThreshold()
    • getQueryTimingUnits

      public String getQueryTimingUnits()
    • getCommandCount

      protected int getCommandCount()
    • setStatementInterceptors

      protected void setStatementInterceptors(List<StatementInterceptorV2> statementInterceptors)
    • getExceptionInterceptor

      protected ExceptionInterceptor getExceptionInterceptor()
    • setSocketTimeout

      protected void setSocketTimeout(int milliseconds) throws SQLException
      Throws:
      SQLException
    • releaseResources

      protected void releaseResources()
    • getEncodingForHandshake

      String getEncodingForHandshake()
      Get the Java encoding to be used for the handshake response. Defaults to UTF-8.
    • isEOFDeprecated

      public boolean isEOFDeprecated()