Class MysqlIO
java.lang.Object
com.mysql.jdbc.MysqlIO
This class is used by Connection for communicating with the MySQL server.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intprotected static final intprotected static final intprotected static final intprotected static final intprotected longprotected static final intprotected MySQLConnectionprotected static final intprotected Stringprotected static final intprotected longprotected longprotected static final intprotected static final intprotected intprotected static final intThe connection to the serverprotected InputStreamprotected BufferedOutputStreamData to the serverprotected static final intprotected intprotected Stringstatic final int(package private) static final intprotected intprotected intprotected SocketFactoryprotected static final StringWe need to have a 'marker' for all-zero datetimes so that ResultSet can decide what to do based on connection settingprotected static final String -
Constructor Summary
ConstructorsConstructorDescriptionMysqlIO(String host, int port, Properties props, String socketFactoryClassName, MySQLConnection conn, int socketTimeout, int useBufferRowSizeThreshold) Constructor: Connect to the MySQL server and setup a stream connection. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidchangeUser(String userName, String password, String database) Re-authenticates as the given user and passwordprotected BufferChecks for errors in the reply packet, and if none, returns the reply packet, ready for readingprotected voidDetermines if the database charset is the same as the platform charsetprotected void(package private) voidcloseStreamer(RowData streamer) (package private) void(package private) voiddoHandshake(String user, String password, String database) Initialize communications with the MySQL server.protected void(package private) voidprotected voidexplainSlowQuery(byte[] querySQL, String truncatedQuery) Runs an 'EXPLAIN' on the given query and dumps the results to the logprotected List<ResultSetRow> fetchRowsViaCursor(List<ResultSetRow> fetchedRows, long statementId, Field[] columnTypes, int fetchSize, boolean useBufferRowExplicit) protected final voidForcibly closes the underlying socket to MySQL.protected intprotected long(package private) StringGet the Java encoding to be used for the handshake response.protected ExceptionInterceptor(package private) StringgetHost()Returns the host this IO is connected toprotected longprotected long(package private) static intprotected NetworkResourcesprotected ResultSetImplgetResultSet(StatementImpl callingStatement, long columnCount, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, boolean isBinaryEncoded, Field[] metadataFromCache) Build a result set.(package private) final intGet the major version of the MySQL server we are talking to.(package private) final intGet the minor version of the MySQL server we are talking to.protected int(package private) final intGet the sub-minor version of the MySQL server we are talking to.(package private) StringGet the version string of the server we are talking to(package private) BufferReturns the packet used for sending data (used by PreparedStatement) Guarded by external synchronization on a mutex.protected longprotected long(package private) booleanbooleanDoes the server send back extra column info?protected boolean(package private) ResultSetInternalMethodsinvokeStatementInterceptorsPost(String sql, Statement interceptedStatement, ResultSetInternalMethods originalResultSet, boolean forceExecute, SQLException statementException) (package private) ResultSetInternalMethodsinvokeStatementInterceptorsPre(String sql, Statement interceptedStatement, boolean forceExecute) protected booleanbooleanprotected booleanisSetNeededForAutoCommitMode(boolean autoCommitFlag) boolean(package private) booleanisVersion(int major, int minor, int subminor) Is the version of the MySQL server we are connected to the given version?(package private) final ResultSetRownextRow(Field[] fields, int columnCount, boolean isBinaryEncoded, int resultSetConcurrency, boolean useBufferRowIfPossible, boolean useBufferRowExplicit, boolean canReuseRowPacketForBufferRow, Buffer existingRowPacket) Retrieve one row from the MySQL server.(package private) final ResultSetRownextRowFast(Field[] fields, int columnCount, boolean isBinaryEncoded, int resultSetConcurrency, boolean useBufferRowIfPossible, boolean useBufferRowExplicit, boolean canReuseRowPacket) (package private) final voidquit()Log-off of the MySQL server and close the socket.(package private) ResultSetImplreadAllResults(StatementImpl callingStatement, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Buffer resultPacket, boolean isBinaryEncoded, long preSentColumnCount, Field[] metadataFromCache) protected final BufferRead one packet from the MySQL serverprotected final ResultSetImplreadResultsForQueryOrUpdate(StatementImpl callingStatement, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Buffer resultPacket, boolean isBinaryEncoded, long preSentColumnCount, Field[] metadataFromCache) Reads one result set off of the wire, if the result is actually an update count, creates an update-count only result set.protected void(package private) voidSets the buffer size to max-bufprotected void(package private) void(package private) voidsecureAuth411(Buffer packet, int packLength, String user, String password, String database, boolean writeClientParams, boolean forChangeUser) Secure authentication for 4.1.1 and newer servers.(package private) final BuffersendCommand(int command, String extraData, Buffer queryPacket, boolean skipCheck, String extraDataCharEncoding, int timeoutMillis) Send a command to the MySQL server If data is to be sent with command, it should be put in extraData.protected voidsetSocketTimeout(int milliseconds) protected voidsetStatementInterceptors(List<StatementInterceptorV2> statementInterceptors) protected booleanprotected final voidReads and discards a single MySQL packet from the input stream.(package private) final ResultSetInternalMethodssqlQueryDirect(StatementImpl callingStatement, String query, String characterEncoding, Buffer queryPacket, int maxRows, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, Field[] cachedMetadata) Send a query stored in a packet directly to the server.(package private) booleantackOnMoreStreamingResults(ResultSetImpl addingTo) protected final FieldunpackField(Buffer packet, boolean extractDefaultValues) Unpacks the Field information from the given packet.static booleanuseBufferRowExplicit(Field[] fields) protected boolean(package private) booleanversionMeetsMinimum(int major, int minor, int subminor) Does the version of the MySQL server we are connected to meet the given minimums?
-
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
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
- See Also:
-
serverCharsetIndex
protected int serverCharsetIndex -
mysqlOutput
Data to the server -
connection
-
mysqlInput
-
mysqlConnection
The connection to the server -
socketFactory
-
host
-
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 toport- the port number that the server is listening onprops- the Properties from DriverManager.getConnection()socketFactoryClassName- the socket factory to useconn- the Connection that is creating ussocketTimeout- 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
- 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 setmaxRows- 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 createdisBinaryEncoded- 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
-
forceClose
protected final void forceClose()Forcibly closes the underlying socket to MySQL. -
skipPacket
Reads and discards a single MySQL packet from the input stream.- Throws:
SQLException- if the network fails while skipping the packet.
-
readPacket
Read one packet from the MySQL server- Returns:
- the packet from the server.
- Throws:
SQLExceptionCommunicationsException
-
unpackField
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 informationextractDefaultValues- should default values be extracted?- Returns:
- the unpacked field
- Throws:
SQLException
-
isSetNeededForAutoCommitMode
protected boolean isSetNeededForAutoCommitMode(boolean autoCommitFlag) -
inTransactionOnServer
protected boolean inTransactionOnServer() -
changeUser
Re-authenticates as the given user and password- Parameters:
userName-password-database-- Throws:
SQLException
-
checkErrorPacket
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
- Throws:
SQLException
-
resetReadPacketSequence
protected void resetReadPacketSequence() -
dumpPacketRingBuffer
- Throws:
SQLException
-
explainSlowQuery
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
Initialize communications with the MySQL server. Handles logging on, and handling initial connection errors.- Parameters:
user-password-database-- Throws:
SQLExceptionCommunicationsException
-
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
Log-off of the MySQL server and close the socket.- Throws:
SQLException
-
closeStreamer
- Throws:
SQLException
-
tackOnMoreStreamingResults
- 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 MysqlDefsextraData- any 'string' data for the commandqueryPacket- a packet pre-loaded with data for the protocol (i.e. from a client-side prepared statement).skipCheck- do not call checkErrorPacket() if trueextraDataCharEncoding- 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 versionminor- the minor versionsubminor- 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- scrollabilityresultSetConcurrency- updatabilitystreamResults- should the driver leave the results on the wire, and read them only when needed?catalog- the catalog in useresultPacket- the first packet of information in the result setisBinaryEncoded- 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
-
enableMultiQueries
- Throws:
SQLException
-
disableMultiQueries
- Throws:
SQLException
-
hadWarnings
boolean hadWarnings() -
scanForAndThrowDataTruncation
- 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
-
getCommandCount
protected int getCommandCount() -
setStatementInterceptors
-
getExceptionInterceptor
-
setSocketTimeout
- 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()
-