Skip to content
Closed
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
@@ -0,0 +1,60 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.tool.schema.extract.internal;

import java.sql.SQLException;

import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;

/**
* Will cache all database information by reading the tableinformation from the name space in one call.
* Schema migration becomes much faster when this object is used.
* NOTE: superclass already caches the sequence information, so perhaps cache can also be coded in superclass
* @author francois
*/
public class DatabaseInformationCachedImpl extends DatabaseInformationImpl {
private NameSpaceTablesInformation defaultNameSpaceTablesInformation;
private final Namespace defaultNamespace;

/**
*
* @param serviceRegistry ServiceRegistry
* @param jdbcEnvironment JdbcEnvironment
* @param ddlTransactionIsolator DdlTransactionIsolator
* @param defaultNamespace NameSpace
*/
public DatabaseInformationCachedImpl(
ServiceRegistry serviceRegistry,
JdbcEnvironment jdbcEnvironment,
DdlTransactionIsolator ddlTransactionIsolator, Namespace defaultNamespace)
throws SQLException {
super(serviceRegistry, jdbcEnvironment, ddlTransactionIsolator, defaultNamespace.getName());
this.defaultNamespace = defaultNamespace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hibernate has specific code style, especially we use empty char aggressively. We have a ide style repo even: https://github.com/hibernate/hibernate-ide-codestyles.

You might need to refer to existing code for the style issue, especially you need to insert more space chars.

}

@Override
public TableInformation getTableInformation(QualifiedTableName qualifiedTableName) {
if (defaultNameSpaceTablesInformation == null) {
// Load table information from the whole namespace in one go (expected to be used in case of schemaMigration, so (almost) all
// tables are likely to be retrieved)
defaultNameSpaceTablesInformation = getTablesInformation(defaultNamespace);
}

TableInformation tableInformation = defaultNameSpaceTablesInformation.getTableInformation(qualifiedTableName.getTableName().getText());
if (tableInformation != null) {
return tableInformation;
}
return super.getTableInformation(qualifiedTableName); // result of this call can of course also be cached, does not seem to be in scope now
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ public void doMigration(Metadata metadata, ExecutionOptions options, TargetDescr
final JdbcContext jdbcContext = tool.resolveJdbcContext( options.getConfigurationValues() );
final DdlTransactionIsolator ddlTransactionIsolator = tool.getDdlTransactionIsolator( jdbcContext );
try {
final DatabaseInformation databaseInformation = Helper.buildDatabaseInformation(
tool.getServiceRegistry(),
ddlTransactionIsolator,
metadata.getDatabase().getDefaultNamespace().getName()
);
final DatabaseInformation databaseInformation = getDatabaseInformation(ddlTransactionIsolator, metadata.getDatabase().getDefaultNamespace());

final GenerationTarget[] targets = tool.buildGenerationTargets(
targetDescriptor,
Expand Down Expand Up @@ -139,6 +135,10 @@ public void doMigration(Metadata metadata, ExecutionOptions options, TargetDescr
}
}

protected DatabaseInformation getDatabaseInformation(DdlTransactionIsolator ddlTransactionIsolator, Namespace namespace) {
return Helper.buildDatabaseInformation(tool.getServiceRegistry(), ddlTransactionIsolator, namespace.getName());
}

protected abstract NameSpaceTablesInformation performTablesMigration(
Metadata metadata,
DatabaseInformation existingDatabase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
*/
package org.hibernate.tool.schema.internal;

import java.sql.SQLException;
import java.util.Set;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.internal.Formatter;
import org.hibernate.mapping.Table;
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
import org.hibernate.tool.schema.extract.internal.DatabaseInformationCachedImpl;
import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
import org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;
Expand All @@ -35,6 +39,20 @@ public GroupedSchemaMigratorImpl(
super( tool, schemaFilter );
}

protected DatabaseInformation getDatabaseInformation(DdlTransactionIsolator ddlTransactionIsolator, Namespace namespace) {
final JdbcEnvironment jdbcEnvironment = tool.getServiceRegistry().getService( JdbcEnvironment.class );
try {
return new DatabaseInformationCachedImpl(
tool.getServiceRegistry(),
jdbcEnvironment,
ddlTransactionIsolator,
namespace);
}
catch (SQLException e) {
throw jdbcEnvironment.getSqlExceptionHelper().convert( e, "Unable to build DatabaseInformationCached" );
}
}

@Override
protected NameSpaceTablesInformation performTablesMigration(
Metadata metadata,
Expand Down