1 package org.hibernate.mapping; 3 4 import java.util.Iterator ; 5 6 import org.hibernate.dialect.Dialect; 7 import org.hibernate.engine.Mapping; 8 import org.hibernate.util.StringHelper; 9 10 14 public class UniqueKey extends Constraint { 15 16 public String sqlConstraintString(Dialect dialect) { 17 StringBuffer buf = new StringBuffer ("unique ("); 18 Iterator iter = getColumnIterator(); 19 while ( iter.hasNext() ) { 20 buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); 21 if ( iter.hasNext() ) buf.append(", "); 22 } 23 return buf.append(')').toString(); 24 } 25 26 public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) { 27 StringBuffer buf = new StringBuffer ( 28 dialect.getAddPrimaryKeyConstraintString(constraintName) 29 ).append('('); 30 Iterator iter = getColumnIterator(); 31 while ( iter.hasNext() ) { 32 buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); 33 if ( iter.hasNext() ) buf.append(", "); 34 } 35 return StringHelper.replace( buf.append(')').toString(), "primary key", "unique" ); } 37 38 public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) { 39 if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { 40 return super.sqlCreateString(dialect, p, defaultCatalog, defaultSchema); 41 } 42 else { 43 return Index.buildSqlCreateIndexString(dialect, getName(), getTable(), getColumnIterator(), true, defaultCatalog, defaultSchema); 44 } 45 } 46 47 public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { 48 if( dialect.supportsUniqueConstraintInCreateAlterTable() ) { 49 return super.sqlDropString(dialect, defaultCatalog, defaultSchema); 50 } 51 else { 52 return Index.buildSqlDropIndexString(dialect, getTable(), getName(), defaultCatalog, defaultSchema); 53 } 54 } 55 56 } 57 | Popular Tags |