Skip to content

Commit

Permalink
#63 - Flesh out BindingCoordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 3, 2023
1 parent 4bb7099 commit 4bdfb30
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.models.internal.CollectionHelper;
import org.hibernate.models.orm.bind.spi.BindingState;
import org.hibernate.models.orm.bind.spi.TableReference;
import org.hibernate.models.orm.categorize.spi.FilterDefRegistration;
import org.hibernate.models.source.spi.ClassDetails;
import org.hibernate.type.spi.TypeConfiguration;
Expand All @@ -31,7 +32,7 @@
public class BindingStateImpl implements BindingState {
private final MetadataBuildingContext metadataBuildingContext;

private Map<String, PhysicalTable> physicalTableMap;
private Map<String, TableReference> tableMap;
private Map<String, InLineView> virtualTableBindingMap;

public BindingStateImpl(MetadataBuildingContext metadataBuildingContext) {
Expand All @@ -47,61 +48,74 @@ public Database getDatabase() {
return getMetadataBuildingContext().getMetadataCollector().getDatabase();
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PhysicalTable

@Override
public int getPhysicalTableCount() {
return physicalTableMap == null ? 0 : physicalTableMap.size();
public int getTableCount() {
return tableMap == null ? 0 : tableMap.size();
}

@Override
public void forEachPhysicalTable(NamedConsumer<PhysicalTable> consumer) {
if ( physicalTableMap != null ) {
public void forEachTable(NamedConsumer<TableReference> consumer) {
if ( tableMap != null ) {
//noinspection unchecked
physicalTableMap.forEach( (BiConsumer<? super String, ? super PhysicalTable>) consumer );
tableMap.forEach( (BiConsumer<? super String, ? super TableReference>) consumer );
}
}

@Override
public PhysicalTable getPhysicalTableByName(String name) {
if ( physicalTableMap == null ) {
public <T extends TableReference> T getTableByName(String name) {
if ( tableMap == null ) {
return null;
}
return physicalTableMap.get( name );
//noinspection unchecked
return (T) tableMap.get( name );
}

@Override
public PhysicalTable getPhysicalTableByPhysicalName(String name) {
if ( physicalTableMap != null ) {
for ( Map.Entry<String, PhysicalTable> entry : physicalTableMap.entrySet() ) {
if ( entry.getValue().physicalName().matches( name ) ) {
return entry.getValue();
}
}
public void addTable(TableReference table) {
if ( tableMap == null ) {
tableMap = new HashMap<>();
}
return null;
}

@Override
public void addPhysicalTable(PhysicalTable physicalTable) {
if ( physicalTableMap == null ) {
physicalTableMap = new HashMap<>();
tableMap.put( table.getLogicalName().getCanonicalName(), table );

if ( table instanceof PhysicalTable physicalTable ) {
final Table addedTable = metadataBuildingContext.getMetadataCollector().addTable(
resolveSchemaName( physicalTable.schema() ),
resolveCatalogName( physicalTable.catalog() ),
physicalTable.logicalName().getCanonicalName(),
null,
!table.isExportable(),
metadataBuildingContext
);
addedTable.setComment( physicalTable.comment() );
}
else if ( table instanceof PhysicalView physicalView ) {
final Table addedTable = metadataBuildingContext.getMetadataCollector().addTable(
null,
null,
null,
null,
!physicalView.isExportable(),
metadataBuildingContext
);
addedTable.setViewQuery( physicalView.query() );
}
else if ( table instanceof SecondaryTable secondaryTable ) {
final Table addedTable = metadataBuildingContext.getMetadataCollector().addTable(
resolveSchemaName( secondaryTable.schema() ),
resolveCatalogName( secondaryTable.catalog() ),
secondaryTable.logicalName().getCanonicalName(),
null,
!table.isExportable(),
metadataBuildingContext
);
addedTable.setComment( secondaryTable.comment() );
}
physicalTableMap.put( physicalTable.logicalName().getCanonicalName(), physicalTable );

final Table addedTable = metadataBuildingContext.getMetadataCollector().addTable(
resolveSchemaName( physicalTable.schema() ),
resolveCatalogName( physicalTable.catalog() ),
physicalTable.logicalName().getCanonicalName(),
null,
!physicalTable.isExportable(),
metadataBuildingContext
);
addedTable.setComment( physicalTable.comment() );
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PhysicalTable

private String resolveSchemaName(Identifier explicit) {
if ( explicit != null ) {
return explicit.getCanonicalName();
Expand Down Expand Up @@ -142,36 +156,6 @@ private String resolveCatalogName(Identifier explicit) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VirtualTableBinding

@Override
public int getNumberOfVirtualTableBindings() {
return virtualTableBindingMap == null ? 0 : virtualTableBindingMap.size();
}

@Override
public void forEachVirtualTableBinding(NamedConsumer<InLineView> consumer) {
if ( virtualTableBindingMap != null ) {
//noinspection unchecked
virtualTableBindingMap.forEach( (BiConsumer<? super String, ? super InLineView>) consumer );
}
}

@Override
public InLineView getVirtualTableBindingByName(String name) {
if ( virtualTableBindingMap == null ) {
return null;
}
return virtualTableBindingMap.get( name );
}


@Override
public void addVirtualTableBinding(InLineView binding) {
if ( virtualTableBindingMap == null ) {
virtualTableBindingMap = new HashMap<>();
}
virtualTableBindingMap.put( binding.logicalName().getCanonicalName(), binding );
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Filter def
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package org.hibernate.models.orm.bind.internal;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.models.orm.bind.spi.TableReference;
import org.hibernate.models.orm.bind.spi.PhysicalTableReference;

/**
* Models a physical table from the underlying database schema
Expand All @@ -25,7 +25,7 @@ public record PhysicalTable(
Identifier schema,
boolean isAbstract,
String comment,
String options) implements TableReference {
String options) implements PhysicalTableReference {

@Override
public Identifier getLogicalName() {
Expand All @@ -37,5 +37,18 @@ public boolean isExportable() {
return !isAbstract;
}

@Override
public Identifier getTableName() {
return physicalName;
}

@Override
public Identifier getSchemaName() {
return schema;
}

@Override
public Identifier getCatalogName() {
return catalog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
package org.hibernate.models.orm.bind.internal;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.models.orm.bind.spi.TableReference;
import org.hibernate.models.orm.bind.spi.PersistentTableReference;

/**
* @see org.hibernate.annotations.View
*
* @author Steve Ebersole
*/
public record PhysicalView(Identifier logicalName, String query) implements TableReference {
public record PhysicalView(Identifier logicalName, String query) implements PersistentTableReference {
@Override
public Identifier getLogicalName() {
return null;
return logicalName;
}

@Override
public boolean isExportable() {
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package org.hibernate.models.orm.bind.internal;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.models.orm.bind.spi.TableReference;
import org.hibernate.models.orm.bind.spi.PhysicalTableReference;

/**
* @see jakarta.persistence.SecondaryTable
Expand All @@ -23,7 +23,7 @@ public record SecondaryTable(
String comment,
String options,
boolean owned,
boolean optional) implements TableReference {
boolean optional) implements PhysicalTableReference {
@Override
public Identifier getLogicalName() {
return logicalName;
Expand All @@ -33,4 +33,19 @@ public Identifier getLogicalName() {
public boolean isExportable() {
return !isAbstract;
}

@Override
public Identifier getSchemaName() {
return schema;
}

@Override
public Identifier getCatalogName() {
return catalog;
}

@Override
public Identifier getTableName() {
return physicalName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public MetadataBuildingContext getBuildingContext() {
logicalName,
BindingHelper.getString( subselectAnn, "value", Subselect.class, bindingContext )
);
bindingState.addVirtualTableBinding( binding );
bindingState.addTable( binding );
}

private void processPhysicalTable(EntityTypeMetadata type, AnnotationUsage<jakarta.persistence.Table> tableAnn) {
Expand All @@ -158,7 +158,7 @@ private void processPhysicalTable(EntityTypeMetadata type, AnnotationUsage<jakar
physicalTable = createImplicitPhysicalTable( type );
}

bindingState.addPhysicalTable( physicalTable );
bindingState.addTable( physicalTable );
}

private PhysicalTable createImplicitPhysicalTable(EntityTypeMetadata type) {
Expand Down Expand Up @@ -300,7 +300,7 @@ public MetadataBuildingContext getBuildingContext() {
BindingHelper.getString( secondaryTableAnn, "options", SecondaryTable.class, bindingContext )
);

bindingState.addPhysicalTable( physicalTable );
bindingState.addTable( physicalTable );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import org.hibernate.boot.model.relational.Database;
import org.hibernate.internal.util.NamedConsumer;
import org.hibernate.models.orm.bind.internal.InLineView;
import org.hibernate.models.orm.bind.internal.PhysicalTable;
import org.hibernate.models.orm.categorize.spi.FilterDefRegistration;

/**
Expand All @@ -18,16 +16,11 @@
public interface BindingState {
Database getDatabase();

int getPhysicalTableCount();
void forEachPhysicalTable(NamedConsumer<PhysicalTable> consumer);
PhysicalTable getPhysicalTableByName(String name);
PhysicalTable getPhysicalTableByPhysicalName(String name);
void addPhysicalTable(PhysicalTable physicalTable);
int getTableCount();
void forEachTable(NamedConsumer<TableReference> consumer);
<T extends TableReference> T getTableByName(String name);
void addTable(TableReference table);

int getNumberOfVirtualTableBindings();
void forEachVirtualTableBinding(NamedConsumer<InLineView> consumer);
InLineView getVirtualTableBindingByName(String name);
void addVirtualTableBinding(InLineView binding);

void apply(FilterDefRegistration registration);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.models.orm.bind.spi;

/**
* @author Steve Ebersole
*/
public interface PersistentTableReference extends TableReference {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.models.orm.bind.spi;

import org.hibernate.boot.model.naming.Identifier;

/**
* @author Steve Ebersole
*/
public interface PhysicalTableReference extends PersistentTableReference, SchemaAware {
Identifier getTableName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.models.orm.bind.spi;

import org.hibernate.boot.model.naming.Identifier;

/**
* @author Steve Ebersole
*/
public interface SchemaAware {
Identifier getSchemaName();
Identifier getCatalogName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void testIt(ServiceRegistryScope scope) {
assertThat( nameParamJdbcMapping ).isNotNull();
assertThat( nameParamJdbcMapping.getJdbcJavaType().getJavaType() ).isEqualTo( String.class );

assertThat( bindingState.getPhysicalTableCount() ).isEqualTo( 2 );
assertThat( bindingState.getTableCount() ).isEqualTo( 2 );

final PhysicalTable simpletonsTable = bindingState.getPhysicalTableByName( "simpletons" );
final PhysicalTable simpletonsTable = bindingState.getTableByName( "simpletons" );
assertThat( simpletonsTable.logicalName().render() ).isEqualTo( "simpletons" );
assertThat( simpletonsTable.physicalName().render() ).isEqualTo( "simpletons" );
assertThat( simpletonsTable.logicalName().getCanonicalName() ).isEqualTo( "simpletons" );
Expand All @@ -66,7 +66,7 @@ void testIt(ServiceRegistryScope scope) {
assertThat( simpletonsTable.schema() ).isNull();
assertThat( simpletonsTable.comment() ).isEqualTo( "Stupid is as stupid does" );

final PhysicalTable simpleStuffTable = bindingState.getPhysicalTableByName( "simple_stuff" );
final PhysicalTable simpleStuffTable = bindingState.getTableByName( "simple_stuff" );
assertThat( simpleStuffTable.logicalName().render() ).isEqualTo( "simple_stuff" );
assertThat( simpleStuffTable.physicalName().render() ).isEqualTo( "simple_stuff" );
assertThat( simpleStuffTable.logicalName().getCanonicalName() ).isEqualTo( "simple_stuff" );
Expand Down
Loading

0 comments on commit 4bdfb30

Please sign in to comment.