KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Constraint.java,v 1.8 2005/02/12 07:19:26 steveebersole Exp $
2
package org.hibernate.mapping;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.hibernate.dialect.Dialect;
10 import org.hibernate.engine.Mapping;
11
12 /**
13  * A relational constraint.
14  * @author Gavin King
15  */

16 public abstract class Constraint implements RelationalModel, Serializable JavaDoc {
17
18     private String JavaDoc name;
19     private final List columns = new ArrayList JavaDoc();
20     private Table table;
21
22     public String JavaDoc getName() {
23         return name;
24     }
25     public void setName(String JavaDoc name) {
26         this.name = name;
27     }
28     public Iterator JavaDoc getColumnIterator() {
29         return columns.iterator();
30     }
31     public void addColumn(Column column) {
32         if ( !columns.contains(column) ) columns.add(column);
33     }
34     public void addColumns(Iterator JavaDoc columnIterator) {
35         while ( columnIterator.hasNext() ) {
36             Selectable col = (Selectable) columnIterator.next();
37             if ( !col.isFormula() ) addColumn( (Column) col );
38         }
39     }
40     /**
41      * @param column
42      * @return true if this constraint already contains a column with same name.
43      */

44     public boolean containsColumn(Column column) {
45         return columns.contains(column);
46     }
47     public int getColumnSpan() {
48         return columns.size();
49     }
50     
51     public Column getColumn(int i) {
52         return (Column) columns.get(i);
53     }
54     
55     public Iterator JavaDoc columnIterator() {
56         return columns.iterator();
57     }
58     public Table getTable() {
59         return table;
60     }
61     public void setTable(Table table) {
62         this.table = table;
63     }
64
65     public String JavaDoc sqlDropString(Dialect dialect, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) {
66         return "alter table " + getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) + " drop constraint " + getName();
67     }
68
69     public String JavaDoc sqlCreateString(Dialect dialect, Mapping p, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) {
70         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("alter table ")
71             .append( getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) )
72             .append( sqlConstraintString( dialect, getName(), defaultCatalog, defaultSchema ) );
73         return buf.toString();
74     }
75     
76     public List getColumns() {
77         return columns;
78     }
79
80     public abstract String JavaDoc sqlConstraintString(Dialect d, String JavaDoc constraintName, String JavaDoc defaultCatalog, String JavaDoc defaultSchema);
81     
82     public String JavaDoc toString() {
83         return getClass().getName() + '(' + getTable().getName() + getColumns() + ") as " + name;
84     }
85 }
86
Popular Tags