1 package org.hibernate.mapping; 3 4 import java.io.Serializable ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.hibernate.dialect.Dialect; 10 import org.hibernate.engine.Mapping; 11 12 16 public abstract class Constraint implements RelationalModel, Serializable { 17 18 private String name; 19 private final List columns = new ArrayList (); 20 private Table table; 21 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public Iterator 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 columnIterator) { 35 while ( columnIterator.hasNext() ) { 36 Selectable col = (Selectable) columnIterator.next(); 37 if ( !col.isFormula() ) addColumn( (Column) col ); 38 } 39 } 40 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 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 sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { 66 return "alter table " + getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) + " drop constraint " + getName(); 67 } 68 69 public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) { 70 StringBuffer buf = new StringBuffer ("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 sqlConstraintString(Dialect d, String constraintName, String defaultCatalog, String defaultSchema); 81 82 public String toString() { 83 return getClass().getName() + '(' + getTable().getName() + getColumns() + ") as " + name; 84 } 85 } 86 | Popular Tags |