View Javadoc
1 package org.apache.torque.engine.database.model; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.util.ArrayList; 58 import java.util.Iterator; 59 import java.util.List; 60 61 import org.apache.commons.logging.Log; 62 import org.apache.commons.logging.LogFactory; 63 64 import org.apache.torque.engine.EngineException; 65 66 import org.xml.sax.Attributes; 67 68 /*** 69 * Information about indices of a table. 70 * 71 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 72 * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a> 73 * @version $Id: Index.java,v 1.2 2003/06/26 06:59:56 mpoeschl Exp $ 74 */ 75 public class Index 76 { 77 /*** Logging class from commons.logging */ 78 private static Log log = LogFactory.getLog(Index.class); 79 /*** name of the index */ 80 private String indexName; 81 /*** table */ 82 private Table parentTable; 83 /*** columns */ 84 private List indexColumns; 85 86 /*** 87 * Creates a new instance with default characteristics (no name or 88 * parent table, small column list size allocation, non-unique). 89 */ 90 public Index() 91 { 92 indexColumns = new ArrayList(3); 93 } 94 95 /*** 96 * Creates a new instance for the list of columns composing an 97 * index. Otherwise performs as {@link #Index()}. 98 * 99 * @param table The table this index is associated with. 100 * @param indexColumns The list of {@link 101 * org.apache.torque.engine.database.model.Column} objects which 102 * make up this index. Cannot be empty. 103 * @exception EngineException Error generating name. 104 * @see #Index() 105 */ 106 protected Index(Table table, List indexColumns) 107 throws EngineException 108 { 109 this(); 110 setTable(table); 111 if (indexColumns.size() > 0) 112 { 113 this.indexColumns = indexColumns; 114 createName(); 115 116 if (log.isDebugEnabled()) 117 { 118 log.debug("Created Index named " + getName() 119 + " with " + indexColumns.size() + " columns"); 120 } 121 } 122 else 123 { 124 throw new EngineException("Cannot create a new Index using an " 125 + "empty list Column object"); 126 } 127 } 128 129 /*** 130 * Creates a name for the index using the NameFactory. 131 * 132 * @throws EngineException if the name could not be created 133 */ 134 private void createName() throws EngineException 135 { 136 Table table = getTable(); 137 List inputs = new ArrayList(4); 138 inputs.add(table.getDatabase()); 139 inputs.add(table.getName()); 140 if (isUnique()) 141 { 142 inputs.add("U"); 143 } 144 else 145 { 146 inputs.add("I"); 147 } 148 // ASSUMPTION: This Index not yet added to the list. 149 inputs.add(new Integer(table.getIndices().length + 1)); 150 indexName = NameFactory.generateName( 151 NameFactory.CONSTRAINT_GENERATOR, inputs); 152 } 153 154 /*** 155 * Imports index from an XML specification 156 * 157 * @param attrib the xml attributes 158 */ 159 public void loadFromXML(Attributes attrib) 160 { 161 indexName = attrib.getValue("name"); 162 } 163 164 /*** 165 * Returns the uniqueness of this index. 166 * 167 * @return the uniqueness of this index 168 */ 169 public boolean isUnique() 170 { 171 return false; 172 } 173 174 /*** 175 * Gets the name of this index. 176 * 177 * @return the name of this index 178 */ 179 public String getName() 180 { 181 if (indexName == null) 182 { 183 try 184 { 185 // generate an index name if we don't have a supplied one 186 createName(); 187 } 188 catch (EngineException e) 189 { 190 // still no name 191 } 192 } 193 return indexName; 194 } 195 196 /*** 197 * Set the name of this index. 198 * 199 * @param name the name of this index 200 */ 201 public void setName(String name) 202 { 203 this.indexName = name; 204 } 205 206 /*** 207 * Set the parent Table of the index 208 * 209 * @param parent the table 210 */ 211 public void setTable(Table parent) 212 { 213 parentTable = parent; 214 } 215 216 /*** 217 * Get the parent Table of the index 218 * 219 * @return the table 220 */ 221 public Table getTable() 222 { 223 return parentTable; 224 } 225 226 /*** 227 * Returns the Name of the table the index is in 228 * 229 * @return the name of the table 230 */ 231 public String getTableName() 232 { 233 return parentTable.getName(); 234 } 235 236 /*** 237 * Adds a new column to an index. 238 * 239 * @param attrib xml attributes for the column 240 */ 241 public void addColumn(Attributes attrib) 242 { 243 indexColumns.add(attrib.getValue("name")); 244 } 245 246 /*** 247 * Return a comma delimited string of the columns which compose this index. 248 * 249 * @return a list of column names 250 */ 251 public String getColumnList() 252 { 253 return Column.makeList(getColumns()); 254 } 255 256 /*** 257 * Return the list of local columns. You should not edit this list. 258 * 259 * @return a list of columns 260 */ 261 public List getColumns() 262 { 263 return indexColumns; 264 } 265 266 /*** 267 * Returns the list of names of the columns referenced by this 268 * index. Slightly over-allocates the list's buffer (just in case 269 * more elements are going to be added, such as when a name is 270 * being generated). Feel free to modify this list. 271 * 272 * @return a list of column names 273 */ 274 protected List getColumnNames() 275 { 276 List names = new ArrayList(indexColumns.size() + 2); 277 Iterator i = getColumns().iterator(); 278 while (i.hasNext()) 279 { 280 Column c = (Column) i.next(); 281 names.add(c.getName()); 282 } 283 return names; 284 } 285 286 /*** 287 * String representation of the index. This is an xml representation. 288 * 289 * @return a xml representation 290 */ 291 public String toString() 292 { 293 StringBuffer result = new StringBuffer(); 294 result.append(" <index name=\"") 295 .append(getName()) 296 .append("\""); 297 298 result.append(">\n"); 299 300 for (int i = 0; i < indexColumns.size(); i++) 301 { 302 result.append(" <index-column name=\"") 303 .append(indexColumns.get(i)) 304 .append("\"/>\n"); 305 } 306 result.append(" </index>\n"); 307 return result.toString(); 308 } 309 }

This page was automatically generated by Maven