Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-13788 Schema update try to recreate existing tables #3151

Merged
merged 3 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
*/
package org.hibernate.dialect;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.hibernate.engine.jdbc.env.spi.AnsiSqlKeywords;
import org.hibernate.engine.jdbc.env.spi.IdentifierCaseStrategy;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;

/**
* @author Vlad Mihalcea
*/
Expand All @@ -22,4 +30,15 @@ public boolean supportsRowValueConstructorSyntaxInInList() {
protected MySQLStorageEngine getDefaultMySQLStorageEngine() {
return InnoDBStorageEngine.INSTANCE;
}

@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {

// some MariaDB drivers does not return case strategy info
builder.setUnquotedCaseStrategy( IdentifierCaseStrategy.MIXED );
builder.setQuotedCaseStrategy( IdentifierCaseStrategy.MIXED );

return super.buildIdentifierHelper( builder, dbMetaData );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.dialect;

import java.sql.CallableStatement;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
Expand All @@ -27,6 +28,9 @@
import org.hibernate.dialect.pagination.LimitHelper;
import org.hibernate.dialect.unique.MySQLUniqueDelegate;
import org.hibernate.dialect.unique.UniqueDelegate;
import org.hibernate.engine.jdbc.env.spi.IdentifierCaseStrategy;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.LockTimeoutException;
Expand Down Expand Up @@ -562,6 +566,18 @@ public JDBCException convert(SQLException sqlException, String message, String s
};
}

@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {

if ( dbMetaData == null ) {
builder.setUnquotedCaseStrategy( IdentifierCaseStrategy.MIXED );
builder.setQuotedCaseStrategy( IdentifierCaseStrategy.MIXED );
}

return super.buildIdentifierHelper( builder, dbMetaData );
}

@Override
public String getNotExpression(String expression) {
return "not (" + expression + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.hibernate.dialect.pagination.AbstractLimitHandler;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.LimitHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierCaseStrategy;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
Expand Down Expand Up @@ -76,6 +79,18 @@ public boolean bindLimitParametersInReverseOrder() {
}
};

@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {

if ( dbMetaData == null ) {
builder.setUnquotedCaseStrategy( IdentifierCaseStrategy.LOWER );
builder.setQuotedCaseStrategy( IdentifierCaseStrategy.MIXED );
}

return super.buildIdentifierHelper( builder, dbMetaData );
}

/**
* Constructs a PostgreSQL81Dialect
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.dialect;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

Expand All @@ -19,6 +21,9 @@
import org.hibernate.dialect.pagination.LegacyLimitHandler;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.TopLimitHandler;
import org.hibernate.engine.jdbc.env.spi.IdentifierCaseStrategy;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
Expand Down Expand Up @@ -107,6 +112,18 @@ public boolean useMaxForLimit() {
return true;
}

@Override
public IdentifierHelper buildIdentifierHelper(
IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData) throws SQLException {

if ( dbMetaData == null ) {
builder.setUnquotedCaseStrategy( IdentifierCaseStrategy.MIXED );
builder.setQuotedCaseStrategy( IdentifierCaseStrategy.MIXED );
}

return super.buildIdentifierHelper( builder, dbMetaData );
}

@Override
public boolean supportsLimitOffset() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class IdentifierHelperBuilder {
private boolean globallyQuoteIdentifiers = false;
private boolean skipGlobalQuotingForColumnDefinitions = false;
private boolean autoQuoteKeywords = true;
private IdentifierCaseStrategy unquotedCaseStrategy = IdentifierCaseStrategy.MIXED;
private IdentifierCaseStrategy unquotedCaseStrategy = IdentifierCaseStrategy.UPPER;
private IdentifierCaseStrategy quotedCaseStrategy = IdentifierCaseStrategy.MIXED;

public static IdentifierHelperBuilder from(JdbcEnvironment jdbcEnvironment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
Expand Down Expand Up @@ -59,12 +61,26 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information

private final ExtractionContext extractionContext;

private final boolean useJdbcMetadataDefaultsSetting;

private Identifier currentCatalog;
private Identifier currentSchema;

private String currentCatalogFilter;
private String currentSchemaFilter;

public InformationExtractorJdbcDatabaseMetaDataImpl(ExtractionContext extractionContext) {
this.extractionContext = extractionContext;

ConfigurationService configService = extractionContext.getServiceRegistry()
.getService( ConfigurationService.class );

useJdbcMetadataDefaultsSetting = configService.getSetting(
"hibernate.temp.use_jdbc_metadata_defaults",
StandardConverters.BOOLEAN,
Boolean.TRUE
);

final String extraPhysycalTableTypesConfig = configService.getSetting(
AvailableSettings.EXTRA_PHYSICAL_TABLE_TYPES,
StandardConverters.STRING,
Expand Down Expand Up @@ -229,11 +245,14 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
TableInformation tableInfo = null;

// 1) look in current namespace
if ( extractionContext.getJdbcEnvironment().getCurrentCatalog() != null
|| extractionContext.getJdbcEnvironment().getCurrentSchema() != null ) {
final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment();
final Identifier currentSchema = getCurrentSchema( jdbcEnvironment );
final Identifier currentCatalog = getCurrentCatalog( jdbcEnvironment );
if ( currentCatalog != null
|| currentSchema != null ) {
tableInfo = locateTableInNamespace(
extractionContext.getJdbcEnvironment().getCurrentCatalog(),
extractionContext.getJdbcEnvironment().getCurrentSchema(),
currentCatalog,
currentSchema,
tableName
);

Expand Down Expand Up @@ -288,42 +307,125 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
}
}

private Identifier getCurrentSchema(JdbcEnvironment jdbcEnvironment) {
if ( currentSchema != null ) {
return currentSchema;
}
final Identifier schema = jdbcEnvironment.getCurrentSchema();
if ( schema != null ) {
currentSchema = schema;
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentSchema = extractionContext.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( extractionContext.getJdbcConnection().getSchema() );
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentCatalog;
}

private Identifier getCurrentCatalog(JdbcEnvironment jdbcEnvironment) {
if ( currentCatalog != null ) {
return currentCatalog;
}
final Identifier catalog = jdbcEnvironment.getCurrentCatalog();
if ( catalog != null ) {
currentCatalog = catalog;
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentCatalog = extractionContext.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( extractionContext.getJdbcConnection().getCatalog() );
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentCatalog;
}

private String getCurrentCatalogFilter(JdbcEnvironment jdbcEnvironment) {
if ( currentCatalogFilter != null ) {
return currentCatalogFilter;
}
final Identifier currentCatalog = jdbcEnvironment.getCurrentCatalog();
if ( currentCatalog != null ) {
currentCatalogFilter = toMetaDataObjectName( currentCatalog );
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentCatalogFilter = extractionContext.getJdbcConnection().getCatalog();
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentCatalogFilter;
}

private String getCurrentSchemaFilter(JdbcEnvironment jdbcEnvironment) {
if ( currentSchemaFilter != null ) {
return currentSchemaFilter;
}
final Identifier currentSchema = jdbcEnvironment.getCurrentSchema();
if ( currentSchema != null ) {
currentSchemaFilter = toMetaDataObjectName( currentSchema );
}

if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentSchemaFilter = extractionContext.getJdbcConnection().getSchema();
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentSchemaFilter;
}

public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) {

String catalogFilter = null;
String schemaFilter = null;

if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsCatalogs() ) {
final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment();
final NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport();
if ( nameQualifierSupport.supportsCatalogs() ) {
if ( catalog == null ) {
if ( extractionContext.getJdbcEnvironment().getCurrentCatalog() != null ) {
// 1) look in current namespace
catalogFilter = toMetaDataObjectName( extractionContext.getJdbcEnvironment().getCurrentCatalog() );
}
else if ( extractionContext.getDefaultCatalog() != null ) {
// 2) look in default namespace
catalogFilter = toMetaDataObjectName( extractionContext.getDefaultCatalog() );
}
else {
catalogFilter = "";
// look in the current namespace
catalogFilter = getCurrentCatalogFilter(jdbcEnvironment);
if ( catalogFilter == null ) {
if ( extractionContext.getDefaultCatalog() != null ) {
// 2) look in default namespace
catalogFilter = toMetaDataObjectName( extractionContext.getDefaultCatalog() );
}
else {
catalogFilter = "";
}
}
}
else {
catalogFilter = toMetaDataObjectName( catalog );
}
}

if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsSchemas() ) {
if ( nameQualifierSupport.supportsSchemas() ) {
if ( schema == null ) {
if ( extractionContext.getJdbcEnvironment().getCurrentSchema() != null ) {
// 1) look in current namespace
schemaFilter = toMetaDataObjectName( extractionContext.getJdbcEnvironment().getCurrentSchema() );
}
else if ( extractionContext.getDefaultSchema() != null ) {
// 2) look in default namespace
schemaFilter = toMetaDataObjectName( extractionContext.getDefaultSchema() );
}
else {
schemaFilter = "";
// 1) look in current namespace
schemaFilter = getCurrentSchemaFilter( jdbcEnvironment );
if ( schemaFilter == null ) {
if ( extractionContext.getDefaultSchema() != null ) {
// 2) look in default namespace
schemaFilter = toMetaDataObjectName( extractionContext.getDefaultSchema() );
}
else {
schemaFilter = "";
}
}
}
else {
Expand Down
Loading