Skip to content

Commit

Permalink
HHH-9966 - Improve schema tooling support for creating catalogs and s…
Browse files Browse the repository at this point in the history
…chemas
  • Loading branch information
sebersole committed Jul 24, 2015
1 parent 33b00de commit 5394659
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 364 deletions.
Expand Up @@ -32,7 +32,7 @@
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.extract.internal.legacy.DatabaseInformationImpl; import org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl;
import org.hibernate.tool.schema.extract.spi.DatabaseInformation; import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
import org.hibernate.tool.schema.internal.TargetDatabaseImpl; import org.hibernate.tool.schema.internal.TargetDatabaseImpl;
import org.hibernate.tool.schema.internal.TargetFileImpl; import org.hibernate.tool.schema.internal.TargetFileImpl;
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.extract.internal.legacy.DatabaseInformationImpl; import org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl;
import org.hibernate.tool.schema.extract.spi.DatabaseInformation; import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
import org.hibernate.tool.schema.spi.SchemaManagementTool; import org.hibernate.tool.schema.spi.SchemaManagementTool;


Expand Down
Expand Up @@ -6,94 +6,150 @@
*/ */
package org.hibernate.tool.schema.extract.internal; package org.hibernate.tool.schema.extract.internal;


import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;


import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.QualifiedSequenceName; import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.QualifiedTableName; import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.extract.spi.DatabaseInformation; import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
import org.hibernate.tool.schema.extract.spi.ExtractionContext; import org.hibernate.tool.schema.extract.spi.ExtractionContext;
import org.hibernate.tool.schema.extract.spi.InformationExtractor;
import org.hibernate.tool.schema.extract.spi.SequenceInformation; import org.hibernate.tool.schema.extract.spi.SequenceInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation; import org.hibernate.tool.schema.extract.spi.TableInformation;


/** /**
* Access to information from the existing database.
*
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class DatabaseInformationImpl implements DatabaseInformation, ExtractionContext.DatabaseObjectAccess { public class DatabaseInformationImpl implements DatabaseInformation, ExtractionContext.DatabaseObjectAccess {
private final Map<QualifiedTableName,TableInformation> tableInformationMap = new HashMap<QualifiedTableName, TableInformation>(); private final InformationExtractor extractor;
private final ExtractionContext extractionContext;

private final JdbcEnvironment jdbcEnvironment;

private final Map<QualifiedSequenceName,SequenceInformation> sequenceInformationMap = new HashMap<QualifiedSequenceName, SequenceInformation>(); private final Map<QualifiedSequenceName,SequenceInformation> sequenceInformationMap = new HashMap<QualifiedSequenceName, SequenceInformation>();


public DatabaseInformationImpl() { public DatabaseInformationImpl(
ServiceRegistry serviceRegistry,
JdbcEnvironment jdbcEnvironment,
JdbcConnectionAccess jdbcConnectionAccess,
Identifier defaultCatalogName,
Identifier defaultSchemaName) throws SQLException {
this.jdbcEnvironment = jdbcEnvironment;

this.extractionContext = new ExtractionContextImpl(
serviceRegistry,
jdbcEnvironment,
jdbcConnectionAccess,
this,
defaultCatalogName,
defaultSchemaName
);

// todo : make this pluggable
this.extractor = new InformationExtractorJdbcDatabaseMetaDataImpl( extractionContext );

// legacy code did initialize sequences...
initializeSequences();
} }



private void initializeSequences() throws SQLException {
// DatabaseInformation implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Iterable<SequenceInformation> itr = jdbcEnvironment.getDialect().getSequenceInformationExtractor().extractMetadata( extractionContext );
for ( SequenceInformation sequenceInformation : itr ) {
sequenceInformationMap.put(
// for now, follow the legacy behavior of storing just the
// unqualified sequence name.
new QualifiedSequenceName(
null,
null,
sequenceInformation.getSequenceName().getSequenceName()
),
sequenceInformation
);
}
}


@Override @Override
public boolean schemaExists(Namespace.Name schema) { public boolean schemaExists(Namespace.Name schema) {
return false; return extractor.schemaExists( schema.getCatalog(), schema.getSchema() );
} }


@Override @Override
public TableInformation getTableInformation(Identifier catalogName, Identifier schemaName, Identifier tableName) { public TableInformation getTableInformation(
return locateTableInformation( new QualifiedTableName( catalogName, schemaName, tableName ) ); Identifier catalogName,
Identifier schemaName,
Identifier tableName) {
return getTableInformation( new QualifiedTableName( catalogName, schemaName, tableName ) );
} }


@Override @Override
public TableInformation getTableInformation(Namespace.Name schemaName, Identifier tableName) { public TableInformation getTableInformation(
return locateTableInformation( new QualifiedTableName( schemaName, tableName ) ); Namespace.Name schemaName,
Identifier tableName) {
return getTableInformation( new QualifiedTableName( schemaName, tableName ) );
} }


@Override @Override
public TableInformation getTableInformation(QualifiedTableName tableName) { public TableInformation getTableInformation(QualifiedTableName qualifiedTableName) {
return locateTableInformation( tableName ); if ( qualifiedTableName.getObjectName() == null ) {
throw new IllegalArgumentException( "Passed table name cannot be null" );
}

return extractor.getTable(
qualifiedTableName.getCatalogName(),
qualifiedTableName.getSchemaName(),
qualifiedTableName.getTableName()
);
} }


@Override @Override
public SequenceInformation getSequenceInformation(Identifier catalogName, Identifier schemaName, Identifier sequenceName) { public void registerTable(TableInformation tableInformation) {
return locateSequenceInformation( new QualifiedSequenceName( catalogName, schemaName, sequenceName ) );
} }


@Override @Override
public SequenceInformation getSequenceInformation(Namespace.Name schemaName, Identifier sequenceName) { public SequenceInformation getSequenceInformation(
return locateSequenceInformation( new QualifiedSequenceName( schemaName, sequenceName ) ); Identifier catalogName,
Identifier schemaName,
Identifier sequenceName) {
return getSequenceInformation( new QualifiedSequenceName( catalogName, schemaName, sequenceName ) );
} }


@Override @Override
public SequenceInformation getSequenceInformation(QualifiedSequenceName sequenceName) { public SequenceInformation getSequenceInformation(
return locateSequenceInformation( sequenceName ); Namespace.Name schemaName,
Identifier sequenceName) {
return getSequenceInformation( new QualifiedSequenceName( schemaName, sequenceName ) );
} }


@Override @Override
public boolean catalogExists(Identifier catalog) { public SequenceInformation getSequenceInformation(QualifiedSequenceName qualifiedSequenceName) {
return false; return locateSequenceInformation( qualifiedSequenceName );
}


// RegisteredObjectAccess implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void registerTableInformation(TableInformation tableInformation) {
tableInformationMap.put( tableInformation.getName(), tableInformation );
} }


public void registerSequenceInformation(SequenceInformation sequenceInformation) { @Override
sequenceInformationMap.put( sequenceInformation.getSequenceName(), sequenceInformation ); public boolean catalogExists(Identifier catalog) {
return extractor.catalogExists( catalog );
} }


@Override @Override
public TableInformation locateTableInformation(QualifiedTableName tableName) { public TableInformation locateTableInformation(QualifiedTableName tableName) {
return tableInformationMap.get( tableName ); return getTableInformation( tableName );
} }


@Override @Override
public SequenceInformation locateSequenceInformation(QualifiedSequenceName sequenceName) { public SequenceInformation locateSequenceInformation(QualifiedSequenceName sequenceName) {
return sequenceInformationMap.get( sequenceName ); // again, follow legacy behavior
} if ( sequenceName.getCatalogName() != null || sequenceName.getSchemaName() != null ) {
sequenceName = new QualifiedSequenceName( null, null, sequenceName.getSequenceName() );
}


@Override return sequenceInformationMap.get( sequenceName );
public void registerTable(TableInformation tableInformation) {
tableInformationMap.put( tableInformation.getName(), tableInformation );
} }
} }

This file was deleted.

This file was deleted.

0 comments on commit 5394659

Please sign in to comment.