Package com.mysql.cj.protocol.x
Class XProtocol
java.lang.Object
- Direct Known Subclasses:
ClientImpl.PooledXProtocol
public class XProtocol extends AbstractProtocol<XMessage> implements Protocol<XMessage>
Low-level interface to communications with X Plugin.
-
Field Summary
Fields Modifier and Type Field Description static java.util.Map<java.lang.String,java.lang.Integer>COLLATION_NAME_TO_COLLATION_INDEXjava.lang.StringdefaultSchemaNameFields inherited from class com.mysql.cj.protocol.AbstractProtocol
authProvider, exceptionInterceptor, log, messageBuilder, packetDebugRingBuffer, propertySet, queryTimingUnits, session, socketConnection, transactionManager, useNanosForElapsedTime -
Constructor Summary
Constructors Constructor Description XProtocol(HostInfo hostInfo, PropertySet propertySet)XProtocol(java.lang.String host, int port, java.lang.String defaultSchema, PropertySet propertySet) -
Method Summary
Modifier and Type Method Description voidafterHandshake()voidbeforeHandshake()voidchangeDatabase(java.lang.String database)voidchangeUser(java.lang.String user, java.lang.String password, java.lang.String database)Re-authenticates as the given user and passwordXMessagecheckErrorMessage()Read one message from the MySQL server, checks for errors in it, and if none, returns the message, ready for readingvoidclose()voidconfigureTimeZone()voidconnect(java.lang.String user, java.lang.String password, java.lang.String database)Create a new session.voiddrainRows()Used only in testsbooleanfailedPreparingStatement(int preparedStatementId, XProtocolError e)Informs this protocol instance that preparing a statement on the connected server failed.voidfreePreparedStatementId(int preparedStatementId)Frees a prepared statement id so that it can be reused.ExceptionInterceptorgetExceptionInterceptor()java.io.InputStreamgetLocalInfileInputStream()Returns the InputStream instance that will be used to send data in response to a "LOAD DATA LOCAL INFILE" statement.intgetNewPreparedStatementId(PreparableStatement<?> preparableStatement)Returns an id to be used as a client-managed prepared statement id.java.lang.StringgetPasswordCharacterEncoding()java.lang.StringgetQueryComment()Returns the comment that will be prepended to all statements sent to the server.ServerSessiongetServerSession()booleanhasMoreResults()booleanhasResults()Used only in testsvoidinit(Session sess, SocketConnection socketConn, PropertySet propSet, TransactionEventHandler trManager)Init method takes the place of constructor.voidinitServerSession()booleanisOpen()booleanisSqlResultPending()voidnegotiateCompression()Negotiates compression capabilities with the server.voidnegotiateSSLConnection()protected voidnewCommand()Signal the intent to start processing a new command.<M extends Message, R extends QueryResult>
Rquery(M message, ResultBuilder<R> resultBuilder)<M extends Message, R extends QueryResult>
java.util.concurrent.CompletableFuture<R>queryAsync(M message, ResultBuilder<R> resultBuilder)<T extends ProtocolEntity>
Tread(java.lang.Class<Resultset> requiredClass, int maxRows, boolean streamResults, XMessage resultPacket, boolean isBinaryEncoded, ColumnDefinition metadata, ProtocolEntityFactory<T,XMessage> protocolEntityFactory)Read protocol entity.<T extends ProtocolEntity>
Tread(java.lang.Class<T> requiredClass, ProtocolEntityFactory<T,XMessage> protocolEntityFactory)byte[]readAuthenticateContinue()voidreadAuthenticateOk()XMessagereadMessage(XMessage reuse)Read one message from the MySQL server into the reusable buffer if provided or into the new one.ColumnDefinitionreadMetadata()ColumnDefinitionreadMetadata(Field f, java.util.function.Consumer<Notice> noticeConsumer)<T extends QueryResult>
TreadQueryResult(ResultBuilder<T> resultBuilder)Read messages from server and deliver them to resultBuilder.XProtocolRowreadRowOrNull(ColumnDefinition metadata, java.util.function.Consumer<Notice> noticeConsumer)ServerCapabilitiesreadServerCapabilities()Get the capabilities from the server.booleanreadyForPreparingStatements()Checks if enough statements have been executed in this MySQL server so that another prepare statement attempt should be done.voidreset()Return Protocol to its initial state right after successful connect.voidsend(Message message, int packetLen)voidsendCapabilities(java.util.Map<java.lang.String,java.lang.Object> keyValuePair)Set client capabilities of current session.XMessagesendCommand(Message queryPacket, boolean skipCheck, int timeoutMillis)Send a command to the MySQL server.voidsetLocalInfileInputStream(java.io.InputStream stream)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.voidsetMaxAllowedPacket(int maxAllowedPacket)voidsetQueryComment(java.lang.String comment)Sets the comment that will be prepended to all statements sent to the server.booleansupportsPreparedStatements()Checks if the MySQL server currently connected supports prepared statements.booleanversionMeetsMinimum(int major, int minor, int subminor)Methods inherited from class com.mysql.cj.protocol.AbstractProtocol
getAuthenticationProvider, getMessageBuilder, getPacketReceivedTimeHolder, getPacketSentTimeHolder, getPropertySet, getQueryTimingUnits, getSocketConnection, setPacketReceivedTimeHolder, setPacketSentTimeHolder, setPropertySetMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.mysql.cj.protocol.Protocol
getAuthenticationProvider, getMessageBuilder, getPacketReceivedTimeHolder, getPacketSentTimeHolder, getPropertySet, getQueryTimingUnits, getSocketConnection, setPacketReceivedTimeHolder, setPacketSentTimeHolder, setPropertySet
-
Field Details
-
defaultSchemaName
public java.lang.String defaultSchemaName -
COLLATION_NAME_TO_COLLATION_INDEX
public static java.util.Map<java.lang.String,java.lang.Integer> COLLATION_NAME_TO_COLLATION_INDEX
-
-
Constructor Details
-
XProtocol
public XProtocol(java.lang.String host, int port, java.lang.String defaultSchema, PropertySet propertySet) -
XProtocol
-
-
Method Details
-
init
public void init(Session sess, SocketConnection socketConn, PropertySet propSet, TransactionEventHandler trManager)Description copied from interface:ProtocolInit method takes the place of constructor. A constructor should be used unless the encapsulation of ProtocolFactory is necessary.- Specified by:
initin interfaceProtocol<XMessage>- Overrides:
initin classAbstractProtocol<XMessage>- Parameters:
sess-SessionsocketConn-SocketConnectionpropSet-PropertySettrManager-TransactionEventHandler
-
getServerSession
- Specified by:
getServerSessionin interfaceProtocol<XMessage>
-
sendCapabilities
public void sendCapabilities(java.util.Map<java.lang.String,java.lang.Object> keyValuePair)Set client capabilities of current session. Must be done before authentication (changeUser(String, String, String)).- Parameters:
keyValuePair- capabilities name/value map
-
negotiateSSLConnection
public void negotiateSSLConnection()- Specified by:
negotiateSSLConnectionin interfaceProtocol<XMessage>
-
negotiateCompression
public void negotiateCompression()Negotiates compression capabilities with the server. -
beforeHandshake
public void beforeHandshake()- Specified by:
beforeHandshakein interfaceProtocol<XMessage>
-
connect
public void connect(java.lang.String user, java.lang.String password, java.lang.String database)Description copied from interface:ProtocolCreate a new session. This generally happens once at the beginning of a connection. -
changeUser
public void changeUser(java.lang.String user, java.lang.String password, java.lang.String database)Description copied from interface:ProtocolRe-authenticates as the given user and password- Specified by:
changeUserin interfaceProtocol<XMessage>- Parameters:
user- DB user namepassword- DB user passworddatabase- database name
-
afterHandshake
public void afterHandshake()- Specified by:
afterHandshakein interfaceProtocol<XMessage>
-
configureTimeZone
public void configureTimeZone()- Specified by:
configureTimeZonein interfaceProtocol<XMessage>
-
initServerSession
public void initServerSession()- Specified by:
initServerSessionin interfaceProtocol<XMessage>
-
readAuthenticateOk
public void readAuthenticateOk() -
readAuthenticateContinue
public byte[] readAuthenticateContinue() -
hasMoreResults
public boolean hasMoreResults() -
readQueryResult
Description copied from interface:ProtocolRead messages from server and deliver them to resultBuilder.- Specified by:
readQueryResultin interfaceProtocol<XMessage>- Type Parameters:
T- result type- Parameters:
resultBuilder-ResultBuilderinstance- Returns:
QueryResult
-
hasResults
public boolean hasResults()Used only in tests- Returns:
- true if there are result rows
-
drainRows
public void drainRows()Used only in tests -
readMetadata
- Specified by:
readMetadatain interfaceProtocol<XMessage>
-
readMetadata
-
readRowOrNull
public XProtocolRow readRowOrNull(ColumnDefinition metadata, java.util.function.Consumer<Notice> noticeConsumer) -
supportsPreparedStatements
public boolean supportsPreparedStatements()Checks if the MySQL server currently connected supports prepared statements.- Returns:
trueif the MySQL server currently connected supports prepared statements.
-
readyForPreparingStatements
public boolean readyForPreparingStatements()Checks if enough statements have been executed in this MySQL server so that another prepare statement attempt should be done.- Returns:
trueif enough executions have been done since last time a prepared statement failed to prepare
-
getNewPreparedStatementId
Returns an id to be used as a client-managed prepared statement id. The methodfreePreparedStatementId(int)must be called when the prepared statement is deallocated so that the same id can be re-used.- Parameters:
preparableStatement-PreparableStatement- Returns:
- a new identifier to be used as prepared statement id
-
freePreparedStatementId
public void freePreparedStatementId(int preparedStatementId)Frees a prepared statement id so that it can be reused. Note that freeing an id from an active prepared statement will result in a statement prepare conflict next time one gets prepared with the same released id.- Parameters:
preparedStatementId- the prepared statement id to release
-
failedPreparingStatement
Informs this protocol instance that preparing a statement on the connected server failed.- Parameters:
preparedStatementId- the id of the prepared statement that failed to preparee-XProtocolError- Returns:
trueif the exception was properly handled
-
newCommand
protected void newCommand()Signal the intent to start processing a new command. A session supports processing a single command at a time. Results are read lazily from the wire. It is necessary to flush any pending result before starting a new command. This method performs the flush if necessary. -
query
public <M extends Message, R extends QueryResult> R query(M message, ResultBuilder<R> resultBuilder) -
queryAsync
public <M extends Message, R extends QueryResult> java.util.concurrent.CompletableFuture<R> queryAsync(M message, ResultBuilder<R> resultBuilder) -
isOpen
public boolean isOpen() -
close
public void close() throws java.io.IOException -
isSqlResultPending
public boolean isSqlResultPending() -
setMaxAllowedPacket
public void setMaxAllowedPacket(int maxAllowedPacket) -
send
-
readServerCapabilities
Get the capabilities from the server.NOTE: This must be called before authentication.
- Specified by:
readServerCapabilitiesin interfaceProtocol<XMessage>- Returns:
- capabilities mapped by name
-
reset
public void reset()Description copied from interface:ProtocolReturn Protocol to its initial state right after successful connect. -
getExceptionInterceptor
- Specified by:
getExceptionInterceptorin interfaceProtocol<XMessage>- Overrides:
getExceptionInterceptorin classAbstractProtocol<XMessage>
-
changeDatabase
public void changeDatabase(java.lang.String database)- Specified by:
changeDatabasein interfaceProtocol<XMessage>
-
getPasswordCharacterEncoding
public java.lang.String getPasswordCharacterEncoding()- Specified by:
getPasswordCharacterEncodingin interfaceProtocol<XMessage>
-
versionMeetsMinimum
public boolean versionMeetsMinimum(int major, int minor, int subminor)- Specified by:
versionMeetsMinimumin interfaceProtocol<XMessage>
-
readMessage
Description copied from interface:ProtocolRead one message from the MySQL server into the reusable buffer if provided or into the new one.- Specified by:
readMessagein interfaceProtocol<XMessage>- Parameters:
reuse-Messageinstance to read into, may be null- Returns:
- the message from the server.
-
checkErrorMessage
Description copied from interface:ProtocolRead one message from the MySQL server, checks for errors in it, and if none, returns the message, ready for reading- Specified by:
checkErrorMessagein interfaceProtocol<XMessage>- Returns:
- a message ready for reading.
-
sendCommand
Description copied from interface:ProtocolSend a command to the MySQL server.- Specified by:
sendCommandin interfaceProtocol<XMessage>- Parameters:
queryPacket- a packet pre-loaded with data for the protocol (eg. from a client-side prepared statement). The first byte of this packet is the MySQL protocol 'command' from MysqlDefsskipCheck- do not call checkErrorPacket() if truetimeoutMillis- timeout- Returns:
- the response packet from the server
-
read
public <T extends ProtocolEntity> T read(java.lang.Class<T> requiredClass, ProtocolEntityFactory<T,XMessage> protocolEntityFactory) throws java.io.IOException -
read
public <T extends ProtocolEntity> T read(java.lang.Class<Resultset> requiredClass, int maxRows, boolean streamResults, XMessage resultPacket, boolean isBinaryEncoded, ColumnDefinition metadata, ProtocolEntityFactory<T,XMessage> protocolEntityFactory) throws java.io.IOExceptionDescription copied from interface:ProtocolRead protocol entity.- Specified by:
readin interfaceProtocol<XMessage>- Type Parameters:
T- object extending theProtocolEntity- Parameters:
requiredClass- required Resultset classmaxRows- the maximum number of rows to read (-1 means all rows)streamResults- should the driver leave the results on the wire, and read them only when needed?resultPacket- the first packet of information in the result setisBinaryEncoded- true if the binary protocol is used (for server prepared statements)metadata- use this metadata instead of the one provided on wireprotocolEntityFactory-ProtocolEntityFactoryinstance- Returns:
ProtocolEntityinstance- Throws:
java.io.IOException- if an error occurs
-
setLocalInfileInputStream
public void setLocalInfileInputStream(java.io.InputStream stream)Description copied from interface:ProtocolSets 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:
setLocalInfileInputStreamin interfaceProtocol<XMessage>- Parameters:
stream- input stream
-
getLocalInfileInputStream
public java.io.InputStream getLocalInfileInputStream()Description copied from interface:ProtocolReturns 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:
getLocalInfileInputStreamin interfaceProtocol<XMessage>- Returns:
- input stream
-
getQueryComment
public java.lang.String getQueryComment()Description copied from interface:ProtocolReturns the comment that will be prepended to all statements sent to the server.- Specified by:
getQueryCommentin interfaceProtocol<XMessage>- Returns:
- query comment string
-
setQueryComment
public void setQueryComment(java.lang.String comment)Description copied from interface:ProtocolSets the comment that will be prepended to all statements sent to the server. Do not use slash-star or star-slash tokens in the comment as these will be added by the driver itself.- Specified by:
setQueryCommentin interfaceProtocol<XMessage>- Parameters:
comment- query comment string
-