KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Index.java,v 1.6 2005/02/14 10:59:35 maxcsaucdk 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.HibernateException;
10 import org.hibernate.dialect.Dialect;
11 import org.hibernate.engine.Mapping;
12 import org.hibernate.util.StringHelper;
13
14 /**
15  * A relational table index
16  * @author Gavin King
17  */

18 public class Index implements RelationalModel, Serializable JavaDoc {
19
20     private Table table;
21     private List columns = new ArrayList JavaDoc();
22     private String JavaDoc name;
23     
24     public String JavaDoc sqlCreateString(Dialect dialect, Mapping mapping, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) throws HibernateException {
25         return buildSqlCreateIndexString(dialect, getName(), getTable(), getColumnIterator(), false, defaultCatalog, defaultSchema);
26     }
27     
28     public static String JavaDoc buildSqlDropIndexString(Dialect dialect, Table table, String JavaDoc name, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) {
29         return "drop index " + StringHelper.qualify( table.getQualifiedName(dialect, defaultCatalog, defaultSchema), name);
30     }
31     public static String JavaDoc buildSqlCreateIndexString(Dialect dialect, String JavaDoc name, Table table, Iterator JavaDoc columns, boolean unique, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) {
32         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("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 JavaDoc 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     // Used only in Table for sqlCreateString (but commented out at the moment)
50
public String JavaDoc sqlConstraintString(Dialect dialect) {
51         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(" index (");
52         Iterator JavaDoc 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 JavaDoc sqlDropString(Dialect dialect, String JavaDoc defaultCatalog, String JavaDoc 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 JavaDoc 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 JavaDoc extraColumns) {
80         while ( extraColumns.hasNext() ) addColumn( (Column) extraColumns.next() );
81     }
82     /**
83      * @param column
84      * @return true if this constraint already contains a column with same name.
85      */

86     public boolean containsColumn(Column column) {
87         return columns.contains(column);
88     }
89     
90     public String JavaDoc getName() {
91         return name;
92     }
93     public void setName(String JavaDoc name) {
94         this.name = name;
95     }
96
97     public String JavaDoc toString() {
98         
99         return getClass().getName() + "(" + getName() + ")";
100     }
101 }
102
Popular Tags