Author: epbernard
Date: 2006-10-30 21:18:40 -0500 (Mon, 30 Oct 2006)
New Revision: 10660
Modified:
branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Constraint.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Index.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Table.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/UniqueKey.java
Log:
HHH-2199 apply supportsNoNullUnique to UniqueKey
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-10-30
16:19:10 UTC (rev 10659)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-10-31
02:18:40 UTC (rev 10660)
@@ -800,7 +800,8 @@
Iterator subIter = table.getUniqueKeyIterator();
while ( subIter.hasNext() ) {
UniqueKey uk = (UniqueKey) subIter.next();
- script.add( uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema )
);
+ String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog,
defaultSchema );
+ if (constraintString != null) script.add( constraintString );
}
}
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Constraint.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Constraint.java 2006-10-30
16:19:10 UTC (rev 10659)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Constraint.java 2006-10-31
02:18:40 UTC (rev 10660)
@@ -11,6 +11,7 @@
/**
* A relational constraint.
+ *
* @author Gavin King
*/
public abstract class Constraint implements RelationalModel, Serializable {
@@ -22,63 +23,88 @@
public String getName() {
return name;
}
+
public void setName(String name) {
this.name = name;
}
+
public Iterator getColumnIterator() {
return columns.iterator();
}
+
public void addColumn(Column column) {
- if ( !columns.contains(column) ) columns.add(column);
+ if ( !columns.contains( column ) ) columns.add( column );
}
+
public void addColumns(Iterator columnIterator) {
while ( columnIterator.hasNext() ) {
Selectable col = (Selectable) columnIterator.next();
if ( !col.isFormula() ) addColumn( (Column) col );
}
}
+
/**
* @param column
* @return true if this constraint already contains a column with same name.
*/
public boolean containsColumn(Column column) {
- return columns.contains(column);
- }
+ return columns.contains( column );
+ }
+
public int getColumnSpan() {
return columns.size();
}
-
+
public Column getColumn(int i) {
- return (Column) columns.get(i);
+ return (Column) columns.get( i );
}
-
+
public Iterator columnIterator() {
return columns.iterator();
}
+
public Table getTable() {
return table;
}
+
public void setTable(Table table) {
this.table = table;
}
+ public boolean isGenerated(Dialect dialect) {
+ return true;
+ }
+
public String sqlDropString(Dialect dialect, String defaultCatalog, String
defaultSchema) {
- return "alter table " + getTable().getQualifiedName(dialect, defaultCatalog,
defaultSchema) + " drop constraint " + getName();
+ if ( isGenerated( dialect ) ) {
+ return "alter table " + getTable()
+ .getQualifiedName( dialect, defaultCatalog, defaultSchema ) + " drop constraint
" + getName();
+ }
+ else {
+ return null;
+ }
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String
defaultSchema) {
- StringBuffer buf = new StringBuffer("alter table ")
- .append( getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) )
- .append( sqlConstraintString( dialect, getName(), defaultCatalog, defaultSchema ) );
- return buf.toString();
+ if ( isGenerated( dialect ) ) {
+ String constraintString = sqlConstraintString( dialect, getName(), defaultCatalog,
defaultSchema );
+ StringBuffer buf = new StringBuffer( "alter table " )
+ .append( getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
+ .append( constraintString );
+ return buf.toString();
+ }
+ else {
+ return null;
+ }
}
-
+
public List getColumns() {
return columns;
}
- public abstract String sqlConstraintString(Dialect d, String constraintName, String
defaultCatalog, String defaultSchema);
-
+ public abstract String sqlConstraintString(Dialect d, String constraintName, String
defaultCatalog,
+ String defaultSchema);
+
public String toString() {
return getClass().getName() + '(' + getTable().getName() + getColumns() +
") as " + name;
}
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Index.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Index.java 2006-10-30
16:19:10 UTC (rev 10659)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Index.java 2006-10-31
02:18:40 UTC (rev 10660)
@@ -13,6 +13,7 @@
/**
* A relational table index
+ *
* @author Gavin King
*/
public class Index implements RelationalModel, Serializable {
@@ -20,107 +21,120 @@
private Table table;
private List columns = new ArrayList();
private String name;
-
- public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog,
String defaultSchema)
+
+ public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog,
String defaultSchema)
throws HibernateException {
return buildSqlCreateIndexString(
- dialect,
- getName(),
- getTable(),
- getColumnIterator(),
- false,
- defaultCatalog,
+ dialect,
+ getName(),
+ getTable(),
+ getColumnIterator(),
+ false,
+ defaultCatalog,
defaultSchema
- );
+ );
}
-
- public static String buildSqlDropIndexString(
- Dialect dialect,
- Table table,
- String name,
- String defaultCatalog,
- String defaultSchema
- ) {
- return "drop index " +
- StringHelper.qualify(
- table.getQualifiedName(dialect, defaultCatalog, defaultSchema),
- name
- );
- }
- public static String buildSqlCreateIndexString(
- Dialect dialect,
- String name,
- Table table,
- Iterator columns,
- boolean unique,
- String defaultCatalog,
- String defaultSchema
- ) {
- StringBuffer buf = new StringBuffer("create")
- .append( unique ? " unique" : "")
- .append(" index ")
- .append( dialect.qualifyIndexName() ? name : StringHelper.unqualify(name) )
- .append(" on ")
- .append( table.getQualifiedName(dialect, defaultCatalog, defaultSchema) )
- .append(" (");
- Iterator iter = columns;
- while ( iter.hasNext() ) {
- buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
- if ( iter.hasNext() ) buf.append(", ");
- }
- buf.append(")");
- return buf.toString();
- }
-
+ public static String buildSqlDropIndexString(
+ Dialect dialect,
+ Table table,
+ String name,
+ String defaultCatalog,
+ String defaultSchema
+ ) {
+ return "drop index " +
+ StringHelper.qualify(
+ table.getQualifiedName( dialect, defaultCatalog, defaultSchema ),
+ name
+ );
+ }
+
+ public static String buildSqlCreateIndexString(
+ Dialect dialect,
+ String name,
+ Table table,
+ Iterator columns,
+ boolean unique,
+ String defaultCatalog,
+ String defaultSchema
+ ) {
+ //TODO handle supportsNotNullUnique=false, but such a case does not exist in the wild
so far
+ StringBuffer buf = new StringBuffer( "create" )
+ .append( unique ?
+ " unique" :
+ "" )
+ .append( " index " )
+ .append( dialect.qualifyIndexName() ?
+ name :
+ StringHelper.unqualify( name ) )
+ .append( " on " )
+ .append( table.getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
+ .append( " (" );
+ Iterator iter = columns;
+ while ( iter.hasNext() ) {
+ buf.append( ( (Column) iter.next() ).getQuotedName( dialect ) );
+ if ( iter.hasNext() ) buf.append( ", " );
+ }
+ buf.append( ")" );
+ return buf.toString();
+ }
+
+
// Used only in Table for sqlCreateString (but commented out at the moment)
public String sqlConstraintString(Dialect dialect) {
- StringBuffer buf = new StringBuffer(" index (");
+ StringBuffer buf = new StringBuffer( " index (" );
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
- buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
- if ( iter.hasNext() ) buf.append(", ");
+ buf.append( ( (Column) iter.next() ).getQuotedName( dialect ) );
+ if ( iter.hasNext() ) buf.append( ", " );
}
- return buf.append(')').toString();
+ return buf.append( ')' ).toString();
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String
defaultSchema) {
- return "drop index " +
- StringHelper.qualify(
- table.getQualifiedName(dialect, defaultCatalog, defaultSchema),
+ return "drop index " +
+ StringHelper.qualify(
+ table.getQualifiedName( dialect, defaultCatalog, defaultSchema ),
name
- );
+ );
}
public Table getTable() {
return table;
}
+
public void setTable(Table table) {
this.table = table;
}
+
public int getColumnSpan() {
return columns.size();
}
+
public Iterator getColumnIterator() {
return columns.iterator();
}
+
public void addColumn(Column column) {
- if ( !columns.contains(column) ) columns.add(column);
+ if ( !columns.contains( column ) ) columns.add( column );
}
+
public void addColumns(Iterator extraColumns) {
while ( extraColumns.hasNext() ) addColumn( (Column) extraColumns.next() );
}
+
/**
* @param column
* @return true if this constraint already contains a column with same name.
*/
public boolean containsColumn(Column column) {
- return columns.contains(column);
- }
-
+ return columns.contains( column );
+ }
+
public String getName() {
return name;
}
+
public void setName(String name) {
this.name = name;
}
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Table.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Table.java 2006-10-30
16:19:10 UTC (rev 10659)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/Table.java 2006-10-31
02:18:40 UTC (rev 10660)
@@ -51,15 +51,15 @@
String referencedClassName;
List columns;
List referencedColumns;
-
+
ForeignKeyKey(List columns, String referencedClassName, List referencedColumns) {
this.referencedClassName = referencedClassName;
this.columns = new ArrayList();
this.columns.addAll( columns );
- if(referencedColumns!=null) {
+ if ( referencedColumns != null ) {
this.referencedColumns = new ArrayList();
- this.referencedColumns.addAll( referencedColumns );
- }
+ this.referencedColumns.addAll( referencedColumns );
+ }
else {
this.referencedColumns = CollectionHelper.EMPTY_LIST;
}
@@ -70,28 +70,33 @@
}
public boolean equals(Object other) {
- ForeignKeyKey fkk = ( ForeignKeyKey ) other;
+ ForeignKeyKey fkk = (ForeignKeyKey) other;
return fkk.columns.equals( columns ) &&
- fkk.referencedClassName.equals( referencedClassName ) &&
fkk.referencedColumns.equals( referencedColumns );
+ fkk.referencedClassName.equals( referencedClassName ) &&
fkk.referencedColumns
+ .equals( referencedColumns );
}
}
public Table() {
uniqueInteger = tableCounter++;
}
-
+
public Table(String name) {
this();
- setName(name);
+ setName( name );
}
public String getQualifiedName(Dialect dialect, String defaultCatalog, String
defaultSchema) {
if ( subselect != null ) {
return "( " + subselect + " )";
}
- String quotedName = getQuotedName(dialect);
- String usedSchema = schema == null ? defaultSchema : getQuotedSchema(dialect);
- String usedCatalog = catalog == null ? defaultCatalog : catalog;
+ String quotedName = getQuotedName( dialect );
+ String usedSchema = schema == null ?
+ defaultSchema :
+ getQuotedSchema( dialect );
+ String usedCatalog = catalog == null ?
+ defaultCatalog :
+ catalog;
return qualify( usedCatalog, usedSchema, quotedName );
}
@@ -110,32 +115,36 @@
return name;
}
- /** returns quoted name as it would be in the mapping file. */
+ /**
+ * returns quoted name as it would be in the mapping file.
+ */
public String getQuotedName() {
return quoted ?
"`" + name + "`" :
name;
}
-
+
public String getQuotedName(Dialect dialect) {
return quoted ?
dialect.openQuote() + name + dialect.closeQuote() :
name;
}
- /** returns quoted name as it is in the mapping file. */
+ /**
+ * returns quoted name as it is in the mapping file.
+ */
public String getQuotedSchema() {
return schemaQuoted ?
"`" + schema + "`" :
schema;
}
-
+
public String getQuotedSchema(Dialect dialect) {
return schemaQuoted ?
dialect.openQuote() + schema + dialect.closeQuote() :
schema;
}
-
+
public void setName(String name) {
if ( name.charAt( 0 ) == '`' ) {
quoted = true;
@@ -157,7 +166,7 @@
return null;
}
- Column myColumn = ( Column ) columns.get( column.getCanonicalName() );
+ Column myColumn = (Column) columns.get( column.getCanonicalName() );
return column.equals( myColumn ) ?
myColumn :
@@ -169,11 +178,11 @@
for ( int i = 0; i < n - 1; i++ ) {
iter.next();
}
- return ( Column ) iter.next();
+ return (Column) iter.next();
}
public void addColumn(Column column) {
- Column old = ( Column ) getColumn( column );
+ Column old = (Column) getColumn( column );
if ( old == null ) {
columns.put( column.getCanonicalName(), column );
column.uniqueInteger = columns.size();
@@ -202,9 +211,9 @@
public Iterator getUniqueKeyIterator() {
return getUniqueKeys().values().iterator();
}
-
+
Map getUniqueKeys() {
- if ( uniqueKeys.size() > 1) {
+ if ( uniqueKeys.size() > 1 ) {
//deduplicate unique constraints sharing the same columns
//this is needed by Hibernate Annotations since it creates automagically
// unique constraints for the user
@@ -219,12 +228,13 @@
Iterator tempUks = finalUniqueKeys.entrySet().iterator();
while ( tempUks.hasNext() ) {
final UniqueKey currentUk = (UniqueKey) ( (Map.Entry) tempUks.next() ).getValue();
- if ( currentUk.getColumns().containsAll( columns ) && columns.containsAll(
currentUk.getColumns() ) ) {
+ if ( currentUk.getColumns().containsAll( columns ) && columns
+ .containsAll( currentUk.getColumns() ) ) {
skip = true;
break;
}
}
- if (! skip) finalUniqueKeys.put(entry.getKey(), uk);
+ if ( !skip ) finalUniqueKeys.put( entry.getKey(), uk );
}
return finalUniqueKeys;
}
@@ -232,11 +242,11 @@
return uniqueKeys;
}
}
-
+
public void validateColumns(Dialect dialect, Mapping mapping, TableMetadata tableInfo)
{
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
- Column col = ( Column ) iter.next();
+ Column col = (Column) iter.next();
ColumnMetadata columnInfo = tableInfo.getColumnMetadata( col.getName() );
@@ -244,21 +254,22 @@
throw new HibernateException( "Missing column: " + col.getName() );
}
else {
- final boolean typesMatch = col.getSqlType(dialect, mapping)
+ final boolean typesMatch = col.getSqlType( dialect, mapping )
.startsWith( columnInfo.getTypeName().toLowerCase() )
- || columnInfo.getTypeCode() == col.getSqlTypeCode(mapping);
+ || columnInfo.getTypeCode() == col.getSqlTypeCode( mapping );
if ( !typesMatch ) {
throw new HibernateException(
- "Wrong column type: " + col.getName() +
- ", expected: " + col.getSqlType(dialect, mapping)
- );
+ "Wrong column type: " + col.getName() +
+ ", expected: " + col.getSqlType( dialect, mapping )
+ );
}
}
}
-
+
}
- public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo,
String defaultCatalog, String defaultSchema)
+ public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo,
String defaultCatalog,
+ String defaultSchema)
throws HibernateException {
StringBuffer root = new StringBuffer( "alter table " )
@@ -269,7 +280,7 @@
Iterator iter = getColumnIterator();
List results = new ArrayList();
while ( iter.hasNext() ) {
- Column column = ( Column ) iter.next();
+ Column column = (Column) iter.next();
ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );
@@ -280,27 +291,27 @@
.append( column.getQuotedName( dialect ) )
.append( ' ' )
.append( column.getSqlType( dialect, p ) );
-
+
String defaultValue = column.getDefaultValue();
- if (defaultValue!=null) {
- alter.append( " default ").append(defaultValue);
-
+ if ( defaultValue != null ) {
+ alter.append( " default " ).append( defaultValue );
+
if ( column.isNullable() ) {
alter.append( dialect.getNullColumnString() );
}
else {
alter.append( " not null" );
}
-
+
}
-
- boolean useUniqueConstraint = column.isUnique() &&
- dialect.supportsUnique() &&
+
+ boolean useUniqueConstraint = column.isUnique() &&
+ dialect.supportsUnique() &&
( !column.isNullable() || dialect.supportsNotNullUnique() );
if ( useUniqueConstraint ) {
alter.append( " unique" );
}
-
+
if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
alter.append( " check(" )
.append( column.getCheckConstraint() )
@@ -319,9 +330,9 @@
return results.iterator();
}
-
+
public boolean hasPrimaryKey() {
- return getPrimaryKey()!=null;
+ return getPrimaryKey() != null;
}
public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) throws
HibernateException {
@@ -330,8 +341,8 @@
.append( name )
.append( " (" );
Iterator itr = getColumnIterator();
- while( itr.hasNext() ) {
- final Column column = ( Column ) itr.next();
+ while ( itr.hasNext() ) {
+ final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
buffer.append( column.getSqlType( dialect, mapping ) );
if ( column.isNullable() ) {
@@ -352,20 +363,20 @@
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String
defaultSchema)
throws HibernateException {
StringBuffer buf = new StringBuffer( "create table " )
- .append( getQualifiedName( dialect , defaultCatalog, defaultSchema ) )
+ .append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( " (" );
- boolean identityColumn = idValue != null && idValue.isIdentityColumn(dialect);
+ boolean identityColumn = idValue != null && idValue.isIdentityColumn( dialect
);
// Try to find out the name of the primary key to create it as identity if the
IdentityGenerator is used
String pkname = null;
if ( hasPrimaryKey() && identityColumn ) {
- pkname = ( ( Column ) getPrimaryKey().getColumnIterator().next() ).getQuotedName(
dialect );
+ pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName(
dialect );
}
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
- Column col = ( Column ) iter.next();
+ Column col = (Column) iter.next();
buf.append( col.getQuotedName( dialect ) )
.append( ' ' );
@@ -383,20 +394,20 @@
buf.append( col.getSqlType( dialect, p ) );
String defaultValue = col.getDefaultValue();
- if (defaultValue!=null) {
- buf.append( " default ").append(defaultValue);
+ if ( defaultValue != null ) {
+ buf.append( " default " ).append( defaultValue );
}
-
+
if ( col.isNullable() ) {
buf.append( dialect.getNullColumnString() );
}
else {
buf.append( " not null" );
}
-
+
}
- boolean useUniqueConstraint = col.isUnique() &&
+ boolean useUniqueConstraint = col.isUnique() &&
( !col.isNullable() || dialect.supportsNotNullUnique() );
if ( useUniqueConstraint ) {
if ( dialect.supportsUnique() ) {
@@ -407,16 +418,16 @@
uk.addColumn( col );
}
}
-
+
if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
buf.append( " check (" )
.append( col.getCheckConstraint() )
.append( ")" );
}
-
+
String columnComment = col.getComment();
- if (columnComment!=null) {
- buf.append( dialect.getColumnComment(columnComment) );
+ if ( columnComment != null ) {
+ buf.append( dialect.getColumnComment( columnComment ) );
}
if ( iter.hasNext() ) {
@@ -426,15 +437,17 @@
}
if ( hasPrimaryKey() ) {
buf.append( ", " )
- .append( getPrimaryKey().sqlConstraintString( dialect ) );
+ .append( getPrimaryKey().sqlConstraintString( dialect ) );
}
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
Iterator ukiter = getUniqueKeyIterator();
while ( ukiter.hasNext() ) {
- UniqueKey uk = ( UniqueKey ) ukiter.next();
- buf.append( ", " )
- .append( uk.sqlConstraintString( dialect ) );
+ UniqueKey uk = (UniqueKey) ukiter.next();
+ String constraint = uk.sqlConstraintString( dialect );
+ if ( constraint != null ) {
+ buf.append( ", " ).append( constraint );
+ }
}
}
/*Iterator idxiter = getIndexIterator();
@@ -447,17 +460,17 @@
Iterator chiter = checkConstraints.iterator();
while ( chiter.hasNext() ) {
buf.append( ", check (" )
- .append( chiter.next() )
- .append( ')' );
+ .append( chiter.next() )
+ .append( ')' );
}
}
-
+
buf.append( ')' );
if ( comment != null ) {
buf.append( dialect.getTableComment( comment ) );
}
-
+
return buf.append( dialect.getTableTypeString() ).toString();
}
@@ -466,7 +479,7 @@
if ( dialect.supportsIfExistsBeforeTableName() ) {
buf.append( "if exists " );
}
- buf.append( getQualifiedName( dialect , defaultCatalog, defaultSchema ) )
+ buf.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( dialect.getCascadeConstraintsString() );
if ( dialect.supportsIfExistsAfterTableName() ) {
buf.append( " if exists" );
@@ -483,9 +496,9 @@
}
public Index getOrCreateIndex(String indexName) {
-
- Index index = ( Index ) indexes.get( indexName );
+ Index index = (Index) indexes.get( indexName );
+
if ( index == null ) {
index = new Index();
index.setName( indexName );
@@ -497,11 +510,11 @@
}
public Index getIndex(String indexName) {
- return ( Index ) indexes.get( indexName );
+ return (Index) indexes.get( indexName );
}
public Index addIndex(Index index) {
- Index current = ( Index ) indexes.get( index.getName() );
+ Index current = (Index) indexes.get( index.getName() );
if ( current != null ) {
throw new MappingException( "Index " + index.getName() + " already
exists!" );
}
@@ -510,7 +523,7 @@
}
public UniqueKey addUniqueKey(UniqueKey uniqueKey) {
- UniqueKey current = ( UniqueKey ) uniqueKeys.get( uniqueKey.getName() );
+ UniqueKey current = (UniqueKey) uniqueKeys.get( uniqueKey.getName() );
if ( current != null ) {
throw new MappingException( "UniqueKey " + uniqueKey.getName() + "
already exists!" );
}
@@ -528,9 +541,9 @@
public UniqueKey getUniqueKey(String keyName) {
return (UniqueKey) uniqueKeys.get( keyName );
}
-
+
public UniqueKey getOrCreateUniqueKey(String keyName) {
- UniqueKey uk = ( UniqueKey ) uniqueKeys.get( keyName );
+ UniqueKey uk = (UniqueKey) uniqueKeys.get( keyName );
if ( uk == null ) {
uk = new UniqueKey();
@@ -545,20 +558,21 @@
}
public ForeignKey createForeignKey(String keyName, List keyColumns, String
referencedEntityName) {
- return createForeignKey(keyName, keyColumns, referencedEntityName, null);
+ return createForeignKey( keyName, keyColumns, referencedEntityName, null );
}
-
- public ForeignKey createForeignKey(String keyName, List keyColumns, String
referencedEntityName, List referencedColumns) {
+
+ public ForeignKey createForeignKey(String keyName, List keyColumns, String
referencedEntityName,
+ List referencedColumns) {
Object key = new ForeignKeyKey( keyColumns, referencedEntityName, referencedColumns );
- ForeignKey fk = ( ForeignKey ) foreignKeys.get( key );
+ ForeignKey fk = (ForeignKey) foreignKeys.get( key );
if ( fk == null ) {
fk = new ForeignKey();
if ( keyName != null ) {
fk.setName( keyName );
}
else {
- fk.setName( "FK" + uniqueColumnString( keyColumns.iterator(),
referencedEntityName ) );
+ fk.setName( "FK" + uniqueColumnString( keyColumns.iterator(),
referencedEntityName ) );
//TODO: add referencedClass to disambiguate to FKs on the same
// columns, pointing to different tables
}
@@ -575,11 +589,12 @@
fk.setName( keyName );
}
- return fk; }
+ return fk;
+ }
-
+
public String uniqueColumnString(Iterator iterator) {
- return uniqueColumnString(iterator, null);
+ return uniqueColumnString( iterator, null );
}
public String uniqueColumnString(Iterator iterator, String referencedEntityName) {
@@ -599,7 +614,7 @@
}
public void setSchema(String schema) {
- if ( schema!=null && schema.charAt( 0 ) == '`' ) {
+ if ( schema != null && schema.charAt( 0 ) == '`' ) {
schemaQuoted = true;
this.schema = schema.substring( 1, schema.length() - 1 );
}
@@ -631,7 +646,7 @@
public boolean isSchemaQuoted() {
return schemaQuoted;
}
-
+
public boolean isQuoted() {
return quoted;
}
@@ -658,14 +673,14 @@
public String toString() {
StringBuffer buf = new StringBuffer().append( getClass().getName() )
- .append('(');
+ .append( '(' );
if ( getCatalog() != null ) {
buf.append( getCatalog() + "." );
}
if ( getSchema() != null ) {
buf.append( getSchema() + "." );
}
- buf.append( getName() ).append(')');
+ buf.append( getName() ).append( ')' );
return buf.toString();
}
@@ -684,11 +699,11 @@
public boolean isAbstractUnionTable() {
return hasDenormalizedTables() && isAbstract;
}
-
+
public boolean hasDenormalizedTables() {
return hasDenormalizedTables;
}
-
+
void setHasDenormalizedTables() {
hasDenormalizedTables = true;
}
@@ -720,29 +735,29 @@
public Iterator sqlCommentStrings(Dialect dialect, String defaultCatalog, String
defaultSchema) {
List comments = new ArrayList();
if ( dialect.supportsCommentOn() ) {
- String tableName = getQualifiedName( dialect , defaultCatalog, defaultSchema );
- if ( comment!=null ) {
+ String tableName = getQualifiedName( dialect, defaultCatalog, defaultSchema );
+ if ( comment != null ) {
StringBuffer buf = new StringBuffer()
- .append("comment on table ")
- .append( tableName )
- .append(" is '")
- .append(comment)
- .append("'");
+ .append( "comment on table " )
+ .append( tableName )
+ .append( " is '" )
+ .append( comment )
+ .append( "'" );
comments.add( buf.toString() );
}
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
String columnComment = column.getComment();
- if ( columnComment!=null ) {
+ if ( columnComment != null ) {
StringBuffer buf = new StringBuffer()
- .append("comment on column ")
- .append( tableName )
- .append('.')
- .append( column.getQuotedName(dialect) )
- .append(" is '")
- .append(columnComment)
- .append("'");
+ .append( "comment on column " )
+ .append( tableName )
+ .append( '.' )
+ .append( column.getQuotedName( dialect ) )
+ .append( " is '" )
+ .append( columnComment )
+ .append( "'" );
comments.add( buf.toString() );
}
}
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/UniqueKey.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/UniqueKey.java 2006-10-30
16:19:10 UTC (rev 10659)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/mapping/UniqueKey.java 2006-10-31
02:18:40 UTC (rev 10660)
@@ -9,48 +9,74 @@
/**
* A relational unique key constraint
+ *
* @author Gavin King
*/
public class UniqueKey extends Constraint {
public String sqlConstraintString(Dialect dialect) {
- StringBuffer buf = new StringBuffer("unique (");
+ StringBuffer buf = new StringBuffer( "unique (" );
Iterator iter = getColumnIterator();
+ boolean nullable = false;
while ( iter.hasNext() ) {
- buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
- if ( iter.hasNext() ) buf.append(", ");
+ Column column = (Column) iter.next();
+ if ( !nullable && column.isNullable() ) nullable = true;
+ buf.append( column.getQuotedName( dialect ) );
+ if ( iter.hasNext() ) buf.append( ", " );
}
- return buf.append(')').toString();
+ //do not add unique constraint on DB not supporting unique and nullable columns
+ return !nullable || dialect.supportsNotNullUnique() ?
+ buf.append( ')' ).toString() :
+ null;
}
- public String sqlConstraintString(Dialect dialect, String constraintName, String
defaultCatalog, String defaultSchema) {
+ public String sqlConstraintString(Dialect dialect, String constraintName, String
defaultCatalog,
+ String defaultSchema) {
StringBuffer buf = new StringBuffer(
- dialect.getAddPrimaryKeyConstraintString(constraintName)
- ).append('(');
+ dialect.getAddPrimaryKeyConstraintString( constraintName )
+ ).append( '(' );
Iterator iter = getColumnIterator();
+ boolean nullable = false;
while ( iter.hasNext() ) {
- buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
- if ( iter.hasNext() ) buf.append(", ");
+ Column column = (Column) iter.next();
+ if ( !nullable && column.isNullable() ) nullable = true;
+ buf.append( column.getQuotedName( dialect ) );
+ if ( iter.hasNext() ) buf.append( ", " );
}
- return StringHelper.replace( buf.append(')').toString(), "primary
key", "unique" ); //TODO: improve this hack!
+ return !nullable || dialect.supportsNotNullUnique() ?
+ StringHelper.replace( buf.append( ')' ).toString(), "primary key",
"unique" ) :
+ //TODO: improve this hack!
+ null;
}
-
+
public String sqlCreateString(Dialect dialect, Mapping p, 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);
- }
- }
-
- public String sqlDropString(Dialect dialect, String defaultCatalog, String
defaultSchema) {
- if( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
- return super.sqlDropString(dialect, defaultCatalog, defaultSchema);
- }
- else {
- return Index.buildSqlDropIndexString(dialect, getTable(), getName(),
defaultCatalog, defaultSchema);
- }
- }
-
+ if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
+ return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
+ }
+ else {
+ return Index.buildSqlCreateIndexString( dialect, getName(), getTable(),
getColumnIterator(), true,
+ defaultCatalog, defaultSchema );
+ }
+ }
+
+ public String sqlDropString(Dialect dialect, String defaultCatalog, String
defaultSchema) {
+ if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
+ return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
+ }
+ else {
+ return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog,
defaultSchema );
+ }
+ }
+
+ public boolean isGenerated(Dialect dialect) {
+ if ( dialect.supportsNotNullUnique() ) return true;
+ Iterator iter = getColumnIterator();
+ while ( iter.hasNext() ) {
+ if ( ( (Column) iter.next() ).isNullable() ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
}