View Javadoc

1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.torque.engine.EngineException;
27  
28  import org.xml.sax.Attributes;
29  
30  /***
31   * Information about indices of a table.
32   *
33   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
34   * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
35   * @version $Id: Index.java 239624 2005-08-24 12:18:03Z henning $
36   */
37  public class Index
38  {
39      /*** Logging class from commons.logging */
40      private static Log log = LogFactory.getLog(Index.class);
41      /*** name of the index */
42      private String indexName;
43      /*** table */
44      private Table parentTable;
45      /*** columns */
46      private List indexColumns;
47  
48      /***
49       * Creates a new instance with default characteristics (no name or
50       * parent table, small column list size allocation, non-unique).
51       */
52      public Index()
53      {
54          indexColumns = new ArrayList(3);
55      }
56  
57      /***
58       * Creates a new instance for the list of columns composing an
59       * index.  Otherwise performs as {@link #Index()}.
60       *
61       * @param table The table this index is associated with.
62       * @param indexColumns The list of {@link
63       * org.apache.torque.engine.database.model.Column} objects which
64       * make up this index.  Cannot be empty.
65       * @exception EngineException Error generating name.
66       * @see #Index()
67       */
68      protected Index(Table table, List indexColumns)
69          throws EngineException
70      {
71          this();
72          setTable(table);
73          if (!indexColumns.isEmpty())
74          {
75              this.indexColumns = indexColumns;
76  
77              if (log.isDebugEnabled())
78              {
79                  log.debug("Created Index named " + getName()
80                          + " with " + indexColumns.size() + " columns");
81              }
82          }
83          else
84          {
85              throw new EngineException("Cannot create a new Index using an "
86                      + "empty list Column object");
87          }
88      }
89  
90      /***
91       * Imports index from an XML specification
92       *
93       * @param attrib the xml attributes
94       */
95      public void loadFromXML(Attributes attrib)
96      {
97          indexName = attrib.getValue("name");
98      }
99  
100     /***
101      * Returns the uniqueness of this index.
102      *
103      * @return the uniqueness of this index
104      */
105     public boolean isUnique()
106     {
107         return false;
108     }
109 
110     /***
111      * Gets the name of this index.
112      *
113      * @return the name of this index
114      */
115     public String getName()
116     {
117         return indexName;
118     }
119 
120     /***
121      * Set the name of this index.
122      *
123      * @param name the name of this index
124      */
125     public void setName(String name)
126     {
127         this.indexName = name;
128     }
129 
130     /***
131      * Set the parent Table of the index
132      *
133      * @param parent the table
134      */
135     public void setTable(Table parent)
136     {
137         parentTable = parent;
138     }
139 
140     /***
141      * Get the parent Table of the index
142      *
143      * @return the table
144      */
145     public Table getTable()
146     {
147         return parentTable;
148     }
149 
150     /***
151      * Returns the Name of the table the index is in
152      *
153      * @return the name of the table
154      */
155     public String getTableName()
156     {
157         return parentTable.getName();
158     }
159 
160     /***
161      * Adds a new column to an index.
162      *
163      * @param attrib xml attributes for the column
164      */
165     public void addColumn(Attributes attrib)
166     {
167         indexColumns.add(attrib.getValue("name"));
168     }
169 
170     /***
171      * Return a comma delimited string of the columns which compose this index.
172      *
173      * @return a list of column names
174      */
175     public String getColumnList()
176     {
177         return Column.makeList(getColumns());
178     }
179 
180     /***
181      * Return the list of local columns. You should not edit this list.
182      *
183      * @return a list of columns
184      */
185     public List getColumns()
186     {
187         return indexColumns;
188     }
189 
190     /***
191      * Returns the list of names of the columns referenced by this
192      * index.  Slightly over-allocates the list's buffer (just in case
193      * more elements are going to be added, such as when a name is
194      * being generated).  Feel free to modify this list.
195      *
196      * @return a list of column names
197      */
198     protected List getColumnNames()
199     {
200         List names = new ArrayList(indexColumns.size() + 2);
201         Iterator i = getColumns().iterator();
202         while (i.hasNext())
203         {
204             Column c = (Column) i.next();
205             names.add(c.getName());
206         }
207         return names;
208     }
209 
210     /***
211      * String representation of the index. This is an xml representation.
212      *
213      * @return a xml representation
214      */
215     public String toString()
216     {
217         StringBuffer result = new StringBuffer();
218         result.append(" <index name=\"")
219               .append(getName())
220               .append("\"");
221 
222         result.append(">\n");
223 
224         for (int i = 0; i < indexColumns.size(); i++)
225         {
226             result.append("  <index-column name=\"")
227                 .append(indexColumns.get(i))
228                 .append("\"/>\n");
229         }
230         result.append(" </index>\n");
231         return result.toString();
232     }
233 }