HHH-6438 made Constraint#name an Identifier

This commit is contained in:
Brett Meyer 2014-03-21 16:12:09 -04:00
parent 9504c102cf
commit 1c222d1b1e
26 changed files with 83 additions and 53 deletions

View File

@ -50,7 +50,7 @@ public class DB2UniqueKeyExporter extends StandardUniqueKeyExporter {
( (Table) constraint.getTable() ).getTableName() );
StringBuilder sb = new StringBuilder()
.append( "create unique index " )
.append( constraint.getName() )
.append( constraint.getName().getText( dialect ) )
.append( " on " )
.append( tableName )
.append( " (" );
@ -73,7 +73,7 @@ public class DB2UniqueKeyExporter extends StandardUniqueKeyExporter {
@Override
public String[] getSqlDropStrings(Constraint constraint, JdbcEnvironment jdbcEnvironment) {
if ( hasNullable( constraint ) ) {
return new String[] { "drop index " + constraint.getName() };
return new String[] { "drop index " + constraint.getName().getText( dialect ) };
}
else {
return super.getSqlDropStrings( constraint, jdbcEnvironment );

View File

@ -67,7 +67,7 @@ public class InformixUniqueKeyExporter extends StandardUniqueKeyExporter {
sb.append( ")" )
.append( " constraint " )
.append( constraint.getName() );
.append( constraint.getName().getText( dialect ) );
return new String[] { sb.toString() };
}
}

View File

@ -59,7 +59,7 @@ public class MySQLIndexExporter extends StandardIndexExporter {
StringBuilder sb = new StringBuilder();
sb.append( "drop index " );
sb.append( ( dialect.qualifyIndexName()
? StringHelper.qualify( tableName, index.getName() ) : index.getName() ) );
? index.getName().getQualifiedText( tableName, dialect ) : index.getName() ) );
sb.append( " on " + tableName );
return new String[] { sb.toString() };

View File

@ -57,7 +57,7 @@ public class MySQLUniqueKeyExporter extends StandardUniqueKeyExporter {
if ( dialect.supportsIfExistsBeforeConstraintName() ) {
sb.append( "if exists " );
}
sb.append( dialect.quote( constraint.getName() ) );
sb.append( constraint.getName().getText( dialect ) );
if ( dialect.supportsIfExistsAfterConstraintName() ) {
sb.append( " if exists" );
}

View File

@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.Identifier;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.UniqueKey;
@ -60,7 +61,7 @@ public class NaturalIdUniqueKeyHelper {
String keyName = "UK_" + HashedNameUtil.hashedName( table.getLogicalName().getText() + "_NaturalID" );
uniqueKey = new UniqueKey();
uniqueKey.setTable( table );
uniqueKey.setName( keyName );
uniqueKey.setName( Identifier.toIdentifier( keyName ) );
table.addUniqueKey( uniqueKey );
naturalIdUniqueKeys.put( table, uniqueKey );
}

View File

@ -222,7 +222,7 @@ public class TableHelper {
final String normalizedName = normalizeDatabaseIdentifier( name, new UniqueKeyNamingStrategyHelper( table, columns ) );
uk.setName( normalizedName );
uk.setName( Identifier.toIdentifier( normalizedName ) );
table.addUniqueKey( uk );
}
@ -239,7 +239,7 @@ public class TableHelper {
final String normalizedName = normalizeDatabaseIdentifier( name, new IndexNamingStrategyHelper( table, columns ) );
idx.setName( normalizedName );
idx.setName( Identifier.toIdentifier( normalizedName ) );
table.addIndex( idx );
}

View File

@ -44,11 +44,11 @@ import org.hibernate.internal.util.StringHelper;
*/
public abstract class AbstractConstraint implements Constraint {
private TableSpecification table;
private String name;
private Identifier name;
private final Map<Identifier, Column> columnMap = new LinkedHashMap<Identifier, Column>();
private final Map<Column, String> columnOrderMap = new HashMap<Column, String>();
protected AbstractConstraint(TableSpecification table, String name) {
protected AbstractConstraint(TableSpecification table, Identifier name) {
this.table = table;
this.name = name;
}
@ -67,7 +67,7 @@ public abstract class AbstractConstraint implements Constraint {
*
* @return the constraint name, or null if the name has not been set
*/
public String getName() {
public Identifier getName() {
return name;
}
@ -80,7 +80,7 @@ public abstract class AbstractConstraint implements Constraint {
* @throws IllegalArgumentException if name is null.
* @throws IllegalStateException if this constraint already has a non-null name.
*/
public void setName(String name) {
public void setName(Identifier name) {
if ( name == null ) {
throw new IllegalArgumentException( "name must be non-null." );
}

View File

@ -167,6 +167,12 @@ public abstract class AbstractTableSpecification implements TableSpecification {
@Override
public ForeignKey createForeignKey(TableSpecification targetTable, String name, boolean createConstraint) {
Identifier identifier = Identifier.toIdentifier( name );
return createForeignKey( targetTable, identifier, createConstraint );
}
@Override
public ForeignKey createForeignKey(TableSpecification targetTable, Identifier name, boolean createConstraint) {
ForeignKey fk = new ForeignKey( this, targetTable, name, createConstraint );
foreignKeys.add( fk );
return fk;
@ -181,8 +187,9 @@ public abstract class AbstractTableSpecification implements TableSpecification {
if ( name == null ) {
throw new IllegalArgumentException( "name must be non-null." );
}
Identifier identifier = Identifier.toIdentifier( name );
for ( T constraint : constraints ) {
if ( name.equals( constraint.getName() ) ) {
if ( identifier.equals( constraint.getName() ) ) {
return constraint;
}
}

View File

@ -47,7 +47,7 @@ public interface Constraint extends Exportable {
*
* @return the name.
*/
public String getName();
public Identifier getName();
/**
* Obtain a read-only view of the columns that are part of this constraint.

View File

@ -82,16 +82,16 @@ public class DenormalizedTable extends Table {
final String fkNamePostfix = Integer.toHexString( getTableName().hashCode() );
for ( ForeignKey fk : fksInSuperTable ) {
String name = fk.getName();
if ( name == null ) {
Identifier name = fk.getName();
if (name == null || name.getText() == null) {
if(!alreadyCopiedNonNameParentFK.contains( fk )){
copyFK( fk, name );
alreadyCopiedNonNameParentFK.add( fk );
}
}
else {
String fkName = name + fkNamePostfix;
ForeignKey copiedFK = super.locateForeignKey( fkName );
Identifier fkName = name.applyPostfix( fkNamePostfix );
ForeignKey copiedFK = super.locateForeignKey( fkName.toString() );
if ( copiedFK == null ) {
copyFK( fk, fkName );
}
@ -99,7 +99,7 @@ public class DenormalizedTable extends Table {
}
}
private void copyFK(ForeignKey fk, String fkName) {
private void copyFK(ForeignKey fk, Identifier fkName) {
ForeignKey copiedFK = createForeignKey( fk.getTargetTable(), fkName, fk.createConstraint() );
copiedFK.setDeleteRule( fk.getDeleteRule() );
copiedFK.setUpdateRule( fk.getUpdateRule() );

View File

@ -55,7 +55,7 @@ public class ForeignKey extends AbstractConstraint {
private boolean createConstraint;
protected ForeignKey(TableSpecification sourceTable, TableSpecification targetTable, String name,
protected ForeignKey(TableSpecification sourceTable, TableSpecification targetTable, Identifier name,
boolean createConstraint) {
super( sourceTable, name );
if ( targetTable == null ) {
@ -218,7 +218,7 @@ public class ForeignKey extends AbstractConstraint {
@Override
public String getExportIdentifier() {
return getSourceTable().getLoggableValueQualifier() + ".FK-" + getName();
return getSourceTable().getLoggableValueQualifier() + ".FK-" + getName().getText();
}
public boolean createConstraint() {

View File

@ -31,9 +31,10 @@ import org.hibernate.internal.util.StringHelper;
* Models an identifier (name).
*
* @author Steve Ebersole
* @author Brett Meyer
*/
public class Identifier implements Comparable<Identifier> {
private final String text;
private String text;
private final boolean isQuoted;
/**
@ -71,7 +72,7 @@ public class Identifier implements Comparable<Identifier> {
final String trimmed = text.trim();
if ( isQuoted( trimmed ) ) {
final String bareName = trimmed.substring( 1, trimmed.length() - 1 );
return new Identifier( bareName, true );
return new Identifier( bareName, quote );
}
else {
return new Identifier( trimmed, quote );
@ -159,6 +160,22 @@ public class Identifier implements Comparable<Identifier> {
String.valueOf( openQuote ) + text + closeQuote :
text;
}
public String getQualifiedText(String prefix, Dialect dialect) {
String qualified = prefix + "." + text;
return isQuoted ? dialect.openQuote() + qualified + dialect.closeQuote() : qualified;
}
public String getUnqualifiedText(Dialect dialect) {
int loc = text.lastIndexOf(".");
String unqualified = ( loc < 0 ) ? text : text.substring( loc + 1 );
return isQuoted ? dialect.openQuote() + unqualified + dialect.closeQuote() : unqualified;
}
public Identifier applyPostfix(String postfix) {
String newText = text + postfix;
return Identifier.toIdentifier( newText, isQuoted );
}
@Override
public String toString() {

View File

@ -38,7 +38,7 @@ public class Index extends AbstractConstraint {
this( null, null, unique );
}
protected Index(Table table, String name, boolean unique) {
protected Index(Table table, Identifier name, boolean unique) {
super( table, name );
this.unique = unique;
}
@ -47,7 +47,7 @@ public class Index extends AbstractConstraint {
public String getExportIdentifier() {
StringBuilder sb = new StringBuilder( getTable().getLoggableValueQualifier() );
sb.append( ".IDX" );
sb.append( '_' ).append( getName() );
sb.append( '_' ).append( getName().getText() );
for ( Column column : getColumns() ) {
sb.append( '_' ).append( column.getColumnName().getText() );
}

View File

@ -134,6 +134,8 @@ public interface TableSpecification extends ValueContainer, Loggable {
* @return The foreign key reference.
*/
public ForeignKey createForeignKey(TableSpecification targetTable, String name, boolean createConstraint);
public ForeignKey createForeignKey(TableSpecification targetTable, Identifier name, boolean createConstraint);
public Iterable<Index> getIndexes();

View File

@ -37,7 +37,7 @@ public class UniqueKey extends AbstractConstraint {
this( null, null );
}
protected UniqueKey(Table table, String name) {
protected UniqueKey(Table table, Identifier name) {
super( table, name );
}

View File

@ -72,7 +72,7 @@ public class StandardForeignKeyExporter implements Exporter<ForeignKey> {
.append( sourceTableName )
.append(
dialect.getAddForeignKeyConstraintString(
foreignKey.getName(),
foreignKey.getName().getText( dialect ),
columnNames,
targetTableName,
targetColumnNames,
@ -108,7 +108,7 @@ public class StandardForeignKeyExporter implements Exporter<ForeignKey> {
( (Table) foreignKey.getSourceTable() ).getTableName()
);
return new String[] {
"alter table " + sourceTableName + dialect.getDropForeignKeyString() + foreignKey.getName()
"alter table " + sourceTableName + dialect.getDropForeignKeyString() + foreignKey.getName().getText( dialect )
};
}
}

View File

@ -25,7 +25,6 @@ package org.hibernate.tool.schema.internal;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.Index;
import org.hibernate.metamodel.spi.relational.Table;
@ -48,7 +47,8 @@ public class StandardIndexExporter implements Exporter<Index> {
);
StringBuilder buf = new StringBuilder()
.append( "create index " )
.append( dialect.qualifyIndexName() ? index.getName() : StringHelper.unqualify( index.getName() ) )
.append( dialect.qualifyIndexName()
? index.getName().getText( dialect ) : index.getName().getUnqualifiedText( dialect ) )
.append( " on " )
.append( tableName )
.append( " (" );
@ -77,8 +77,7 @@ public class StandardIndexExporter implements Exporter<Index> {
( (Table) index.getTable() ).getTableName()
);
return new String[] {
"drop index " + ( dialect.qualifyIndexName()
? StringHelper.qualify( tableName, index.getName() ) : index.getName() )
"drop index " + index.getName().getQualifiedText( tableName, dialect )
};
}
}

View File

@ -58,7 +58,7 @@ public class StandardUniqueKeyExporter implements Exporter<Constraint> {
.append( "alter table " )
.append( tableName )
.append( " add constraint " )
.append( constraint.getName() )
.append( constraint.getName().getText( dialect ) )
.append( " unique ( " );
final Iterator columnIterator = constraint.getColumns().iterator();
@ -88,7 +88,7 @@ public class StandardUniqueKeyExporter implements Exporter<Constraint> {
if ( dialect.supportsIfExistsBeforeConstraintName() ) {
sb.append( "if exists " );
}
sb.append( dialect.quote( constraint.getName() ) );
sb.append( constraint.getName().getText( dialect ) );
if ( dialect.supportsIfExistsAfterConstraintName() ) {
sb.append( " if exists" );
}

View File

@ -80,7 +80,7 @@ public class UniqueConstraintBindingTest extends BaseAnnotationBindingTestCase {
int i = 0;
for ( UniqueKey key : uniqueKeyIterable ) {
i++;
assertEquals( ukName, key.getName() );
assertEquals( ukName, key.getName().getText() );
assertTrue( table == key.getTable() );
assertNotNull( key.getColumns() );
assertEquals( "There should be " + ukNumColumns + " columns in the unique constraint", ukNumColumns, key.getColumns().size() );

View File

@ -70,7 +70,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
idColumn.setJdbcDataType( BIGINT );
idColumn.setSize( Size.precision( 18, 0 ) );
table.getPrimaryKey().addColumn( idColumn );
table.getPrimaryKey().setName( "my_table_pk" );
table.getPrimaryKey().setName( Identifier.toIdentifier( "my_table_pk" ) );
Entity entity = new Entity( makeJavaType( "NoSuchClass" ), null );
HierarchyDetails hierarchyDetails = new HierarchyDetails.Builder().createHierarchyDetails();

View File

@ -59,8 +59,8 @@ public class TableManipulationTests extends BaseUnitTestCase {
idColumn.setJdbcDataType( INTEGER );
idColumn.setSize( Size.precision( 18, 0 ) );
table.getPrimaryKey().addColumn( idColumn );
table.getPrimaryKey().setName( "my_table_pk" );
assertEquals( "my_table_pk", table.getPrimaryKey().getName() );
table.getPrimaryKey().setName( Identifier.toIdentifier( "my_table_pk" ) );
assertEquals( "my_table_pk", table.getPrimaryKey().getName().toString() );
assertEquals( "my_table.PK", table.getPrimaryKey().getExportIdentifier() );
Column col_1 = table.locateOrCreateColumn( "col_1" );
@ -97,7 +97,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
bookId.setJdbcDataType( INTEGER );
bookId.setSize( Size.precision( 18, 0 ) );
book.getPrimaryKey().addColumn( bookId );
book.getPrimaryKey().setName( "BOOK_PK" );
book.getPrimaryKey().setName( Identifier.toIdentifier( "BOOK_PK" ) );
Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ), Identifier.toIdentifier( "PAGE" ) );
@ -105,7 +105,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
pageId.setJdbcDataType( INTEGER );
pageId.setSize( Size.precision( 18, 0 ) );
page.getPrimaryKey().addColumn( pageId );
page.getPrimaryKey().setName( "PAGE_PK" );
page.getPrimaryKey().setName( Identifier.toIdentifier( "PAGE_PK" ) );
Column pageBookId = page.locateOrCreateColumn( "BOOK_ID" );
pageId.setJdbcDataType( INTEGER );

View File

@ -52,7 +52,7 @@ public abstract class AbstractJPAIndexTest extends BaseCoreFunctionalTestCase {
org.hibernate.metamodel.spi.relational.Index index = indexes.next();
if (index.isUnique()) {
found++;
assertTrue( StringHelper.isNotEmpty( index.getName() ) );
assertTrue( StringHelper.isNotEmpty( index.getName().toString() ) );
assertEquals( 2, index.getColumnSpan() );
Column column = index.getColumns().get( 0 );
assertEquals( "brand", column.getColumnName().getText() );
@ -61,7 +61,7 @@ public abstract class AbstractJPAIndexTest extends BaseCoreFunctionalTestCase {
}
else {
found++;
assertEquals( "Car_idx", index.getName() );
assertEquals( "Car_idx", index.getName().toString() );
assertEquals( 1, index.getColumnSpan() );
Column column = index.getColumns().get( 0 );
assertEquals( "since", column.getColumnName().getText() );
@ -80,7 +80,7 @@ public abstract class AbstractJPAIndexTest extends BaseCoreFunctionalTestCase {
assertTrue( indexes.hasNext() );
org.hibernate.metamodel.spi.relational.Index index = indexes.next();
assertFalse( indexes.hasNext() );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName() ) );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName().toString() ) );
assertEquals( 2, index.getColumnSpan() );
org.hibernate.metamodel.spi.relational.Column column = index.getColumns().get( 0 );
assertEquals( "dealer_name", column.getColumnName().getText() );
@ -97,7 +97,7 @@ public abstract class AbstractJPAIndexTest extends BaseCoreFunctionalTestCase {
assertTrue( indexes.hasNext() );
org.hibernate.metamodel.spi.relational.Index index = indexes.next();
assertFalse( indexes.hasNext() );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName() ) );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName().toString() ) );
assertEquals( 1, index.getColumnSpan() );
org.hibernate.metamodel.spi.relational.Column column = index.getColumns().get( 0 );
assertEquals( "name", column.getColumnName().getText() );
@ -112,7 +112,7 @@ public abstract class AbstractJPAIndexTest extends BaseCoreFunctionalTestCase {
assertTrue( indexes.hasNext() );
org.hibernate.metamodel.spi.relational.Index index = indexes.next();
assertFalse( indexes.hasNext() );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName() ) );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName().toString() ) );
assertEquals( 1, index.getColumnSpan() );
org.hibernate.metamodel.spi.relational.Column column = index.getColumns().get( 0 );
assertEquals( "importers_id", column.getColumnName().getText() );

View File

@ -57,7 +57,7 @@ public class IndexTest extends AbstractJPAIndexTest {
assertTrue( indexes.hasNext() );
org.hibernate.metamodel.spi.relational.Index index = indexes.next();
assertFalse( indexes.hasNext() );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName() ) );
assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName().toString() ) );
assertEquals( 1, index.getColumnSpan() );
org.hibernate.metamodel.spi.relational.Column column = index.getColumns().get( 0 );
assertEquals( "GEN_VALUE", column.getColumnName().getText() );

View File

@ -132,10 +132,10 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
private void testConstraintLength(TableSpecification table) {
for (UniqueKey uk : table.getUniqueKeys()) {
assertTrue(uk.getName().length() <= MAX_NAME_LENGTH);
assertTrue(uk.getName().toString().length() <= MAX_NAME_LENGTH);
}
for (ForeignKey fk : table.getForeignKeys()) {
assertTrue(fk.getName().length() <= MAX_NAME_LENGTH);
assertTrue(fk.getName().toString().length() <= MAX_NAME_LENGTH);
}
}

View File

@ -82,7 +82,7 @@ public class ABCTest extends LegacyTestCase {
for ( Schema schema : metadata().getDatabase().getSchemas() ) {
for ( Table table : schema.getTables() ) {
for ( Index index : table.getIndexes() ) {
if ( index.getName().equals( "indx_a_name" ) ) {
if ( index.getName().toString().equals( "indx_a_name" ) ) {
found = true;
break;
}

View File

@ -123,8 +123,9 @@ public class SchemaUtil {
}
public static boolean hasUniqueKey(TableSpecification table, String keyName) {
Identifier identifier = Identifier.toIdentifier( keyName );
for ( UniqueKey uk : table.getUniqueKeys() ) {
if ( uk.getName().equals( keyName ) ) {
if ( uk.getName().equals( identifier ) ) {
return true;
}
}
@ -140,8 +141,9 @@ public class SchemaUtil {
* @return
*/
public static boolean hasUniqueKey(TableSpecification table, String keyName, String... columnNames) {
Identifier identifier = Identifier.toIdentifier( keyName );
for ( UniqueKey uk : table.getUniqueKeys() ) {
if ( uk.getName().equals( keyName ) ) {
if ( uk.getName().equals( identifier ) ) {
for (String columnName : columnNames) {
if (!uk.hasColumn( columnName )) {
return false;
@ -171,8 +173,9 @@ public class SchemaUtil {
}
public static boolean hasIndex(TableSpecification table, String indexName) {
Identifier identifier = Identifier.toIdentifier( indexName );
for ( Index index : table.getIndexes() ) {
if ( index.getName().equals( indexName ) ) {
if ( index.getName().equals( identifier ) ) {
return true;
}
}
@ -188,8 +191,9 @@ public class SchemaUtil {
* @return
*/
public static boolean hasIndex(TableSpecification table, String name, String... columnNames) {
Identifier identifier = Identifier.toIdentifier( name );
for ( Index index : table.getIndexes() ) {
if ( index.getName().equals( name ) ) {
if ( index.getName().equals( identifier ) ) {
if (columnNames.length != index.getColumnSpan()) {
return false;
}