Skip to content

Commit

Permalink
HHH-8183 Added setting to enable synonyms
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Meyer committed Apr 15, 2013
1 parent 15a0868 commit 1af9724
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
Expand Up @@ -23,6 +23,8 @@
*/
package org.hibernate.cfg;

import org.hibernate.tool.hbm2ddl.DatabaseMetadata;

/**
* @author Steve Ebersole
*/
Expand Down Expand Up @@ -638,4 +640,12 @@ public interface AvailableSettings {
public static final String JACC_CONTEXT_ID = "hibernate.jacc_context_id";
public static final String JACC_PREFIX = "hibernate.jacc";
public static final String JACC_ENABLED = "hibernate.jacc.enabled";

/**
* If enabled, allows {@link org.hibernate.tool.hbm2ddl.DatabaseMetadata} to
* support synonyms during schema update and validations. Due to the
* possibility that this would return duplicate tables (especially in
* Oracle), this is disabled by default.
*/
public static final String ENABLE_SYNONYMS = "hibernate.synonyms";
}
Expand Up @@ -34,20 +34,25 @@
import java.util.Map;
import java.util.Set;

import org.jboss.logging.Logger;

import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.exception.spi.SQLExceptionConverter;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.mapping.Table;
import org.jboss.logging.Logger;

/**
* JDBC database metadata
* @author Christoph Sturm, Teodor Danciu
*/
/**
* @author Brett Meyer
*/
public class DatabaseMetadata {

private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, DatabaseMetaData.class.getName());
Expand All @@ -59,19 +64,42 @@ public class DatabaseMetadata {
private DatabaseMetaData meta;
private SQLExceptionConverter sqlExceptionConverter;

private final String[] types;
/**
* @deprecated Use {@link #DatabaseMetadata(Connection, Dialect, Configuration)} instead
*/
@Deprecated
public DatabaseMetadata(Connection connection, Dialect dialect) throws SQLException {
this(connection, dialect, true);
this(connection, dialect, null, true);
}

/**
* @deprecated Use {@link #DatabaseMetadata(Connection, Dialect, Configuration, boolean)} instead
*/
@Deprecated
public DatabaseMetadata(Connection connection, Dialect dialect, boolean extras) throws SQLException {
this(connection, dialect, null, extras);
}

public DatabaseMetadata(Connection connection, Dialect dialect, Configuration config) throws SQLException {
this(connection, dialect, config, true);
}

public DatabaseMetadata(Connection connection, Dialect dialect, Configuration config, boolean extras)
throws SQLException {
sqlExceptionConverter = dialect.buildSQLExceptionConverter();
meta = connection.getMetaData();
this.extras = extras;
initSequences(connection, dialect);
initSequences( connection, dialect );
if ( config != null
&& ConfigurationHelper.getBoolean( AvailableSettings.ENABLE_SYNONYMS, config.getProperties(), false ) ) {
types = new String[] { "TABLE", "VIEW", "SYNONYM" };
}
else {
types = new String[] { "TABLE", "VIEW" };
}
}

private static final String[] TYPES = {"TABLE", "VIEW", "SYNONYM"};

public TableMetadata getTableMetadata(String name, String schema, String catalog, boolean isQuoted) throws HibernateException {

Object identifier = identifier(catalog, schema, name);
Expand All @@ -85,14 +113,14 @@ public TableMetadata getTableMetadata(String name, String schema, String catalog
ResultSet rs = null;
try {
if ( (isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
rs = meta.getTables(catalog, schema, name, TYPES);
rs = meta.getTables(catalog, schema, name, types);
} else if ( (isQuoted && meta.storesUpperCaseQuotedIdentifiers())
|| (!isQuoted && meta.storesUpperCaseIdentifiers() )) {
rs = meta.getTables(
StringHelper.toUpperCase(catalog),
StringHelper.toUpperCase(schema),
StringHelper.toUpperCase(name),
TYPES
types
);
}
else if ( (isQuoted && meta.storesLowerCaseQuotedIdentifiers())
Expand All @@ -101,11 +129,11 @@ else if ( (isQuoted && meta.storesLowerCaseQuotedIdentifiers())
StringHelper.toLowerCase( catalog ),
StringHelper.toLowerCase(schema),
StringHelper.toLowerCase(name),
TYPES
types
);
}
else {
rs = meta.getTables(catalog, schema, name, TYPES);
rs = meta.getTables(catalog, schema, name, types);
}

while ( rs.next() ) {
Expand Down
Expand Up @@ -193,7 +193,7 @@ public void execute(Target target) {
LOG.fetchingDatabaseMetadata();
connectionHelper.prepare( true );
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect );
meta = new DatabaseMetadata( connection, dialect, configuration );
stmt = connection.createStatement();
}
catch ( SQLException sqle ) {
Expand Down
Expand Up @@ -145,7 +145,7 @@ public void validate() {
LOG.fetchingDatabaseMetadata();
connectionHelper.prepare( false );
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect, false );
meta = new DatabaseMetadata( connection, dialect, configuration, false );
}
catch ( SQLException sqle ) {
LOG.unableToGetDatabaseMetadata(sqle);
Expand Down

0 comments on commit 1af9724

Please sign in to comment.