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_INDEX
java.lang.String
defaultSchemaName
Fields 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 void
afterHandshake()
void
beforeHandshake()
void
changeDatabase(java.lang.String database)
void
changeUser(java.lang.String user, java.lang.String password, java.lang.String database)
Re-authenticates as the given user and passwordXMessage
checkErrorMessage()
Read one message from the MySQL server, checks for errors in it, and if none, returns the message, ready for readingvoid
close()
void
configureTimeZone()
void
connect(java.lang.String user, java.lang.String password, java.lang.String database)
Create a new session.void
drainRows()
Used only in testsboolean
failedPreparingStatement(int preparedStatementId, XProtocolError e)
Informs this protocol instance that preparing a statement on the connected server failed.void
freePreparedStatementId(int preparedStatementId)
Frees a prepared statement id so that it can be reused.ExceptionInterceptor
getExceptionInterceptor()
java.io.InputStream
getLocalInfileInputStream()
Returns the InputStream instance that will be used to send data in response to a "LOAD DATA LOCAL INFILE" statement.int
getNewPreparedStatementId(PreparableStatement<?> preparableStatement)
Returns an id to be used as a client-managed prepared statement id.java.lang.String
getPasswordCharacterEncoding()
java.lang.String
getQueryComment()
Returns the comment that will be prepended to all statements sent to the server.ServerSession
getServerSession()
boolean
hasMoreResults()
boolean
hasResults()
Used only in testsvoid
init(Session sess, SocketConnection socketConn, PropertySet propSet, TransactionEventHandler trManager)
Init method takes the place of constructor.void
initServerSession()
boolean
isOpen()
boolean
isSqlResultPending()
void
negotiateCompression()
Negotiates compression capabilities with the server.void
negotiateSSLConnection()
protected void
newCommand()
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()
void
readAuthenticateOk()
XMessage
readMessage(XMessage reuse)
Read one message from the MySQL server into the reusable buffer if provided or into the new one.ColumnDefinition
readMetadata()
ColumnDefinition
readMetadata(Field f, java.util.function.Consumer<Notice> noticeConsumer)
<T extends QueryResult>
TreadQueryResult(ResultBuilder<T> resultBuilder)
Read messages from server and deliver them to resultBuilder.XProtocolRow
readRowOrNull(ColumnDefinition metadata, java.util.function.Consumer<Notice> noticeConsumer)
ServerCapabilities
readServerCapabilities()
Get the capabilities from the server.boolean
readyForPreparingStatements()
Checks if enough statements have been executed in this MySQL server so that another prepare statement attempt should be done.void
reset()
Return Protocol to its initial state right after successful connect.void
send(Message message, int packetLen)
void
sendCapabilities(java.util.Map<java.lang.String,java.lang.Object> keyValuePair)
Set client capabilities of current session.XMessage
sendCommand(Message queryPacket, boolean skipCheck, int timeoutMillis)
Send a command to the MySQL server.void
setLocalInfileInputStream(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.void
setMaxAllowedPacket(int maxAllowedPacket)
void
setQueryComment(java.lang.String comment)
Sets the comment that will be prepended to all statements sent to the server.boolean
supportsPreparedStatements()
Checks if the MySQL server currently connected supports prepared statements.boolean
versionMeetsMinimum(int major, int minor, int subminor)
Methods inherited from class com.mysql.cj.protocol.AbstractProtocol
getAuthenticationProvider, getMessageBuilder, getPacketReceivedTimeHolder, getPacketSentTimeHolder, getPropertySet, getQueryTimingUnits, getSocketConnection, setPacketReceivedTimeHolder, setPacketSentTimeHolder, setPropertySet
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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:Protocol
Init method takes the place of constructor. A constructor should be used unless the encapsulation of ProtocolFactory is necessary.- Specified by:
init
in interfaceProtocol<XMessage>
- Overrides:
init
in classAbstractProtocol<XMessage>
- Parameters:
sess
-Session
socketConn
-SocketConnection
propSet
-PropertySet
trManager
-TransactionEventHandler
-
getServerSession
- Specified by:
getServerSession
in 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:
negotiateSSLConnection
in interfaceProtocol<XMessage>
-
negotiateCompression
public void negotiateCompression()Negotiates compression capabilities with the server. -
beforeHandshake
public void beforeHandshake()- Specified by:
beforeHandshake
in interfaceProtocol<XMessage>
-
connect
public void connect(java.lang.String user, java.lang.String password, java.lang.String database)Description copied from interface:Protocol
Create 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:Protocol
Re-authenticates as the given user and password- Specified by:
changeUser
in interfaceProtocol<XMessage>
- Parameters:
user
- DB user namepassword
- DB user passworddatabase
- database name
-
afterHandshake
public void afterHandshake()- Specified by:
afterHandshake
in interfaceProtocol<XMessage>
-
configureTimeZone
public void configureTimeZone()- Specified by:
configureTimeZone
in interfaceProtocol<XMessage>
-
initServerSession
public void initServerSession()- Specified by:
initServerSession
in interfaceProtocol<XMessage>
-
readAuthenticateOk
public void readAuthenticateOk() -
readAuthenticateContinue
public byte[] readAuthenticateContinue() -
hasMoreResults
public boolean hasMoreResults() -
readQueryResult
Description copied from interface:Protocol
Read messages from server and deliver them to resultBuilder.- Specified by:
readQueryResult
in interfaceProtocol<XMessage>
- Type Parameters:
T
- result type- Parameters:
resultBuilder
-ResultBuilder
instance- 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:
readMetadata
in 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:
true
if 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:
true
if 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:
true
if 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:
readServerCapabilities
in interfaceProtocol<XMessage>
- Returns:
- capabilities mapped by name
-
reset
public void reset()Description copied from interface:Protocol
Return Protocol to its initial state right after successful connect. -
getExceptionInterceptor
- Specified by:
getExceptionInterceptor
in interfaceProtocol<XMessage>
- Overrides:
getExceptionInterceptor
in classAbstractProtocol<XMessage>
-
changeDatabase
public void changeDatabase(java.lang.String database)- Specified by:
changeDatabase
in interfaceProtocol<XMessage>
-
getPasswordCharacterEncoding
public java.lang.String getPasswordCharacterEncoding()- Specified by:
getPasswordCharacterEncoding
in interfaceProtocol<XMessage>
-
versionMeetsMinimum
public boolean versionMeetsMinimum(int major, int minor, int subminor)- Specified by:
versionMeetsMinimum
in interfaceProtocol<XMessage>
-
readMessage
Description copied from interface:Protocol
Read one message from the MySQL server into the reusable buffer if provided or into the new one.- Specified by:
readMessage
in interfaceProtocol<XMessage>
- Parameters:
reuse
-Message
instance to read into, may be null- Returns:
- the message from the server.
-
checkErrorMessage
Description copied from interface:Protocol
Read one message from the MySQL server, checks for errors in it, and if none, returns the message, ready for reading- Specified by:
checkErrorMessage
in interfaceProtocol<XMessage>
- Returns:
- a message ready for reading.
-
sendCommand
Description copied from interface:Protocol
Send a command to the MySQL server.- Specified by:
sendCommand
in 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:Protocol
Read protocol entity.- Specified by:
read
in 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
-ProtocolEntityFactory
instance- Returns:
ProtocolEntity
instance- Throws:
java.io.IOException
- if an error occurs
-
setLocalInfileInputStream
public void setLocalInfileInputStream(java.io.InputStream stream)Description copied from interface:Protocol
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 interfaceProtocol<XMessage>
- Parameters:
stream
- input stream
-
getLocalInfileInputStream
public java.io.InputStream getLocalInfileInputStream()Description copied from interface:Protocol
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 interfaceProtocol<XMessage>
- Returns:
- input stream
-
getQueryComment
public java.lang.String getQueryComment()Description copied from interface:Protocol
Returns the comment that will be prepended to all statements sent to the server.- Specified by:
getQueryComment
in interfaceProtocol<XMessage>
- Returns:
- query comment string
-
setQueryComment
public void setQueryComment(java.lang.String comment)Description copied from interface:Protocol
Sets 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:
setQueryComment
in interfaceProtocol<XMessage>
- Parameters:
comment
- query comment string
-