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.HibernateException; 10 import org.hibernate.dialect.Dialect; 11 import org.hibernate.engine.Mapping; 12 import org.hibernate.util.StringHelper; 13 14 18 public class Index implements RelationalModel, Serializable { 19 20 private Table table; 21 private List columns = new ArrayList (); 22 private String name; 23 24 public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema) throws HibernateException { 25 return buildSqlCreateIndexString(dialect, getName(), getTable(), getColumnIterator(), false, defaultCatalog, defaultSchema); 26 } 27 28 public static String buildSqlDropIndexString(Dialect dialect, Table table, String name, String defaultCatalog, String defaultSchema) { 29 return "drop index " + StringHelper.qualify( table.getQualifiedName(dialect, defaultCatalog, defaultSchema), name); 30 } 31 public static String buildSqlCreateIndexString(Dialect dialect, String name, Table table, Iterator columns, boolean unique, String defaultCatalog, String defaultSchema) { 32 StringBuffer buf = new StringBuffer ("create") 33 .append( unique ? " unique " : "") 34 .append(" index ") 35 .append( dialect.qualifyIndexName() ? name : StringHelper.unqualify(name) ) 36 .append(" on ") 37 .append( table.getQualifiedName(dialect, defaultCatalog, defaultSchema) ) 38 .append(" ("); 39 Iterator iter = columns; 40 while ( iter.hasNext() ) { 41 buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); 42 if ( iter.hasNext() ) buf.append(", "); 43 } 44 buf.append(")"); 45 return buf.toString(); 46 } 47 48 49 public String sqlConstraintString(Dialect dialect) { 51 StringBuffer buf = new StringBuffer (" index ("); 52 Iterator iter = getColumnIterator(); 53 while ( iter.hasNext() ) { 54 buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); 55 if ( iter.hasNext() ) buf.append(", "); 56 } 57 return buf.append(')').toString(); 58 } 59 60 public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { 61 return "drop index " + StringHelper.qualify( table.getQualifiedName(dialect, defaultCatalog, defaultSchema), name); 62 } 63 64 public Table getTable() { 65 return table; 66 } 67 public void setTable(Table table) { 68 this.table = table; 69 } 70 public int getColumnSpan() { 71 return columns.size(); 72 } 73 public Iterator getColumnIterator() { 74 return columns.iterator(); 75 } 76 public void addColumn(Column column) { 77 if ( !columns.contains(column) ) columns.add(column); 78 } 79 public void addColumns(Iterator extraColumns) { 80 while ( extraColumns.hasNext() ) addColumn( (Column) extraColumns.next() ); 81 } 82 86 public boolean containsColumn(Column column) { 87 return columns.contains(column); 88 } 89 90 public String getName() { 91 return name; 92 } 93 public void setName(String name) { 94 this.name = name; 95 } 96 97 public String toString() { 98 99 return getClass().getName() + "(" + getName() + ")"; 100 } 101 } 102 | Popular Tags |