1 package org.apache.torque.engine.database.model; 2 3 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 37 public class Index 38 { 39 40 private static Log log = LogFactory.getLog(Index.class); 41 42 private String indexName; 43 44 private Table parentTable; 45 46 private List indexColumns; 47 48 52 public Index() 53 { 54 indexColumns = new ArrayList (3); 55 } 56 57 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 95 public void loadFromXML(Attributes attrib) 96 { 97 indexName = attrib.getValue("name"); 98 } 99 100 105 public boolean isUnique() 106 { 107 return false; 108 } 109 110 115 public String getName() 116 { 117 return indexName; 118 } 119 120 125 public void setName(String name) 126 { 127 this.indexName = name; 128 } 129 130 135 public void setTable(Table parent) 136 { 137 parentTable = parent; 138 } 139 140 145 public Table getTable() 146 { 147 return parentTable; 148 } 149 150 155 public String getTableName() 156 { 157 return parentTable.getName(); 158 } 159 160 165 public void addColumn(Attributes attrib) 166 { 167 indexColumns.add(attrib.getValue("name")); 168 } 169 170 175 public String getColumnList() 176 { 177 return Column.makeList(getColumns()); 178 } 179 180 185 public List getColumns() 186 { 187 return indexColumns; 188 } 189 190 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 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 } 234 | Popular Tags |