HHH-7050 : back out of changes

This commit is contained in:
Gail Badner 2012-02-16 16:33:43 -08:00
parent f5679281e0
commit 2115f1f79c
9 changed files with 45 additions and 240 deletions

View File

@ -256,7 +256,6 @@ public class Binder {
bindSecondaryTables( entitySource, entityBinding );
Deque<TableSpecification> tableStack = new ArrayDeque<TableSpecification>( );
tableStack.add( entityBinding.getPrimaryTable() );
bindAttributes( entitySource, entityBinding, tableStack );
bindTableUniqueConstraints( entitySource, entityBinding );
@ -385,6 +384,7 @@ public class Binder {
final EntityBinding entityBinding = buildBasicEntityBinding( entitySource, superEntityBinding );
entityBinding.setPrimaryTable( superEntityBinding.getPrimaryTable() );
entityBinding.setPrimaryTableName( superEntityBinding.getPrimaryTableName() );
bindDiscriminatorValue( entitySource, entityBinding );
return entityBinding;
@ -989,10 +989,12 @@ public class Binder {
final String tableName = attributeSource.getExplicitCollectionTableName();
if ( StringHelper.isNotEmpty( tableName ) ) {
final String table = currentBindingContext.getNamingStrategy().tableName( tableName );
Table collectionTable = schema.locateTable( table );
final Identifier tableIdentifier = Identifier.toIdentifier(
currentBindingContext.getNamingStrategy().tableName( tableName )
);
Table collectionTable = schema.locateTable( tableIdentifier );
if ( collectionTable == null ) {
collectionTable = schema.createTable( Identifier.toIdentifier( tableName ), true );
collectionTable = schema.createTable( tableIdentifier );
}
pluralAttributeBinding.setCollectionTable( collectionTable );
}
@ -1000,7 +1002,7 @@ public class Binder {
// todo : not sure wel have all the needed info here in all cases, specifically when needing to know the "other side"
final EntityBinding owner = pluralAttributeBinding.getContainer().seekEntityBinding();
final String ownerTableLogicalName = Table.class.isInstance( owner.getPrimaryTable() )
? owner.getPrimaryTable().getLogicalName()
? Table.class.cast( owner.getPrimaryTable() ).getTableName().getName()
: null;
String collectionTableName = currentBindingContext.getNamingStrategy().collectionTableName(
owner.getEntity().getName(),
@ -1010,13 +1012,11 @@ public class Binder {
pluralAttributeBinding.getContainer().getPathBase() + '.' + attributeSource.getName()
);
collectionTableName = quoteIdentifier( collectionTableName );
// TODO: may not know physical collection table name if this is an association;
// for now, assume that the physical collection table name is known at this point;
boolean isPhysicalCollectionTable = true;
pluralAttributeBinding.setCollectionTable(
schema.locateOrCreateTable(
collectionTableName,
isPhysicalCollectionTable
Identifier.toIdentifier(
collectionTableName
)
)
);
}
@ -1037,9 +1037,11 @@ public class Binder {
AbstractPluralAttributeBinding pluralAttributeBinding,
Deque<TableSpecification> tableStack) {
// TableSpecification targetTable = tableStack.peekLast();
pluralAttributeBinding.getPluralAttributeKeyBinding().prepareForeignKey(
attributeSource.getKeySource().getExplicitForeignKeyName(),
tableStack.peekLast().getLogicalName()
//tableStack.peekLast().getLogicalName()
null // todo : handle secondary table names
);
pluralAttributeBinding.getPluralAttributeKeyBinding().getForeignKey().setDeleteRule(
attributeSource.getKeySource().getOnDeleteAction()
@ -1289,6 +1291,7 @@ public class Binder {
final TableSource tableSource = entitySource.getPrimaryTable();
final Table table = createTable( entityBinding, tableSource );
entityBinding.setPrimaryTable( table );
entityBinding.setPrimaryTableName( table.getTableName().getName() );
}
private void bindSecondaryTables(EntitySource entitySource, EntityBinding entityBinding) {
@ -1317,7 +1320,7 @@ public class Binder {
return currentBindingContext.getMetadataImplementor()
.getDatabase()
.locateSchema( databaseSchemaName )
.locateOrCreateTable( tableName, true );
.locateOrCreateTable( Identifier.toIdentifier( tableName ) );
}
private void bindTableUniqueConstraints(EntitySource entitySource, EntityBinding entityBinding) {

View File

@ -81,7 +81,7 @@ public class TableProcessor {
String tableName = JandexHelper.getValue( tableAnnotation, "appliesTo", String.class );
ObjectName objectName = new ObjectName( tableName );
Schema schema = metadata.getDatabase().getSchema( objectName.getSchema(), objectName.getCatalog() );
Table table = schema.locateTable( tableName );
Table table = schema.locateTable( objectName.getName() );
if ( table != null ) {
bindHibernateTableAnnotation( table, tableAnnotation );
}

View File

@ -231,7 +231,7 @@ public class EntityBinding implements AttributeBindingContainer {
}
public TableSpecification locateTable(String tableName) {
if ( tableName == null || tableName.equals( getPrimaryTableLogicalName() ) ) {
if ( tableName == null || tableName.equals( getPrimaryTableName() ) ) {
return primaryTable;
}
TableSpecification tableSpec = secondaryTables.get( tableName );
@ -246,8 +246,8 @@ public class EntityBinding implements AttributeBindingContainer {
}
return tableSpec;
}
public String getPrimaryTableLogicalName() {
return primaryTable.getLogicalName();
public String getPrimaryTableName() {
return primaryTableName;
}
public void setPrimaryTableName(String primaryTableName) {

View File

@ -53,11 +53,6 @@ public class InLineView extends AbstractTableSpecification {
return select;
}
@Override
public String getLogicalName() {
return logicalName;
}
@Override
public String getLoggableValueQualifier() {
return logicalName;

View File

@ -34,7 +34,7 @@ import java.util.Map;
public class Schema {
private final Name name;
private Map<String, InLineView> inLineViews = new HashMap<String, InLineView>();
private Map<String, Table> tables = new HashMap<String, Table>();
private Map<Identifier, Table> tables = new HashMap<Identifier, Table>();
public Schema(Name name) {
this.name = name;
@ -48,72 +48,20 @@ public class Schema {
return name;
}
/**
* Returns the table with the specified logical table name.
* .
* @param logicalTableName
*
* @return the table with the specified logical table name,
* or null if there is no table with the specified
* logical table name.
*/
public Table locateTable(String logicalTableName) {
return tables.get( logicalTableName );
public Table locateTable(Identifier name) {
return tables.get( name );
}
/**
* Creates a {@link Table} with the specified logical name. If
* {@code isPhysicalName} is true, then {@code name} is also
* the phycical table name.
*
* @param name - the name of the table
* @param isPhysicalName - true, if the name is known to be the
* physical name.
* @return the created table.
*/
public Table createTable(Identifier name, boolean isPhysicalName) {
Table table = new Table( this, name, isPhysicalName );
tables.put( table.getLogicalName(), table );
public Table createTable(Identifier name) {
Table table = new Table( this, name );
tables.put( name, table );
return table;
}
/* package-protected */
void remapLogicalTableName(String oldName) {
Table table = tables.remove( oldName );
if ( table == null ) {
throw new IllegalStateException(
String.format(
"Schema (%s) does not contain a table with logical name (%s) to remap.",
name,
oldName
)
);
}
tables.put( table.getLogicalName(), table );
}
/**
* Locates the table with the specified name; if none is found,
* a table with the specified name is created.
* <p/>
* The value for {@code isPhysicalTableName} is ignored if a table
* is located with the specified name, and that name is defined
* as the physical table name (indicated by a non-null value returned
* by {@link Table#getTableName()}.
*
* @param name - the name
* @param isPhysicalTableName - true, if the table is known to be
* the physical table name.
* @return the located or created table.
*/
public Table locateOrCreateTable(String name, boolean isPhysicalTableName) {
public Table locateOrCreateTable(Identifier name) {
final Table existing = locateTable( name );
Identifier tableIdentifier = Identifier.toIdentifier( name );
if ( existing == null ) {
return createTable( tableIdentifier, isPhysicalTableName );
}
else if ( isPhysicalTableName && existing.getTableName() == null ) {
existing.setPhysicalName( tableIdentifier );
return createTable( name );
}
return existing;
}

View File

@ -34,54 +34,27 @@ import org.hibernate.dialect.Dialect;
*
* @author Gavin King
* @author Steve Ebersole
* @author Gail Badner
*/
public class Table extends AbstractTableSpecification implements Exportable {
private final Schema database;
private String logicalName;
private Identifier name;
private boolean isPhysicalName;
private ObjectName objectName;
private String qualifiedName;
private final Identifier tableName;
private final ObjectName objectName;
private final String qualifiedName;
private final LinkedHashMap<String,Index> indexes = new LinkedHashMap<String,Index>();
private final LinkedHashMap<String,UniqueKey> uniqueKeys = new LinkedHashMap<String,UniqueKey>();
private final List<CheckConstraint> checkConstraints = new ArrayList<CheckConstraint>();
private final List<String> comments = new ArrayList<String>();
/**
* Constructs a {@link Table} instance.
*
* @param database - the schema
* @param name - the name
* @param isPhysicalTableName - true, if the name is known to be the physical
* table name; false, otherwise.
*/
public Table(Schema database, String name, boolean isPhysicalTableName) {
this( database, Identifier.toIdentifier( name ), isPhysicalTableName );
public Table(Schema database, String tableName) {
this( database, Identifier.toIdentifier( tableName ) );
}
/**
* Constructs a {@link Table} instance.
*
* @param database - the schema
* @param name - the name {@link Identifier}
* @param isPhysicalTableName - true, if the name is known to be the physical
* table name; false, otherwise.
*/
public Table(Schema database, Identifier name, boolean isPhysicalTableName) {
public Table(Schema database, Identifier tableName) {
this.database = database;
setName( name, isPhysicalTableName );
}
private static String createLogicalName(Identifier tableName) {
return tableName.isQuoted() ?
new StringBuilder( tableName.getName().length() + 2 )
.append( '`' )
.append( tableName.getName() )
.append( '`' ).toString() :
tableName.getName();
this.tableName = tableName;
objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), tableName );
this.qualifiedName = objectName.toText();
}
@Override
@ -89,51 +62,8 @@ public class Table extends AbstractTableSpecification implements Exportable {
return database;
}
/**
* Gets the logical table name.
*
* @return the logical table name.
*/
public String getLogicalName() {
return logicalName;
}
/**
* Returns the physical table name.
*
* @return the physical table name, or null if the physical
* table name is not defined.
*/
public Identifier getTableName() {
return isPhysicalName ? name : null;
}
/**
* Sets the physical table name.
*
* @param physicalName - the physical table name
*/
public final void setPhysicalName(Identifier physicalName) {
setName( physicalName, true );
}
private void setName(Identifier name, boolean isPhysicalName) {
if ( name == null ) {
throw new IllegalArgumentException( "name cannot be null." );
}
if ( isPhysicalName && ! this.isPhysicalName ) {
this.isPhysicalName = isPhysicalName;
}
if ( ! name.equals( this.name ) ) {
String logicalNameOld = logicalName;
this.name = name;
this.logicalName = createLogicalName( name );
objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), name );
this.qualifiedName = objectName.toText();
if ( logicalNameOld != null ) {
database.remapLogicalTableName( logicalNameOld );
}
}
return tableName;
}
@Override

View File

@ -45,13 +45,6 @@ public interface TableSpecification extends ValueContainer, Loggable {
*/
public int getTableNumber();
/**
* Get logical table name
*
* @return the logical table name.
*/
public String getLogicalName();
/**
* Get the primary key definition for this table spec.
*

View File

@ -56,7 +56,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
@Test
public void testBasicMiddleOutBuilding() {
Table table = new Table( new Schema( null, null ), "the_table", true );
Table table = new Table( new Schema( null, null ), "the_table" );
Column idColumn = table.locateOrCreateColumn( "id" );
idColumn.setJdbcDataType( BIGINT );
idColumn.setSize( Size.precision( 18, 0 ) );

View File

@ -35,7 +35,6 @@ import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
@ -48,7 +47,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
@Test
public void testTableCreation() {
Schema schema = new Schema( null, null );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), true );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) );
assertNull( table.getSchema().getName().getSchema() );
assertNull( table.getSchema().getName().getCatalog() );
assertEquals( "my_table", table.getTableName().toString() );
@ -92,10 +91,10 @@ public class TableManipulationTests extends BaseUnitTestCase {
@Test
public void testTableSpecificationCounter() {
Schema schema = new Schema( null, null );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), true );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) );
InLineView inLineView = schema.createInLineView( "my_inlineview", "subselect" );
InLineView otherInLineView = schema.createInLineView( "my_other_inlineview", "other subselect" );
Table otherTable = schema.createTable( Identifier.toIdentifier( "my_other_table" ), true );
Table otherTable = schema.createTable( Identifier.toIdentifier( "my_other_table" ) );
int firstTableNumber = table.getTableNumber();
assertEquals( firstTableNumber, table.getTableNumber() );
@ -107,7 +106,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
@Test
public void testBasicForeignKeyDefinition() {
Schema schema = new Schema( null, null );
Table book = schema.createTable( Identifier.toIdentifier( "BOOK" ), true );
Table book = schema.createTable( Identifier.toIdentifier( "BOOK" ) );
Column bookId = book.locateOrCreateColumn( "id" );
bookId.setJdbcDataType( INTEGER );
@ -115,7 +114,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
book.getPrimaryKey().addColumn( bookId );
book.getPrimaryKey().setName( "BOOK_PK" );
Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ), true );
Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ) );
Column pageId = page.locateOrCreateColumn( "id" );
pageId.setJdbcDataType( INTEGER );
@ -137,12 +136,12 @@ public class TableManipulationTests extends BaseUnitTestCase {
public void testQualifiedName() {
Dialect dialect = new H2Dialect();
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), true );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) );
assertEquals( "my_table", table.getTableName().getName() );
assertEquals( "my_table", table.getTableName().toString() );
assertEquals( "schema.\"catalog\".my_table", table.getQualifiedName( dialect ) );
table = schema.createTable( Identifier.toIdentifier( "`my_table`" ), true );
table = schema.createTable( Identifier.toIdentifier( "`my_table`" ) );
assertEquals( "my_table", table.getTableName().getName() );
assertEquals( "`my_table`", table.getTableName().toString() );
assertEquals( "schema.\"catalog\".\"my_table\"", table.getQualifiedName( dialect ) );
@ -150,67 +149,4 @@ public class TableManipulationTests extends BaseUnitTestCase {
InLineView inLineView = schema.createInLineView( "my_inlineview", "select ..." );
assertEquals( "( select ... )", inLineView.getQualifiedName( dialect ) );
}
@Test
public void testTableLogicalName() {
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), false );
assertEquals( "my_table", table.getLogicalName() );
assertNull( table.getTableName() );
assertSame( table, schema.locateTable( table.getLogicalName() ) );
table.setPhysicalName( Identifier.toIdentifier( "my_new_table" ) );
assertEquals( "my_new_table", table.getLogicalName() );
assertEquals( "my_new_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( "my_new_table" ) );
assertNull( schema.locateTable( "my_table" ) );
table.setPhysicalName( Identifier.toIdentifier( "my_newer_table" ) );
assertEquals( "my_newer_table", table.getLogicalName() );
assertEquals( "my_newer_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( "my_newer_table" ) );
assertNull( schema.locateTable( "my_new_table" ) );
}
@Test
public void testQuotedTableLogicalName() {
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.createTable( Identifier.toIdentifier( "`my_table`" ), false );
assertEquals( "`my_table`", table.getLogicalName() );
assertNull( table.getTableName() );
assertSame( table, schema.locateTable( table.getLogicalName() ) );
table.setPhysicalName( Identifier.toIdentifier( "`my_new_table`" ) );
assertEquals( "`my_new_table`", table.getLogicalName() );
assertEquals( "my_new_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( "`my_new_table`" ) );
assertNull( schema.locateTable( "`my_table`" ) );
table.setPhysicalName( Identifier.toIdentifier( "`my_newer_table`" ) );
assertEquals( "`my_newer_table`", table.getLogicalName() );
assertEquals( "my_newer_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( "`my_newer_table`" ) );
assertNull( schema.locateTable( "`my_new_table`" ) );
}
@Test
public void testInLineViewLogicalName() {
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
InLineView view = schema.createInLineView( "my_view", "select" );
assertEquals( "my_view", view.getLogicalName() );
assertEquals( "select", view.getSelect() );
assertSame( view, schema.getInLineView( view.getLogicalName() ) );
}
@Test
public void testLocateOrCreateTable() {
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.locateOrCreateTable( "my_table", false );
assertEquals( "my_table", table.getLogicalName() );
assertNull( table.getTableName() );
Table tableTemp = schema.locateOrCreateTable( "my_table", true );
assertSame( table, tableTemp );
assertEquals( "my_table", table.getLogicalName() );
assertEquals( Identifier.toIdentifier( "my_table" ), table.getTableName() );
tableTemp = schema.locateOrCreateTable( "my_table", false );
assertSame( table, tableTemp );
assertEquals( "my_table", table.getLogicalName() );
assertEquals( Identifier.toIdentifier( "my_table" ), table.getTableName() );
}
}