HHH-7797 Finished auditing dialects. Cleanup and javadocs. Completed

uniqueness test.
This commit is contained in:
brmeyer 2012-12-14 11:45:35 -05:00
parent 962d1e580d
commit 49c8a8e4f0
10 changed files with 53 additions and 139 deletions

View File

@ -401,11 +401,6 @@ public class Cache71Dialect extends Dialect {
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.
*

View File

@ -216,10 +216,6 @@ public class H2Dialect extends Dialect {
return " for update";
}
public boolean supportsUnique() {
return true;
}
public boolean supportsLimit() {
return true;
}

View File

@ -251,10 +251,6 @@ public class HSQLDialect extends Dialect {
}
}
public boolean supportsUnique() {
return false;
}
public boolean supportsLimit() {
return true;
}

View File

@ -306,13 +306,6 @@ public class IngresDialect extends Dialect {
return true;
}
/**
* Ingres explicitly needs "unique not null", because "with null" is default
*/
public boolean supportsNotNullUnique() {
return false;
}
/**
* Does this dialect support temporary tables?
*/

View File

@ -240,13 +240,6 @@ public class RDMSOS2200Dialect extends Dialect {
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.

View File

@ -422,10 +422,6 @@ public class SybaseASE15Dialect extends SybaseDialect {
return false;
}
public boolean supportsUniqueConstraintInCreateAlterTable() {
return false;
}
public String getCrossJoinSeparator() {
return ", ";
}

View File

@ -102,14 +102,6 @@ public class TimesTenDialect extends Dialect {
public boolean qualifyIndexName() {
return false;
}
public boolean supportsUnique() {
return false;
}
public boolean supportsUniqueConstraintInCreateAlterTable() {
return false;
}
public String getAddColumnString() {
return "add";

View File

@ -44,20 +44,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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 );
@ -66,20 +52,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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 );
@ -88,41 +60,19 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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 ) )
@ -134,15 +84,9 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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() )
@ -153,13 +97,8 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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 ) )
@ -170,13 +109,8 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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 " )
@ -186,10 +120,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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();
@ -207,10 +137,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
@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();

View File

@ -28,12 +28,16 @@ import org.hibernate.metamodel.relational.UniqueKey;
* 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
@ -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
@ -55,7 +60,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
@ -64,9 +70,9 @@ public interface UniqueDelegate {
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
@ -74,9 +80,9 @@ public interface UniqueDelegate {
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

View File

@ -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
*/
@ -47,18 +52,34 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
@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;
}
}