Skip to content

Commit 0b4b87a

Browse files
authored
MySQL simplify connection ctx access (#602)
* add assumed mysql capabilties flags Signed-off-by: Billy Yuan <billy112487983@gmail.com> * revert to move sequenceId back to commandCodec instead of MySQLEncoder Signed-off-by: Billy Yuan <billy112487983@gmail.com> * minors Signed-off-by: Billy Yuan <billy112487983@gmail.com> * minors Signed-off-by: Billy Yuan <billy112487983@gmail.com> * simplify mysql collation logic Signed-off-by: Billy Yuan <billy112487983@gmail.com>
1 parent ebab50a commit 0b4b87a

28 files changed

+75
-74
lines changed

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLCollation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ public enum MySQLCollation {
310310
}
311311
}
312312

313+
public static final MySQLCollation DEFAULT_COLLATION = utf8mb4_general_ci;
314+
313315
private final String mysqlCharsetName;
314316
private final String mappedJavaCharsetName;
315317
private final int collationId;

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLConnectionFactory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MySQLConnectionFactory implements ConnectionFactory {
3939
private final String password;
4040
private final String database;
4141
private final Map<String, String> connectionAttributes;
42-
private final String collation;
42+
private final MySQLCollation collation;
4343
private final Charset charsetEncoding;
4444
private final boolean useAffectedRows;
4545
private final SslMode sslMode;
@@ -59,15 +59,18 @@ public MySQLConnectionFactory(Vertx vertx, ContextInternal context, MySQLConnect
5959
this.password = options.getPassword();
6060
this.database = options.getDatabase();
6161
this.connectionAttributes = options.getProperties() == null ? null : Collections.unmodifiableMap(options.getProperties());
62-
String collation;
62+
MySQLCollation collation;
6363
if (options.getCollation() != null) {
6464
// override the collation if configured
65-
collation = options.getCollation();
66-
MySQLCollation mySQLCollation = MySQLCollation.valueOfName(collation);
67-
charsetEncoding = Charset.forName(mySQLCollation.mappedJavaCharsetName());
65+
collation = MySQLCollation.valueOfName(options.getCollation());
66+
charsetEncoding = Charset.forName(collation.mappedJavaCharsetName());
6867
} else {
6968
String charset = options.getCharset();
70-
collation = MySQLCollation.getDefaultCollationFromCharsetName(charset);
69+
if (charset == null) {
70+
collation = MySQLCollation.DEFAULT_COLLATION;
71+
} else {
72+
collation = MySQLCollation.valueOfName(MySQLCollation.getDefaultCollationFromCharsetName(charset));
73+
}
7174
String characterEncoding = options.getCharacterEncoding();
7275
if (characterEncoding == null) {
7376
charsetEncoding = Charset.defaultCharset();

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLConnectionImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,17 @@ public MySQLConnection changeUser(MySQLAuthOptions options, Handler<AsyncResult<
171171

172172
@Override
173173
public Future<Void> changeUser(MySQLAuthOptions options) {
174-
String collation;
174+
MySQLCollation collation;
175175
if (options.getCollation() != null) {
176176
// override the collation if configured
177-
collation = options.getCollation();
177+
collation = MySQLCollation.valueOfName(options.getCollation());
178178
} else {
179179
String charset = options.getCharset();
180-
collation = MySQLCollation.getDefaultCollationFromCharsetName(charset);
180+
if (charset == null) {
181+
collation = MySQLCollation.DEFAULT_COLLATION;
182+
} else {
183+
collation = MySQLCollation.valueOfName(MySQLCollation.getDefaultCollationFromCharsetName(charset));
184+
}
181185
}
182186
Buffer serverRsaPublicKey = null;
183187
if (options.getServerRsaPublicKeyValue() != null) {

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLSocketConnection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public MySQLSocketConnection(NetSocketInternal socket,
5454
void sendStartupMessage(String username,
5555
String password,
5656
String database,
57-
String collation,
57+
MySQLCollation collation,
5858
Buffer serverRsaPublicKey,
5959
Map<String, String> properties,
6060
SslMode sslMode,
@@ -88,4 +88,8 @@ protected <R> void doSchedule(CommandBase<R> cmd, Handler<AsyncResult<R>> handle
8888
super.doSchedule(cmd, handler);
8989
}
9090
}
91+
92+
public void upgradeToSsl(Handler<AsyncResult<Void>> completionHandler) {
93+
socket.upgradeToSsl(completionHandler);
94+
}
9195
}

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/AuthenticationCommandBaseCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected final void handleAuthMoreData(byte[] password, ByteBuf payload) {
5252
int nonScrambledPasswordPacketLength = password.length + 1;
5353
ByteBuf nonScrambledPasswordPacket = allocateBuffer(nonScrambledPasswordPacketLength + 4);
5454
nonScrambledPasswordPacket.writeMediumLE(nonScrambledPasswordPacketLength);
55-
nonScrambledPasswordPacket.writeByte(encoder.sequenceId);
55+
nonScrambledPasswordPacket.writeByte(sequenceId);
5656
nonScrambledPasswordPacket.writeBytes(password);
5757
nonScrambledPasswordPacket.writeByte(0x00); // end with a 0x00
5858
sendNonSplitPacket(nonScrambledPasswordPacket);
@@ -64,7 +64,7 @@ protected final void handleAuthMoreData(byte[] password, ByteBuf payload) {
6464
isWaitingForRsaPublicKey = true;
6565
ByteBuf rsaPublicKeyRequest = allocateBuffer(5);
6666
rsaPublicKeyRequest.writeMediumLE(1);
67-
rsaPublicKeyRequest.writeByte(encoder.sequenceId);
67+
rsaPublicKeyRequest.writeByte(sequenceId);
6868
rsaPublicKeyRequest.writeByte(AUTH_PUBLIC_KEY_REQUEST_FLAG);
6969
sendNonSplitPacket(rsaPublicKeyRequest);
7070
} else {
@@ -93,7 +93,7 @@ protected final void sendEncryptedPasswordWithServerRsaPublicKey(byte[] password
9393
}
9494

9595
protected final void encodeConnectionAttributes(Map<String, String> clientConnectionAttributes, ByteBuf packet) {
96-
ByteBuf kv = encoder.chctx.alloc().ioBuffer();
96+
ByteBuf kv = allocateBuffer();
9797
for (Map.Entry<String, String> attribute : clientConnectionAttributes.entrySet()) {
9898
BufferUtils.writeLengthEncodedString(kv, attribute.getKey(), StandardCharsets.UTF_8);
9999
BufferUtils.writeLengthEncodedString(kv, attribute.getValue(), StandardCharsets.UTF_8);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ChangeUserCommandCodec.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.vertx.mysqlclient.impl.util.Native41Authenticator;
2121
import io.vertx.sqlclient.impl.command.CommandResponse;
2222

23-
import java.nio.charset.Charset;
2423
import java.nio.charset.StandardCharsets;
2524
import java.util.Map;
2625

@@ -83,7 +82,7 @@ private void sendChangeUserCommand() {
8382
// encode packet header
8483
int packetStartIdx = packet.writerIndex();
8584
packet.writeMediumLE(0); // will set payload length later by calculation
86-
packet.writeByte(encoder.sequenceId);
85+
packet.writeByte(sequenceId);
8786

8887
// encode packet payload
8988
packet.writeByte(CommandType.COM_CHANGE_USER);
@@ -96,9 +95,8 @@ private void sendChangeUserCommand() {
9695
packet.writeCharSequence(password, StandardCharsets.UTF_8);
9796
}
9897
BufferUtils.writeNullTerminatedString(packet, cmd.database(), StandardCharsets.UTF_8);
99-
MySQLCollation collation = MySQLCollation.valueOfName(cmd.collation());
98+
MySQLCollation collation = cmd.collation();
10099
int collationId = collation.collationId();
101-
encoder.charset = Charset.forName(collation.mappedJavaCharsetName());
102100
packet.writeShortLE(collationId);
103101

104102
if ((encoder.clientCapabilitiesFlag & CLIENT_PLUGIN_AUTH) != 0) {

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/CloseConnectionCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void sendQuitCommand() {
3737
ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
3838
// encode packet header
3939
packet.writeMediumLE(PAYLOAD_LENGTH);
40-
packet.writeByte(encoder.sequenceId);
40+
packet.writeByte(sequenceId);
4141

4242
// encode packet payload
4343
packet.writeByte(CommandType.COM_QUIT);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/CloseStatementCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private void sendCloseStatementCommand(MySQLPreparedStatement statement) {
4141
ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
4242
// encode packet header
4343
packet.writeMediumLE(PAYLOAD_LENGTH);
44-
packet.writeByte(encoder.sequenceId);
44+
packet.writeByte(sequenceId);
4545

4646
// encode packet payload
4747
packet.writeByte(CommandType.COM_STMT_CLOSE);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/CommandCodec.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ abstract class CommandCodec<R, C extends CommandBase<R>> {
3838
public R result;
3939
final C cmd;
4040
MySQLEncoder encoder;
41+
int sequenceId;
4142

4243
CommandCodec(C cmd) {
4344
this.cmd = cmd;
@@ -47,7 +48,7 @@ abstract class CommandCodec<R, C extends CommandBase<R>> {
4748

4849
void encode(MySQLEncoder encoder) {
4950
this.encoder = encoder;
50-
this.encoder.sequenceId = 0;
51+
this.sequenceId = 0;
5152
}
5253

5354
ByteBuf allocateBuffer() {
@@ -76,21 +77,21 @@ private void sendSplitPacket(ByteBuf packet) {
7677
// send a packet with 0xFFFFFF length payload
7778
ByteBuf packetHeader = allocateBuffer(4);
7879
packetHeader.writeMediumLE(PACKET_PAYLOAD_LENGTH_LIMIT);
79-
packetHeader.writeByte(encoder.sequenceId++);
80+
packetHeader.writeByte(sequenceId++);
8081
encoder.chctx.write(packetHeader);
8182
encoder.chctx.write(payload.readRetainedSlice(PACKET_PAYLOAD_LENGTH_LIMIT));
8283
}
8384

8485
// send a packet with last part of the payload
8586
ByteBuf packetHeader = allocateBuffer(4);
8687
packetHeader.writeMediumLE(payload.readableBytes());
87-
packetHeader.writeByte(encoder.sequenceId++);
88+
packetHeader.writeByte(sequenceId++);
8889
encoder.chctx.write(packetHeader);
8990
encoder.chctx.writeAndFlush(payload);
9091
}
9192

9293
void sendNonSplitPacket(ByteBuf packet) {
93-
encoder.sequenceId++;
94+
sequenceId++;
9495
encoder.chctx.writeAndFlush(packet);
9596
}
9697

@@ -99,7 +100,7 @@ final void sendBytesAsPacket(byte[] payload) {
99100
ByteBuf packet = allocateBuffer(payloadLength + 4);
100101
// encode packet header
101102
packet.writeMediumLE(payloadLength);
102-
packet.writeByte(encoder.sequenceId);
103+
packet.writeByte(sequenceId);
103104

104105
// encode packet payload
105106
packet.writeBytes(payload);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/DebugCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void sendDebugCommand() {
3737
ByteBuf packet = allocateBuffer(PAYLOAD_LENGTH + 4);
3838
// encode packet header
3939
packet.writeMediumLE(PAYLOAD_LENGTH);
40-
packet.writeByte(encoder.sequenceId);
40+
packet.writeByte(sequenceId);
4141

4242
// encode packet payload
4343
packet.writeByte(CommandType.COM_DEBUG);

0 commit comments

Comments
 (0)