下面列出了怎么用org.hibernate.boot.model.relational.Namespace的API类实例代码及写法,或者点击链接到github查看源代码。
public DatabaseInformationImpl(
ServiceRegistry serviceRegistry,
JdbcEnvironment jdbcEnvironment,
DdlTransactionIsolator ddlTransactionIsolator,
Namespace.Name defaultNamespace) throws SQLException {
this.jdbcEnvironment = jdbcEnvironment;
this.extractionContext = new ImprovedExtractionContextImpl(
serviceRegistry,
jdbcEnvironment,
ddlTransactionIsolator,
defaultNamespace.getCatalog(),
defaultNamespace.getSchema(),
this
);
// todo : make this pluggable
this.extractor = new InformationExtractorJdbcDatabaseMetaDataImpl( extractionContext );
// because we do not have defined a way to locate sequence info by name
initializeSequences();
}
@Override
protected void validateTables(
Metadata metadata,
DatabaseInformation databaseInformation,
ExecutionOptions options,
Dialect dialect, Namespace namespace) {
final NameSpaceTablesInformation tables = databaseInformation.getTablesInformation( namespace );
for ( Table table : namespace.getTables() ) {
if ( schemaFilter.includeTable( table ) && table.isPhysicalTable() ) {
validateTable(
table,
tables.getTableInformation( table ),
metadata,
options,
dialect
);
}
}
}
@Override
protected void validateTables(
Metadata metadata,
DatabaseInformation databaseInformation,
ExecutionOptions options,
Dialect dialect,
Namespace namespace) {
for ( Table table : namespace.getTables() ) {
if ( schemaFilter.includeTable( table ) && table.isPhysicalTable() ) {
final TableInformation tableInformation = databaseInformation.getTableInformation(
table.getQualifiedTableName()
);
validateTable( table, tableInformation, metadata, options, dialect );
}
}
}
public static DatabaseInformation buildDatabaseInformation(
ServiceRegistry serviceRegistry,
DdlTransactionIsolator ddlTransactionIsolator,
Namespace.Name defaultNamespace) {
final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
try {
return new DatabaseInformationImpl(
serviceRegistry,
jdbcEnvironment,
ddlTransactionIsolator,
defaultNamespace
);
}
catch (SQLException e) {
throw jdbcEnvironment.getSqlExceptionHelper().convert( e, "Unable to build DatabaseInformation" );
}
}
protected void buildSequence(Database database) {
final int sourceIncrementSize = getSourceIncrementSize();
final Namespace namespace = database.locateNamespace(
logicalQualifiedSequenceName.getCatalogName(),
logicalQualifiedSequenceName.getSchemaName()
);
Sequence sequence = namespace.locateSequence( logicalQualifiedSequenceName.getObjectName() );
if ( sequence != null ) {
sequence.validate( initialValue, sourceIncrementSize );
}
else {
sequence = namespace.createSequence( logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize );
}
this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
sequence.getName(),
database.getJdbcEnvironment().getDialect()
);
}
public Table(
Namespace namespace,
Identifier physicalTableName,
boolean isAbstract) {
this.catalog = namespace.getPhysicalName().getCatalog();
this.schema = namespace.getPhysicalName().getSchema();
this.name = physicalTableName;
this.isAbstract = isAbstract;
}
public Table(Namespace namespace, Identifier physicalTableName, String subselect, boolean isAbstract) {
this.catalog = namespace.getPhysicalName().getCatalog();
this.schema = namespace.getPhysicalName().getSchema();
this.name = physicalTableName;
this.subselect = subselect;
this.isAbstract = isAbstract;
}
public DenormalizedTable(
Namespace namespace,
Identifier physicalTableName,
String subselectFragment,
boolean isAbstract,
Table includedTable) {
super( namespace, physicalTableName, subselectFragment, isAbstract );
this.includedTable = includedTable;
includedTable.setHasDenormalizedTables();
}
protected abstract NameSpaceTablesInformation performTablesMigration(
Metadata metadata,
DatabaseInformation existingDatabase,
ExecutionOptions options,
Dialect dialect,
Formatter formatter,
Set<String> exportIdentifiers,
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace, GenerationTarget[] targets);
protected void createSchemaAndCatalog(
DatabaseInformation existingDatabase,
ExecutionOptions options,
Dialect dialect,
Formatter formatter,
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
if ( tryToCreateCatalogs || tryToCreateSchemas ) {
if ( tryToCreateCatalogs ) {
final Identifier catalogLogicalName = namespace.getName().getCatalog();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName )
&& !existingDatabase.catalogExists( catalogLogicalName ) ) {
applySqlStrings(
false,
dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ),
formatter,
options,
targets
);
exportedCatalogs.add( catalogLogicalName );
}
}
if ( tryToCreateSchemas
&& namespace.getPhysicalName().getSchema() != null
&& !existingDatabase.schemaExists( namespace.getName() ) ) {
applySqlStrings(
false,
dialect.getCreateSchemaCommand( namespace.getPhysicalName().getSchema().render( dialect ) ),
formatter,
options,
targets
);
}
}
}
private void applyConstraintDropping(
Namespace namespace,
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
final Dialect dialect = metadata.getDatabase().getJdbcEnvironment().getDialect();
if ( !dialect.dropConstraints() ) {
return;
}
for ( Table table : namespace.getTables() ) {
if ( !table.isPhysicalTable() ) {
continue;
}
if ( !schemaFilter.includeTable( table ) ) {
continue;
}
final Iterator fks = table.getForeignKeyIterator();
while ( fks.hasNext() ) {
final ForeignKey foreignKey = (ForeignKey) fks.next();
applySqlStrings(
dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, metadata ),
formatter,
options,
targets
);
}
}
}
@Override
public void registerExportables(Database database) {
final Namespace namespace = database.locateNamespace(
logicalQualifiedSequenceName.getCatalogName(),
logicalQualifiedSequenceName.getSchemaName()
);
Sequence sequence = namespace.locateSequence( logicalQualifiedSequenceName.getObjectName() );
if ( sequence != null ) {
sequence.validate( 1, 1 );
}
else {
sequence = namespace.createSequence(
logicalQualifiedSequenceName.getObjectName(),
1,
1
);
}
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
sequence.getName(),
dialect
);
this.sql = jdbcEnvironment.getDialect().getSequenceNextValString( sequenceName );
}
@Override
public java.util.Collection<Table> collectTableMappings() {
ArrayList<Table> tables = new ArrayList<>();
for ( Namespace namespace : getDatabase().getNamespaces() ) {
tables.addAll( namespace.getTables() );
}
return tables;
}
@Override
public Table addTable(
String schemaName,
String catalogName,
String name,
String subselectFragment,
boolean isAbstract) {
final Namespace namespace = getDatabase().locateNamespace(
getDatabase().toIdentifier( catalogName ),
getDatabase().toIdentifier( schemaName )
);
// annotation binding depends on the "table name" for @Subselect bindings
// being set into the generated table (mainly to avoid later NPE), but for now we need to keep that :(
final Identifier logicalName;
if ( name != null ) {
logicalName = getDatabase().toIdentifier( name );
}
else {
logicalName = null;
}
if ( subselectFragment != null ) {
return new Table( namespace, logicalName, subselectFragment, isAbstract );
}
else {
Table table = namespace.locateTable( logicalName );
if ( table != null ) {
if ( !isAbstract ) {
table.setAbstract( false );
}
return table;
}
return namespace.createTable( logicalName, isAbstract );
}
}
@Override
public Table addDenormalizedTable(
String schemaName,
String catalogName,
String name,
boolean isAbstract,
String subselectFragment,
Table includedTable) throws DuplicateMappingException {
final Namespace namespace = getDatabase().locateNamespace(
getDatabase().toIdentifier( catalogName ),
getDatabase().toIdentifier( schemaName )
);
// annotation binding depends on the "table name" for @Subselect bindings
// being set into the generated table (mainly to avoid later NPE), but for now we need to keep that :(
final Identifier logicalName;
if ( name != null ) {
logicalName = getDatabase().toIdentifier( name );
}
else {
logicalName = null;
}
if ( subselectFragment != null ) {
return new DenormalizedTable( namespace, logicalName, subselectFragment, isAbstract, includedTable );
}
else {
Table table = namespace.locateTable( logicalName );
if ( table != null ) {
throw new DuplicateMappingException( DuplicateMappingException.Type.TABLE, logicalName.toString() );
}
else {
table = namespace.createDenormalizedTable( logicalName, isAbstract, includedTable );
}
return table;
}
}
@Override
public java.util.Collection<Table> collectTableMappings() {
ArrayList<Table> tables = new ArrayList<>();
for ( Namespace namespace : database.getNamespaces() ) {
tables.addAll( namespace.getTables() );
}
return tables;
}
/**
* Create indexes for {@code @Index} annotations
* @param queryEntity
* @param context
*/
private void addUserIndexes(QueryEntity queryEntity, SchemaDefinitionContext context, String tableName) {
Namespace namespace = context.getDatabase().getDefaultNamespace();
Optional<Table> tableOptional = namespace.getTables().stream().filter( currentTable -> currentTable.getName().equals( tableName ) ).findFirst();
if ( tableOptional.isPresent() ) {
Table table = tableOptional.get();
for ( Iterator<Index> indexIterator = table.getIndexIterator(); indexIterator.hasNext(); ) {
Index index = indexIterator.next();
appendIndex( queryEntity, index, context );
}
}
}
@SuppressWarnings("unchecked")
private void addTableInfo(QueryEntity queryEntity, SchemaDefinitionContext context, String tableName) {
Namespace namespace = context.getDatabase().getDefaultNamespace();
Optional<Table> tableOptional = namespace.getTables().stream().filter( currentTable -> currentTable.getName().equals( tableName ) ).findFirst();
if ( tableOptional.isPresent() ) {
Table table = tableOptional.get();
for ( Iterator<Column> columnIterator = table.getColumnIterator(); columnIterator.hasNext(); ) {
Column currentColumn = columnIterator.next();
String fieldType = fieldType( currentColumn );
queryEntity.addQueryField( StringHelper.realColumnName( currentColumn.getName() ), fieldType, null );
}
}
}
@Test
public void testDatabaseMetadata() {
for(Namespace namespace : MetadataExtractorIntegrator.INSTANCE.getDatabase().getNamespaces()) {
for( Table table : namespace.getTables()) {
LOGGER.info( "Table {} has the following columns: {}",
table,
StreamSupport.stream(
Spliterators.spliteratorUnknownSize( table.getColumnIterator(), Spliterator.ORDERED), false)
.collect( Collectors.toList()) );
}
}
}
public Table(Namespace namespace, String subselect, boolean isAbstract) {
this.catalog = namespace.getPhysicalName().getCatalog();
this.schema = namespace.getPhysicalName().getSchema();
this.subselect = subselect;
this.isAbstract = isAbstract;
}
public DenormalizedTable(Namespace namespace, Identifier physicalTableName, boolean isAbstract, Table includedTable) {
super( namespace, physicalTableName, isAbstract );
this.includedTable = includedTable;
includedTable.setHasDenormalizedTables();
}
public DenormalizedTable(Namespace namespace, String subselect, boolean isAbstract, Table includedTable) {
super( namespace, subselect, isAbstract );
this.includedTable = includedTable;
includedTable.setHasDenormalizedTables();
}
@Override
public boolean schemaExists(Namespace.Name namespace) {
return extractor.schemaExists( namespace.getCatalog(), namespace.getSchema() );
}
@Override
public TableInformation getTableInformation(
Namespace.Name namespace,
Identifier tableName) {
return getTableInformation( new QualifiedTableName( namespace, tableName ) );
}
@Override
public NameSpaceTablesInformation getTablesInformation(Namespace namespace) {
return extractor.getTables( namespace.getPhysicalName().getCatalog(), namespace.getPhysicalName().getSchema() );
}
@Override
public SequenceInformation getSequenceInformation(Namespace.Name schemaName, Identifier sequenceName) {
return getSequenceInformation( new QualifiedSequenceName( schemaName, sequenceName ) );
}
protected abstract void validateTables(
Metadata metadata,
DatabaseInformation databaseInformation,
ExecutionOptions options,
Dialect dialect, Namespace namespace);
@Override
public boolean includeNamespace( Namespace namespace ) {
return true;
}
@Override
public void registerExportables(Database database) {
final Dialect dialect = database.getJdbcEnvironment().getDialect();
final Namespace namespace = database.locateNamespace(
qualifiedTableName.getCatalogName(),
qualifiedTableName.getSchemaName()
);
Table table = namespace.locateTable( qualifiedTableName.getObjectName() );
if ( table == null ) {
table = namespace.createTable( qualifiedTableName.getObjectName(), false );
// todo : note sure the best solution here. do we add the columns if missing? other?
final Column segmentColumn = new ExportableColumn(
database,
table,
segmentColumnName,
StringType.INSTANCE,
dialect.getTypeName( Types.VARCHAR, segmentValueLength, 0, 0 )
);
segmentColumn.setNullable( false );
table.addColumn( segmentColumn );
// lol
table.setPrimaryKey( new PrimaryKey( table ) );
table.getPrimaryKey().addColumn( segmentColumn );
final Column valueColumn = new ExportableColumn(
database,
table,
valueColumnName,
LongType.INSTANCE
);
table.addColumn( valueColumn );
}
// allow physical naming strategies a chance to kick in
this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
dialect
);
table.addInitCommand( generateInsertInitCommand() );
this.selectQuery = buildSelectQuery( dialect );
this.updateQuery = buildUpdateQuery();
this.insertQuery = buildInsertQuery();
}
@Override
public void registerExportables(Database database) {
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final Namespace namespace = database.locateNamespace(
logicalQualifiedTableName.getCatalogName(),
logicalQualifiedTableName.getSchemaName()
);
Table table = namespace.locateTable( logicalQualifiedTableName.getObjectName() );
if ( table == null ) {
table = namespace.createTable( logicalQualifiedTableName.getObjectName(), false );
}
this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
dialect
);
this.valueColumnNameText = logicalValueColumnNameIdentifier.render( dialect );
this.selectQuery = "select " + valueColumnNameText + " as id_val" +
" from " + dialect.appendLockHint( LockMode.PESSIMISTIC_WRITE, tableNameText ) +
dialect.getForUpdateString();
this.updateQuery = "update " + tableNameText +
" set " + valueColumnNameText + "= ?" +
" where " + valueColumnNameText + "=?";
ExportableColumn valueColumn = new ExportableColumn(
database,
table,
valueColumnNameText,
LongType.INSTANCE
);
table.addColumn( valueColumn );
table.addInitCommand(
new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
);
}