Skip to content

Commit

Permalink
QUARKUS-3363 - Disable JDBC Metadata Defaults in quarkus
Browse files Browse the repository at this point in the history
Handle Hibernate's `DialectSpecificSettings` as supported Quarkus settings

- test for mariadb
  • Loading branch information
sebersole committed Sep 25, 2024
1 parent 23df28b commit a97c74f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ interface HibernateOrmConfigPersistenceUnitDialect {
* E.g. `MyISAM` or `InnoDB` for MySQL.
*
* @deprecated Use {@code mysql.}{@linkplain MySQLDialectConfig#storageEngine storage-engine}
* or {@code mariadb.}{@linkplain MySQLDialectConfig#storageEngine storage-engine} instead
* or {@code mariadb.}{@linkplain MySQLDialectConfig#storageEngine storage-engine} instead
*
* @asciidoclet
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import static org.hibernate.cfg.AvailableSettings.USE_DIRECT_REFERENCE_CACHE_ENTRIES;
import static org.hibernate.cfg.AvailableSettings.USE_QUERY_CACHE;
import static org.hibernate.cfg.AvailableSettings.USE_SECOND_LEVEL_CACHE;
import static org.hibernate.cfg.DialectSpecificSettings.COCKROACH_VERSION_STRING;
import static org.hibernate.cfg.DialectSpecificSettings.HANA_MAX_LOB_PREFETCH_SIZE;
import static org.hibernate.cfg.DialectSpecificSettings.MYSQL_BYTES_PER_CHARACTER;
import static org.hibernate.cfg.DialectSpecificSettings.MYSQL_NO_BACKSLASH_ESCAPES;
import static org.hibernate.cfg.DialectSpecificSettings.ORACLE_APPLICATION_CONTINUITY;
import static org.hibernate.cfg.DialectSpecificSettings.ORACLE_AUTONOMOUS_DATABASE;
import static org.hibernate.cfg.DialectSpecificSettings.ORACLE_EXTENDED_STRING_SIZE;
import static org.hibernate.cfg.DialectSpecificSettings.SQL_SERVER_COMPATIBILITY_LEVEL;
import static org.hibernate.cfg.DialectSpecificSettings.SYBASE_ANSI_NULL;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -1210,105 +1207,101 @@ private static void handleDialectSpecificSettings(
Optional<String> dbKind,
Optional<String> dialect) {

final Optional<SupportedDatabaseKind> databaseKind = determineDatabaseKind( dbKind, dialect);
final Optional<SupportedDatabaseKind> databaseKind = determineDatabaseKind(dbKind, dialect);

handleStorageEngine( databaseKind, persistenceUnitName, dialectConfig, storageEngineCollector,
puPropertiesCollector, systemProperties );
handleStorageEngine(databaseKind, persistenceUnitName, dialectConfig, storageEngineCollector,
puPropertiesCollector, systemProperties);

if ( dialectConfig.mariadb().bytesPerCharacter().isPresent()
|| dialectConfig.mariadb().noBackslashEscapes().isPresent() ) {
if ( databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.MARIADB ) {
LOG.warnf( "MariaDB specific settings being ignored because the database is not MariaDB." );
}
else {
if (dialectConfig.mariadb().bytesPerCharacter().isPresent()
|| dialectConfig.mariadb().noBackslashEscapes().isPresent()) {
if (databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.MARIADB) {
LOG.warnf("MariaDB specific settings being ignored because the database is not MariaDB.");
} else {
applyOptionalIntegerSetting(dialectConfig.mariadb().bytesPerCharacter(), MYSQL_BYTES_PER_CHARACTER,
puPropertiesCollector);
puPropertiesCollector);
applyOptionalBooleanSetting(dialectConfig.mariadb().noBackslashEscapes(), MYSQL_NO_BACKSLASH_ESCAPES,
puPropertiesCollector);
puPropertiesCollector);
}
}

if ( dialectConfig.mysql().bytesPerCharacter().isPresent()
|| dialectConfig.mysql().noBackslashEscapes().isPresent() ) {
if ( databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.MYSQL ) {
LOG.warnf( "MariaDB specific settings being ignored because the database is not MySQL." );
}
else {
if (dialectConfig.mysql().bytesPerCharacter().isPresent()
|| dialectConfig.mysql().noBackslashEscapes().isPresent()) {
if (databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.MYSQL) {
LOG.warnf("MariaDB specific settings being ignored because the database is not MySQL.");
} else {
applyOptionalIntegerSetting(dialectConfig.mysql().bytesPerCharacter(), MYSQL_BYTES_PER_CHARACTER,
puPropertiesCollector);
puPropertiesCollector);
applyOptionalBooleanSetting(dialectConfig.mysql().noBackslashEscapes(), MYSQL_NO_BACKSLASH_ESCAPES,
puPropertiesCollector);
puPropertiesCollector);
}
}

if ( dialectConfig.oracle().isAnyPropertySet() ) {
if ( databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.ORACLE ) {
LOG.warnf( "Oracle specific settings being ignored because the database is not Oracle." );
}
else {
if (dialectConfig.oracle().isAnyPropertySet()) {
if (databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.ORACLE) {
LOG.warnf("Oracle specific settings being ignored because the database is not Oracle.");
} else {
applyOptionalBooleanSetting(dialectConfig.oracle().applicationContinuity(), ORACLE_APPLICATION_CONTINUITY,
puPropertiesCollector);
puPropertiesCollector);
applyOptionalBooleanSetting(dialectConfig.oracle().autonomous(), ORACLE_AUTONOMOUS_DATABASE,
puPropertiesCollector);
puPropertiesCollector);
applyOptionalBooleanSetting(dialectConfig.oracle().extended(), ORACLE_EXTENDED_STRING_SIZE,
puPropertiesCollector);
puPropertiesCollector);
}
}

if ( dialectConfig.mssql().isAnyPropertySet() ) {
if ( databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.MSSQL ) {
LOG.warnf( "SQL Server specific settings being ignored because the database is not SQL Server." );
}
else {
applyOptionalStringSetting( dialectConfig.mssql().compatibilityLevel(), SQL_SERVER_COMPATIBILITY_LEVEL,
puPropertiesCollector);
if (dialectConfig.mssql().isAnyPropertySet()) {
if (databaseKind.isPresent() && databaseKind.get() != SupportedDatabaseKind.MSSQL) {
LOG.warnf("SQL Server specific settings being ignored because the database is not SQL Server.");
} else {
applyOptionalStringSetting(dialectConfig.mssql().compatibilityLevel(), SQL_SERVER_COMPATIBILITY_LEVEL,
puPropertiesCollector);
}
}
}

private static Optional<SupportedDatabaseKind> determineDatabaseKind(Optional<String> dbKind, Optional<String> dialect) {
if ( dbKind.isPresent() ) {
final String databaseKindName = DatabaseKind.normalize( dbKind.get() );
final SupportedDatabaseKind resolved = determineDatabaseKind( databaseKindName );
if ( resolved != null ) {
return Optional.of( resolved );
if (dbKind.isPresent()) {
final String databaseKindName = DatabaseKind.normalize(dbKind.get());
final SupportedDatabaseKind resolved = determineDatabaseKind(databaseKindName);
if (resolved != null) {
return Optional.of(resolved);
}
}

if ( dialect.isPresent() ) {
if (dialect.isPresent()) {
String lowercaseDialect = dialect.get().toLowerCase(Locale.ROOT);
final SupportedDatabaseKind resolved = determineDatabaseKind( lowercaseDialect );
if ( resolved != null ) {
return Optional.of( resolved );
final SupportedDatabaseKind resolved = determineDatabaseKind(lowercaseDialect);
if (resolved != null) {
return Optional.of(resolved);
}
}

return Optional.empty();
}

private static SupportedDatabaseKind determineDatabaseKind(String name) {
if ( name.contains( "db2" ) ) {
if (name.contains("db2")) {
return SupportedDatabaseKind.DB2;
}
if ( name.contains( "derby" ) ) {
if (name.contains("derby")) {
return SupportedDatabaseKind.DERBY;
}
if ( name.contains( "h2" ) ) {
if (name.contains("h2")) {
return SupportedDatabaseKind.H2;
}
if ( name.contains( "mariadb" ) ) {
if (name.contains("mariadb")) {
return SupportedDatabaseKind.MARIADB;
}
if ( name.contains( "mssql" ) || name.contains( "sqlserver" ) ) {
if (name.contains("mssql") || name.contains("sqlserver")) {
return SupportedDatabaseKind.MSSQL;
}
if ( name.contains( "mysql" ) ) {
if (name.contains("mysql")) {
return SupportedDatabaseKind.MYSQL;
}
if ( name.contains( "oracle" ) ) {
if (name.contains("oracle")) {
return SupportedDatabaseKind.ORACLE;
}
if ( name.contains( "postgresql" ) ) {
if (name.contains("postgresql")) {
return SupportedDatabaseKind.POSTGRESQL;
}

Expand All @@ -1323,63 +1316,58 @@ private static void handleStorageEngine(
BiConsumer<String, String> puPropertiesCollector,
BuildProducer<SystemPropertyBuildItem> systemProperties) {

final String topLevelStorageEngine = dialectConfig.storageEngine().orElse( null );
final String topLevelStorageEngine = dialectConfig.storageEngine().orElse(null);

if ( topLevelStorageEngine != null ) {
if (topLevelStorageEngine != null) {
// NOTE: this top-level storage-engine setting is deprecated - log a warning
LOG.warnf(
"The storage engine set through configuration property '%1$s' is no longer supported; "
+ "use '%1$s' or '%1$s' instead, depending on the database.",
HibernateOrmRuntimeConfig.puPropertyKey( persistenceUnitName, "dialect.mariadb.storage-engine" ),
HibernateOrmRuntimeConfig.puPropertyKey( persistenceUnitName, "dialect.mysql.storage-engine" )
);
HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName, "dialect.mariadb.storage-engine"),
HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName, "dialect.mysql.storage-engine"));
}

// if we know this is mariadb and any non-mariadb settings are set, ...
// todo : what? log a warning? exception?

final String mariaDbStorageEngine = dialectConfig.mariadb().storageEngine().orElse( topLevelStorageEngine );
final String mysqlDbStorageEngine = dialectConfig.mysql().storageEngine().orElse( topLevelStorageEngine );
if ( supportedDatabaseKind.isPresent() && supportedDatabaseKind.get() == SupportedDatabaseKind.MARIADB ) {
if ( mariaDbStorageEngine != null ) {
final String mariaDbStorageEngine = dialectConfig.mariadb().storageEngine().orElse(topLevelStorageEngine);
final String mysqlDbStorageEngine = dialectConfig.mysql().storageEngine().orElse(topLevelStorageEngine);
if (supportedDatabaseKind.isPresent() && supportedDatabaseKind.get() == SupportedDatabaseKind.MARIADB) {
if (mariaDbStorageEngine != null) {
storageEngineCollector.add(mariaDbStorageEngine);
systemProperties.produce(new SystemPropertyBuildItem(AvailableSettings.STORAGE_ENGINE, mariaDbStorageEngine));
}
}
else if ( supportedDatabaseKind.isPresent() && supportedDatabaseKind.get() == SupportedDatabaseKind.MYSQL ) {
if ( mysqlDbStorageEngine != null ) {
} else if (supportedDatabaseKind.isPresent() && supportedDatabaseKind.get() == SupportedDatabaseKind.MYSQL) {
if (mysqlDbStorageEngine != null) {
storageEngineCollector.add(mysqlDbStorageEngine);
systemProperties.produce(new SystemPropertyBuildItem(AvailableSettings.STORAGE_ENGINE, mysqlDbStorageEngine));
}
}
else {
} else {
final String storageEngine;
final String storageEngineSource;
if ( topLevelStorageEngine != null ) {
if (topLevelStorageEngine != null) {
storageEngine = topLevelStorageEngine;
storageEngineSource = HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName, "dialect.storage-engine");
}
else if ( mariaDbStorageEngine != null ) {
} else if (mariaDbStorageEngine != null) {
storageEngine = mariaDbStorageEngine;
storageEngineSource = HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName, "dialect.mariadb.storage-engine");
}
else if ( mysqlDbStorageEngine != null ) {
storageEngineSource = HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName,
"dialect.mariadb.storage-engine");
} else if (mysqlDbStorageEngine != null) {
storageEngine = mysqlDbStorageEngine;
storageEngineSource = HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName, "dialect.mysql.storage-engine");
}
else {
storageEngineSource = HibernateOrmRuntimeConfig.puPropertyKey(persistenceUnitName,
"dialect.mysql.storage-engine");
} else {
storageEngine = null;
storageEngineSource = null;
}

if ( storageEngine != null ) {
if ( supportedDatabaseKind.isPresent() ) {
if (storageEngine != null) {
if (supportedDatabaseKind.isPresent()) {
LOG.warnf(
"The storage engine set through configuration property '%1$s', is being ignored"
+ " because the database is neither MySQL nor MariaDB.",
storageEngineSource);
}
else {
} else {
systemProperties.produce(new SystemPropertyBuildItem(AvailableSettings.STORAGE_ENGINE, storageEngine));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public interface MySQLDialectConfig {
* Specifies the bytes per character to use based on the database's configured
* <a href="https://dev.mysql.com/doc/refman/8.0/en/charset-charsets.html">charset</a>.
*
* See link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#MYSQL_BYTES_PER_CHARACTER[MYSQL_BYTES_PER_CHARACTER]
* See
* link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#MYSQL_BYTES_PER_CHARACTER[MYSQL_BYTES_PER_CHARACTER]
*
* @asciidoctor
*/
Expand All @@ -27,7 +28,8 @@ public interface MySQLDialectConfig {
/**
* Specifies whether the {@code NO_BACKSLASH_ESCAPES} sql mode is enabled.
*
* See link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#MYSQL_NO_BACKSLASH_ESCAPES[MYSQL_NO_BACKSLASH_ESCAPES]
* See
* link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#MYSQL_NO_BACKSLASH_ESCAPES[MYSQL_NO_BACKSLASH_ESCAPES]
*
* @asciidoctor
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public interface OracleDialectConfig {
/**
* Support for Oracle's MAX_STRING_SIZE = EXTENDED.
*
* See link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#ORACLE_EXTENDED_STRING_SIZE[ORACLE_EXTENDED_STRING_SIZE]
* See
* link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#ORACLE_EXTENDED_STRING_SIZE[ORACLE_EXTENDED_STRING_SIZE]
*
* @asciidoctor
*/
Expand All @@ -24,7 +25,8 @@ public interface OracleDialectConfig {
/**
* Specifies whether this database is running on an Autonomous Database Cloud Service.
*
* See link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#ORACLE_AUTONOMOUS_DATABASE[ORACLE_AUTONOMOUS_DATABASE]
* See
* link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#ORACLE_AUTONOMOUS_DATABASE[ORACLE_AUTONOMOUS_DATABASE]
*
* @asciidoctor
*/
Expand All @@ -34,7 +36,8 @@ public interface OracleDialectConfig {
/**
* Specifies whether this database is accessed using a database service protected by Application Continuity.
*
* See link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#ORACLE_APPLICATION_CONTINUITY[ORACLE_APPLICATION_CONTINUITY]
* See
* link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#ORACLE_APPLICATION_CONTINUITY[ORACLE_APPLICATION_CONTINUITY]
*
* @asciidoctor
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public interface SqlServerDialectConfig {
/**
* The {@code compatibility_level} as defined in {@code sys.databases}.
*
* See link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#SQL_SERVER_COMPATIBILITY_LEVEL[SQL_SERVER_COMPATIBILITY_LEVEL]
* See
* link:{hibernate-orm-javadocs-url}/org/hibernate/cfg/DialectSpecificSettings.html#SQL_SERVER_COMPATIBILITY_LEVEL[SQL_SERVER_COMPATIBILITY_LEVEL]
*
* @asciidoctor
*/
Expand Down

0 comments on commit a97c74f

Please sign in to comment.