diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java index 08a425ddda..84fd8c273a 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java @@ -17,6 +17,8 @@ import java.util.Calendar; import java.util.Date; import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.hibernate.Length; import org.hibernate.LockOptions; import org.hibernate.PessimisticLockException; @@ -210,12 +212,12 @@ public class MySQLDialect extends Dialect { protected static DatabaseVersion createVersion(DialectResolutionInfo info, DatabaseVersion defaultVersion) { final String versionString = info.getDatabaseVersion(); if ( versionString != null ) { - final String[] components = versionString.split( "\\." ); - if ( components.length >= 3 ) { + final Matcher matcher = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+).*").matcher(versionString); + if ( matcher.matches() && matcher.groupCount() >= 3 ) { try { - final int majorVersion = Integer.parseInt( components[0] ); - final int minorVersion = Integer.parseInt( components[1] ); - final int patchLevel = Integer.parseInt( components[2] ); + final int majorVersion = Integer.parseInt( matcher.group(1)); + final int minorVersion = Integer.parseInt( matcher.group(2) ); + final int patchLevel = Integer.parseInt( matcher.group(3) ); return DatabaseVersion.make( majorVersion, minorVersion, patchLevel ); } catch (NumberFormatException ex) { diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/MySQLDialectDatabaseVersionTest.java b/hibernate-core/src/test/java/org/hibernate/dialect/MySQLDialectDatabaseVersionTest.java new file mode 100644 index 0000000000..466180eac6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/dialect/MySQLDialectDatabaseVersionTest.java @@ -0,0 +1,97 @@ +package org.hibernate.dialect; + +import java.util.Map; +import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + + +import static org.junit.Assert.assertEquals; + +@RequiresDialect(MySQLDialect.class) +@TestForIssue(jiraKey = "HHH-18518") +public class MySQLDialectDatabaseVersionTest extends BaseUnitTestCase { + + @Test + public void versionWithSuffix() { + String version = "8.0.37-azure"; + Dialect dialect = new MySQLDialect( new TestingMySQLDialectResolutionInfo( version ) ); + + assertEquals(8, dialect.getVersion().getMajor()); + assertEquals(0, dialect.getVersion().getMinor()); + assertEquals(37, dialect.getVersion().getMicro()); + } + + @Test + public void releaseVersion() { + String version = "8.0.37"; + Dialect dialect = new MySQLDialect( new TestingMySQLDialectResolutionInfo( version ) ); + + assertEquals(8, dialect.getVersion().getMajor()); + assertEquals(0, dialect.getVersion().getMinor()); + assertEquals(37, dialect.getVersion().getMicro()); + } + + static final class TestingMySQLDialectResolutionInfo implements DialectResolutionInfo { + private final String databaseVersion; + + TestingMySQLDialectResolutionInfo(String databaseVersion) { + this.databaseVersion = databaseVersion; + } + + + @Override + public String getDatabaseName() { + return "MySQL"; + } + + @Override + public String getDatabaseVersion() { + return this.databaseVersion; + } + + @Override + public int getDatabaseMajorVersion() { + return 8; + } + + @Override + public int getDatabaseMinorVersion() { + return 0; + } + + @Override + public String getDriverName() { + return "MySQL JDBC Driver"; + } + + @Override + public int getDriverMajorVersion() { + return 8; + } + + @Override + public int getDriverMinorVersion() { + return 3; + } + + @Override + public String getSQLKeywords() { + return ""; + } + + @Override + public String toString() { + return "8.3.0"; + } + + @Override + public Map getConfigurationValues() { + return Map.of(); + } + + } + +}