From 1bad41b4b0e21c5380bdb7ec1742b81f0aa7e45a Mon Sep 17 00:00:00 2001 From: Lukasz Sanek Date: Thu, 29 Apr 2021 11:58:16 +0100 Subject: [PATCH] Fix for 95564 Fix for 95564 --- .../mysql/cj/protocol/a/NativeProtocol.java | 2 +- .../regression/ConnectionRegressionTest.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/protocol-impl/java/com/mysql/cj/protocol/a/NativeProtocol.java b/src/main/protocol-impl/java/com/mysql/cj/protocol/a/NativeProtocol.java index a6d0d978d..9e13b98c2 100644 --- a/src/main/protocol-impl/java/com/mysql/cj/protocol/a/NativeProtocol.java +++ b/src/main/protocol-impl/java/com/mysql/cj/protocol/a/NativeProtocol.java @@ -524,7 +524,7 @@ public void changeDatabase(String database) { sendCommand(this.commandBuilder.buildComInitDb(getSharedSendPacket(), database), false, 0); } catch (CJException ex) { if (this.getPropertySet().getBooleanProperty(PropertyKey.createDatabaseIfNotExist).getValue()) { - sendCommand(this.commandBuilder.buildComQuery(getSharedSendPacket(), "CREATE DATABASE IF NOT EXISTS " + database), false, 0); + sendCommand(this.commandBuilder.buildComQuery(getSharedSendPacket(), "CREATE DATABASE IF NOT EXISTS " + StringUtils.quoteIdentifier(database, true)), false, 0); sendCommand(this.commandBuilder.buildComInitDb(getSharedSendPacket(), database), false, 0); } else { diff --git a/src/test/java/testsuite/regression/ConnectionRegressionTest.java b/src/test/java/testsuite/regression/ConnectionRegressionTest.java index dac614df2..a9370d30a 100644 --- a/src/test/java/testsuite/regression/ConnectionRegressionTest.java +++ b/src/test/java/testsuite/regression/ConnectionRegressionTest.java @@ -11852,4 +11852,31 @@ public void testBug102188() throws Exception { getConnectionWithProps("defaultAuthenticationPlugin=authentication_ldap_sasl_client").close(); assertNotNull(Security.getProvider("MySQLScramShaSasl")); } + + /** + * Tests fix for Bug#95564, createDatabaseIfNotExist is not working for databases with hyphen in name. + * + * @throws Exception + */ + @Test + public void testBug95564() throws Exception { + String databaseName = "test-bug95564"; + + this.stmt.executeUpdate("DROP DATABASE IF EXISTS " + StringUtils.quoteIdentifier(databaseName, true)); + + Properties props = getPropertiesFromTestsuiteUrl(); + props.setProperty(PropertyKey.createDatabaseIfNotExist.getKeyName(), "true"); + props.setProperty(PropertyKey.DBNAME.getKeyName(), databaseName); + + Connection con = getConnectionWithProps(props); + + this.rs = this.stmt.executeQuery("SHOW DATABASES LIKE '" + databaseName + "'"); + if (this.rs.next()) { + assertEquals(databaseName, this.rs.getString(1)); + } else { + fail("Database " + databaseName + " is not found."); + } + + con.close(); + } }