Skip to content

Commit

Permalink
HHH-7797 Finished auditing dialects. Cleanup and javadocs. Completed
Browse files Browse the repository at this point in the history
uniqueness test.
  • Loading branch information
brmeyer committed Dec 18, 2012
1 parent fa09bc2 commit 98149d2
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 139 deletions.
Expand Up @@ -401,11 +401,6 @@ public boolean qualifyIndexName() {
return false;
}

public boolean supportsUnique() {
// Does this dialect support the UNIQUE column syntax?
return true;
}

/**
* The syntax used to add a foreign key constraint to a table.
*
Expand Down
Expand Up @@ -216,10 +216,6 @@ public String getForUpdateString() {
return " for update";
}

public boolean supportsUnique() {
return true;
}

public boolean supportsLimit() {
return true;
}
Expand Down
Expand Up @@ -246,10 +246,6 @@ public String getForUpdateString() {
return "";
}

public boolean supportsUnique() {
return false;
}

public boolean supportsLimit() {
return true;
}
Expand Down
Expand Up @@ -306,13 +306,6 @@ public boolean useMaxForLimit() {
return true;
}

/**
* Ingres explicitly needs "unique not null", because "with null" is default
*/
public boolean supportsNotNullUnique() {
return false;
}

/**
* Does this dialect support temporary tables?
*/
Expand Down
Expand Up @@ -240,13 +240,6 @@ public String getForUpdateString() {
return ""; // Original Dialect.java returns " for update";
}

/**
* RDMS does not support adding Unique constraints via create and alter table.
*/
public boolean supportsUniqueConstraintInCreateAlterTable() {
return true;
}

// Verify the state of this new method in Hibernate 3.0 Dialect.java
/**
* RDMS does not support Cascade Deletes.
Expand Down
Expand Up @@ -422,10 +422,6 @@ public boolean supportsExpectedLobUsagePattern() {
return false;
}

public boolean supportsUniqueConstraintInCreateAlterTable() {
return false;
}

public String getCrossJoinSeparator() {
return ", ";
}
Expand Down
Expand Up @@ -102,14 +102,6 @@ public boolean dropConstraints() {
public boolean qualifyIndexName() {
return false;
}

public boolean supportsUnique() {
return false;
}

public boolean supportsUniqueConstraintInCreateAlterTable() {
return false;
}

public String getAddColumnString() {
return "add";
Expand Down
Expand Up @@ -44,20 +44,6 @@ public DefaultUniqueDelegate( Dialect dialect ) {
@Override
public String applyUniqueToColumn( org.hibernate.mapping.Table table,
org.hibernate.mapping.Column column ) {
// if ( column.isUnique()
// && ( column.isNullable()
// || dialect.supportsNotNullUnique() ) ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// // If the constraint is supported, do not add to the column syntax.
// UniqueKey uk = getOrCreateUniqueKey( column.getQuotedName( dialect ) + '_' );
// uk.addColumn( column );
// }
// else if ( dialect.supportsUnique() ) {
// // Otherwise, add to the column syntax if supported.
// sb.append( " unique" );
// }
// }

org.hibernate.mapping.UniqueKey uk = table.getOrCreateUniqueKey(
column.getQuotedName( dialect ) + '_' );
uk.addColumn( column );
Expand All @@ -66,20 +52,6 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table,

@Override
public String applyUniqueToColumn( Table table, Column column ) {
// if ( column.isUnique()
// && ( column.isNullable()
// || dialect.supportsNotNullUnique() ) ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// // If the constraint is supported, do not add to the column syntax.
// UniqueKey uk = getOrCreateUniqueKey( column.getQuotedName( dialect ) + '_' );
// uk.addColumn( column );
// }
// else if ( dialect.supportsUnique() ) {
// // Otherwise, add to the column syntax if supported.
// sb.append( " unique" );
// }
// }

UniqueKey uk = table.getOrCreateUniqueKey( column.getColumnName()
.encloseInQuotesIfQuoted( dialect ) + '_' );
uk.addColumn( column );
Expand All @@ -88,41 +60,19 @@ public String applyUniqueToColumn( Table table, Column column ) {

@Override
public String applyUniquesToTable( org.hibernate.mapping.Table table ) {
// TODO: Am I correct that this shouldn't be done unless the constraint
// isn't created in an alter table?
// Iterator uniqueKeyIterator = table.getUniqueKeyIterator();
// while ( uniqueKeyIterator.hasNext() ) {
// UniqueKey uniqueKey = (UniqueKey) uniqueKeyIterator.next();
//
// sb.append( ", " ).append( createUniqueConstraint( uniqueKey) );
// }
return "";
}

@Override
public String applyUniquesToTable( Table table ) {
// TODO: Am I correct that this shouldn't be done unless the constraint
// isn't created in an alter table?
// Iterator uniqueKeyIterator = table.getUniqueKeyIterator();
// while ( uniqueKeyIterator.hasNext() ) {
// UniqueKey uniqueKey = (UniqueKey) uniqueKeyIterator.next();
//
// sb.append( ", " ).append( createUniqueConstraint( uniqueKey) );
// }
return "";
}

@Override
public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
String defaultCatalog, String defaultSchema ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
// }
// else {
// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
// defaultCatalog, defaultSchema );
// }

// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether or not it happens.
return new StringBuilder( "alter table " )
.append( uniqueKey.getTable().getQualifiedName(
dialect, defaultCatalog, defaultSchema ) )
Expand All @@ -134,15 +84,9 @@ public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,

@Override
public String applyUniquesOnAlter( UniqueKey uniqueKey ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
// }
// else {
// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
// defaultCatalog, defaultSchema );
// }

return new StringBuilder( "alter table " )
// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether or not it happens.
return new StringBuilder( "alter table " )
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
.append( " add constraint " )
.append( uniqueKey.getName() )
Expand All @@ -153,13 +97,8 @@ public String applyUniquesOnAlter( UniqueKey uniqueKey ) {
@Override
public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
String defaultCatalog, String defaultSchema ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
// }
// else {
// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
// }

// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether or not it happens.
return new StringBuilder( "alter table " )
.append( uniqueKey.getTable().getQualifiedName(
dialect, defaultCatalog, defaultSchema ) )
Expand All @@ -170,13 +109,8 @@ public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,

@Override
public String dropUniquesOnAlter( UniqueKey uniqueKey ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
// }
// else {
// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
// }

// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether or not it happens.
return new StringBuilder( "alter table " )
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
.append( " drop constraint " )
Expand All @@ -186,10 +120,6 @@ public String dropUniquesOnAlter( UniqueKey uniqueKey ) {

@Override
public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) {
// TODO: This may not be necessary, but not all callers currently
// check it on their own. Go through their logic.
// if ( !isGenerated( dialect ) ) return null;

StringBuilder sb = new StringBuilder();
sb.append( " unique (" );
Iterator columnIterator = uniqueKey.getColumnIterator();
Expand All @@ -207,10 +137,6 @@ public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) {

@Override
public String uniqueConstraintSql( UniqueKey uniqueKey ) {
// TODO: This may not be necessary, but not all callers currently
// check it on their own. Go through their logic.
// if ( !isGenerated( dialect ) ) return null;

StringBuilder sb = new StringBuilder();
sb.append( " unique (" );
Iterator columnIterator = uniqueKey.getColumns().iterator();
Expand Down
Expand Up @@ -28,12 +28,16 @@
* Dialect-level delegate in charge of applying "uniqueness" to a column.
* Uniqueness can be defined in 1 of 3 ways:
*
* 1.) Add a unique constraint via separate create/alter table statements.
* 1.) Add a unique constraint via separate alter table statements.
* 2.) Add a unique constraint via dialect-specific syntax in table create statement.
* 3.) Add "unique" syntax to the column itself.
*
* #1 & #2 are preferred, if possible -- #3 should be solely a fall-back.
*
* TODO: This could eventually be simplified. With AST, 1 "applyUniqueness"
* method might be possible. But due to .cfg and .mapping still resolving
* around StringBuilders, separate methods were needed.
*
* See HHH-7797.
*
* @author Brett Meyer
Expand All @@ -43,7 +47,8 @@ public interface UniqueDelegate {
/**
* If the delegate supports unique constraints, this method should simply
* create the UniqueKey on the Table. Otherwise, the constraint isn't
* supported and "unique" should be added to the column definition.
* supported and "unique" should be returned in order to add it
* to the column definition.
*
* @param table
* @param column
Expand All @@ -55,7 +60,8 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table,
/**
* If the delegate supports unique constraints, this method should simply
* create the UniqueKey on the Table. Otherwise, the constraint isn't
* supported and "unique" should be added to the column definition.
* supported and "unique" should be returned in order to add it
* to the column definition.
*
* @param table
* @param column
Expand All @@ -64,19 +70,19 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table,
public String applyUniqueToColumn( Table table, Column column );

/**
* If creating unique constraints in separate alter statements are not
* supported, this method should return the syntax necessary to create
* the constraint on the original create table statement.
* If constraints are supported, but not in seperate alter statements,
* return uniqueConstraintSql in order to add the constraint to the
* original table definition.
*
* @param table
* @return String
*/
public String applyUniquesToTable( org.hibernate.mapping.Table table );

/**
* If creating unique constraints in separate alter statements are not
* supported, this method should return the syntax necessary to create
* the constraint on the original create table statement.
* If constraints are supported, but not in seperate alter statements,
* return uniqueConstraintSql in order to add the constraint to the
* original table definition.
*
* @param table
* @return String
Expand Down
Expand Up @@ -20,18 +20,23 @@
*/
package org.hibernate.test.constraint;

import javax.persistence.Column;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.mapping.Column;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* HHH-7797 re-wrote the way dialects handle unique constraints. Test as
* many variations of unique, not null, and primary key constraints as possible.
* HHH-7797 re-wrote the way dialects handle unique constraints. Test
* variations of unique & not null to ensure the constraints are created
* correctly for each dialect.
*
* @author Brett Meyer
*/
Expand All @@ -47,18 +52,34 @@ protected Class<?>[] getAnnotatedClasses() {

@Test
public void testConstraints() {
// nothing yet -- more interested in DDL creation
Column column = (Column) configuration().getClassMapping( Entity1.class.getName() )
.getProperty( "foo1" ).getColumnIterator().next();
assertFalse( column.isNullable() );
assertTrue( column.isUnique() );

column = (Column) configuration().getClassMapping( Entity1.class.getName() )
.getProperty( "foo2" ).getColumnIterator().next();
assertTrue( column.isNullable() );
assertTrue( column.isUnique() );

column = (Column) configuration().getClassMapping( Entity1.class.getName() )
.getProperty( "id" ).getColumnIterator().next();
assertFalse( column.isNullable() );
assertTrue( column.isUnique() );
}

// Primary key w/ not null and unique
@Entity
@Table( name = "Entity1" )
public static class Entity1 {
@Id
@GeneratedValue
// @Column( nullable = false, unique = true)
@javax.persistence.Column( nullable = false, unique = true)
public long id;

@Column( nullable = false, unique = true)
public String foo;
@javax.persistence.Column( nullable = false, unique = true)
public String foo1;

@javax.persistence.Column( nullable = true, unique = true)
public String foo2;
}
}

0 comments on commit 98149d2

Please sign in to comment.