KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > UniqueKey


1 //$Id: UniqueKey.java,v 1.6 2005/02/14 10:59:35 maxcsaucdk Exp $
2
package org.hibernate.mapping;
3
4 import java.util.Iterator JavaDoc;
5
6 import org.hibernate.dialect.Dialect;
7 import org.hibernate.engine.Mapping;
8 import org.hibernate.util.StringHelper;
9
10 /**
11  * A relational unique key constraint
12  * @author Gavin King
13  */

14 public class UniqueKey extends Constraint {
15
16     public String JavaDoc sqlConstraintString(Dialect dialect) {
17         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("unique (");
18         Iterator JavaDoc 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 JavaDoc sqlConstraintString(Dialect dialect, String JavaDoc constraintName, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) {
27         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(
28             dialect.getAddPrimaryKeyConstraintString(constraintName)
29         ).append('(');
30         Iterator JavaDoc 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" ); //TODO: improve this hack!
36
}
37     
38     public String JavaDoc sqlCreateString(Dialect dialect, Mapping p, String JavaDoc defaultCatalog, String JavaDoc 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 JavaDoc sqlDropString(Dialect dialect, String JavaDoc defaultCatalog, String JavaDoc 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