Skip to content

Commit bad574b

Browse files
committed
Merge branch 'master' of github.com:eclipse-vertx/vertx-sql-client into 490-connection-error-path-handling
Signed-off-by: Greg Watts <gwatts@us.ibm.com>
2 parents c62acc5 + 0b4b87a commit bad574b

30 files changed

+83
-105
lines changed

vertx-db2-client/src/main/java/io/vertx/db2client/DB2ConnectOptions.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public static DB2ConnectOptions fromUri(String connectionUri) throws IllegalArgu
4848

4949
public static final String DEFAULT_HOST = "localhost";
5050
public static final int DEFAULT_PORT = 50000;
51-
public static final String DEFAULT_USER = "root";
52-
public static final String DEFAULT_PASSWORD = "INVALID";
53-
public static final String DEFAULT_SCHEMA = "INVALID";
5451
public static final String DEFAULT_CHARSET = "utf8";
5552
public static final boolean DEFAULT_USE_AFFECTED_ROWS = false;
5653
public static final int DEFAULT_PIPELINING_LIMIT = 1; //256; // TODO default to 256 once implemented properly
@@ -95,7 +92,7 @@ public DB2ConnectOptions setPort(int port) {
9592

9693
@Override
9794
public DB2ConnectOptions setUser(String user) {
98-
if (user == null || user.trim().length() < 1) {
95+
if (user == null || user.length() < 1) {
9996
throw new DB2Exception("The user cannot be blank or null", SqlCode.MISSING_CREDENTIALS, SQLState.CONNECT_USERID_ISNULL);
10097
} else {
10198
return (DB2ConnectOptions) super.setUser(user);
@@ -104,7 +101,7 @@ public DB2ConnectOptions setUser(String user) {
104101

105102
@Override
106103
public DB2ConnectOptions setPassword(String password) {
107-
if (password == null || password.trim().length() < 1) {
104+
if (password == null || password.length() < 1) {
108105
throw new DB2Exception("The password cannot be blank or null", SqlCode.MISSING_CREDENTIALS, SQLState.CONNECT_PASSWORD_ISNULL);
109106
} else {
110107
return (DB2ConnectOptions) super.setPassword(password);
@@ -113,7 +110,7 @@ public DB2ConnectOptions setPassword(String password) {
113110

114111
@Override
115112
public DB2ConnectOptions setDatabase(String database) {
116-
if (database == null || database.trim().length() < 1) {
113+
if (database == null || database.length() < 1) {
117114
throw new DB2Exception("The database name cannot be blank or null", SqlCode.DATABASE_NOT_FOUND, SQLState.DATABASE_NOT_FOUND);
118115
} else {
119116
return (DB2ConnectOptions) super.setDatabase(database);
@@ -173,9 +170,6 @@ public DB2ConnectOptions addProperty(String key, String value) {
173170
protected void init() {
174171
this.setHost(DEFAULT_HOST);
175172
this.setPort(DEFAULT_PORT);
176-
this.setUser(DEFAULT_USER);
177-
this.setPassword(DEFAULT_PASSWORD);
178-
this.setDatabase(DEFAULT_SCHEMA);
179173
this.setPipeliningLimit(DEFAULT_PIPELINING_LIMIT);
180174
this.setProperties(new HashMap<>(DEFAULT_CONNECTION_ATTRIBUTES));
181175
}

vertx-db2-client/src/test/java/io/vertx/db2client/DB2ErrorMessageTest.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ public void testConnectInvalidDatabase(TestContext ctx) {
3838
"The SQL error message returned is not correct. It should have contained \"provided was not found\" or \"The connection was closed by the database server\", but instead it said \"" + ex.getMessage() + "\"");
3939
ctx.assertTrue(ex.getErrorCode() == SqlCode.DATABASE_NOT_FOUND ||
4040
ex.getErrorCode() == SqlCode.CONNECTION_REFUSED,
41-
"Wrong SQL code received");
41+
"Wrong SQL code received. Expecting " + SqlCode.DATABASE_NOT_FOUND + " or " + SqlCode.CONNECTION_REFUSED + ", but received " + ex.getErrorCode());
4242
ctx.assertTrue(ex.getSqlState().equalsIgnoreCase("2E000") ||
4343
ex.getSqlState() == SQLState.AUTH_DATABASE_CONNECTION_REFUSED,
44-
"Wrong SQL state received");
45-
// System.out.println(ex.getMessage());
44+
"Wrong SQL state received. Expecting 2E000 or " + SQLState.AUTH_DATABASE_CONNECTION_REFUSED + ", but received " + ex.getSqlState());
4645
}));
4746
}
4847

@@ -55,7 +54,6 @@ public void testConnectInvalidUsername(TestContext ctx) {
5554
ctx.assertTrue(ex.getMessage().contains("Invalid credentials"), "The SQL error message returned is not correct. It should have contained \"Invalid credentials\", but instead it said \"" + ex.getMessage() + "\"");
5655
ctx.assertEquals(SqlCode.INVALID_CREDENTIALS, ex.getErrorCode());
5756
ctx.assertEquals(SQLState.NET_CONNECT_AUTH_FAILED, ex.getSqlState());
58-
// System.out.println(ex.getMessage());
5957
}));
6058
}
6159

@@ -68,23 +66,18 @@ public void testConnectInvalidPassword(TestContext ctx) {
6866
ctx.assertTrue(ex.getMessage().contains("Invalid credentials"), "The SQL error message returned is not correct. It should have contained \"Invalid credentials\", but instead it said \"" + ex.getMessage() + "\"");
6967
ctx.assertEquals(SqlCode.INVALID_CREDENTIALS, ex.getErrorCode());
7068
ctx.assertEquals(SQLState.NET_CONNECT_AUTH_FAILED, ex.getSqlState());
71-
// System.out.println(ex.getMessage());
7269
}));
7370
}
7471

7572
@Test
76-
@Ignore
7773
public void testQueryBlankDatabase(TestContext ctx) {
7874
try {
7975
options.setDatabase("");
8076
ctx.fail("Expected a DB2Exception to be thrown");
81-
} catch (Exception e) {
82-
ctx.assertTrue(e instanceof DB2Exception, "Expected a DB2Exception to be thrown");
83-
DB2Exception ex = (DB2Exception) e;
77+
} catch (DB2Exception ex) {
8478
ctx.assertTrue(ex.getMessage().contains("The database name cannot be blank or null"), "The SQL error message returned is not correct. It should have contained \"The database name cannot be blank or null\", but instead it said \"" + ex.getMessage() + "\"");
8579
ctx.assertEquals(SqlCode.DATABASE_NOT_FOUND, ex.getErrorCode());
8680
ctx.assertEquals(SQLState.DATABASE_NOT_FOUND, ex.getSqlState());
87-
// System.out.println(ex.getMessage());
8881
}
8982
}
9083

@@ -93,13 +86,10 @@ public void testQueryBlankUsername(TestContext ctx) {
9386
try {
9487
options.setUser("");
9588
ctx.fail("Expected a DB2Exception to be thrown");
96-
} catch (Exception e) {
97-
ctx.assertTrue(e instanceof DB2Exception, "Expected a DB2Exception to be thrown");
98-
DB2Exception ex = (DB2Exception) e;
89+
} catch (DB2Exception ex) {
9990
ctx.assertTrue(ex.getMessage().contains("The user cannot be blank or null"), "The SQL error message returned is not correct. It should have contained \"The user cannot be blank or null\", but instead it said \"" + ex.getMessage() + "\"");
10091
ctx.assertEquals(SqlCode.MISSING_CREDENTIALS, ex.getErrorCode());
10192
ctx.assertEquals(SQLState.CONNECT_USERID_ISNULL, ex.getSqlState());
102-
// System.out.println(ex.getMessage());
10393
}
10494
}
10595

@@ -108,13 +98,10 @@ public void testQueryBlankPassword(TestContext ctx) {
10898
try {
10999
options.setPassword("");
110100
ctx.fail("Expected a DB2Exception to be thrown");
111-
} catch (Exception e) {
112-
ctx.assertTrue(e instanceof DB2Exception, "Expected a DB2Exception to be thrown");
113-
DB2Exception ex = (DB2Exception) e;
101+
} catch (DB2Exception ex) {
114102
ctx.assertTrue(ex.getMessage().contains("The password cannot be blank or null"), "The SQL error message returned is not correct. It should have contained \"The password cannot be blank or null\", but instead it said \"" + ex.getMessage() + "\"");
115103
ctx.assertEquals(SqlCode.MISSING_CREDENTIALS, ex.getErrorCode());
116104
ctx.assertEquals(SQLState.CONNECT_PASSWORD_ISNULL, ex.getSqlState());
117-
// System.out.println(ex.getMessage());
118105
}
119106
}
120107

@@ -128,7 +115,6 @@ public void testQueryBlankTable(TestContext ctx) {
128115
ctx.assertTrue(ex.getMessage().contains("The SQL syntax provided was invalid"), "The SQL error message returned is not correct. It should have contained \"The SQL syntax provided was invalid\", but instead it said \"" + ex.getMessage() + "\"");
129116
ctx.assertEquals(SqlCode.INVALID_SQL_STATEMENT, ex.getErrorCode());
130117
ctx.assertEquals("42601", ex.getSqlState());
131-
// System.out.println(ex.getMessage());
132118
}));
133119
}));
134120
}
@@ -143,7 +129,6 @@ public void testInvalidTableQuery(TestContext ctx) {
143129
ctx.assertTrue(ex.getMessage().contains("provided is not defined"), "The SQL error message returned is not correct. It should have contained \"provided is not defined\", but instead it said \"" + ex.getMessage() + "\"");
144130
ctx.assertEquals(SqlCode.OBJECT_NOT_DEFINED, ex.getErrorCode());
145131
ctx.assertEquals("42704", ex.getSqlState());
146-
// System.out.println(ex.getMessage());
147132
}));
148133
}));
149134
}
@@ -158,7 +143,6 @@ public void testInvalidColumnQuery(TestContext ctx) {
158143
ctx.assertTrue(ex.getMessage().contains("provided does not exist"), "The SQL error message returned is not correct. It should have contained \"provided does not exist\", but instead it said \"" + ex.getMessage() + "\"");
159144
ctx.assertEquals(SqlCode.COLUMN_DOES_NOT_EXIST, ex.getErrorCode());
160145
ctx.assertEquals("42703", ex.getSqlState());
161-
// System.out.println(ex.getMessage());
162146
}));
163147
}));
164148
}
@@ -173,7 +157,6 @@ public void testInvalidQuery(TestContext ctx) {
173157
ctx.assertTrue(ex.getMessage().contains("The SQL syntax provided was invalid"), "The SQL error message returned is not correct. It should have contained \"The SQL syntax provided was invalid\", but instead it said \"" + ex.getMessage() + "\"");
174158
ctx.assertEquals(SqlCode.INVALID_SQL_STATEMENT, ex.getErrorCode());
175159
ctx.assertEquals("42601", ex.getSqlState());
176-
// System.out.println(ex.getMessage());
177160
}));
178161
}));
179162
}

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);

0 commit comments

Comments
 (0)