1 22 23 package org.xquark.extractor.algebra; 24 25 import java.util.*; 26 27 import org.xquark.extractor.common.Debug; 28 import org.xquark.extractor.common.SqlWrapperException; 29 import org.xquark.extractor.sql.SqlExpression; 30 31 public class Table extends Expression implements Relation { 32 33 private static final String RCSRevision = "$Revision: 1.5 $"; 34 35 private static final String RCSName = "$Name: $"; 36 37 protected org.xquark.extractor.metadata.Table _table; 38 39 protected List _attributes; 40 41 protected List _keys; 42 43 private Set _providedTableInstances = Collections.singleton(this); 44 45 49 protected List _usedKeysAttributeList = null; 50 51 public Table(org.xquark.extractor.metadata.Table table) { 52 setTable(table); 53 } 54 55 synchronized Object clone(Map clonedExprs) 56 throws CloneNotSupportedException { 57 Table retVal = (Table) super.clone(clonedExprs); 58 clonedExprs.put(this, retVal); 59 return retVal; 60 61 } 62 63 68 public org.xquark.extractor.metadata.Table getTable() { 69 return _table; 70 } 71 72 78 public void setTable(org.xquark.extractor.metadata.Table aTable) { 79 _table = aTable; 80 Debug.assertTrue(null != _table, "null!=_table"); 81 82 List mAttributes = _table.getChildren(); 83 84 85 _attributes = new ArrayList(); 86 87 org.xquark.extractor.metadata.Attribute mAttr; 88 Attribute attribute; 89 Iterator iter = mAttributes.iterator(); 90 while (iter.hasNext()) { 91 mAttr = (org.xquark.extractor.metadata.Attribute) iter.next(); 92 attribute = new Attribute(mAttr); 93 _attributes.add(attribute); 94 } 95 96 97 List keysInMetadata = null; 98 99 keysInMetadata = _table.getKeys(); 100 if (null != keysInMetadata) { 101 _keys = new ArrayList(); 102 List key = null; 103 Attribute keyAttribute = null; 104 105 List keyInMetadata = null; 106 org.xquark.extractor.metadata.Attribute keyAttributeInMetadata = null; 107 for (int i = 0; i < keysInMetadata.size(); i++) { 108 keyInMetadata = (List) keysInMetadata.get(i); 109 key = new ArrayList(); 110 for (int j = 0; j < keyInMetadata.size(); j++) { 111 keyAttributeInMetadata = (org.xquark.extractor.metadata.Attribute) keyInMetadata 112 .get(j); 113 114 if (keyAttributeInMetadata instanceof org.xquark.extractor.metadata.PseudoAttribute) { 115 if (keyAttributeInMetadata.getName().equalsIgnoreCase( 116 "rowid")) { 117 keyAttribute = new PseudoAttribute( 118 (org.xquark.extractor.metadata.PseudoAttribute) keyAttributeInMetadata); 119 } else { 120 Debug 121 .nyi("PseudoAttribute other than oracle rowid"); 122 } 123 } else if (keyAttributeInMetadata instanceof org.xquark.extractor.metadata.Attribute) { 124 keyAttribute = findAttribute(keyAttributeInMetadata); 125 } else { 126 Debug.nyi("unknow key attribute"); 127 } 128 129 Debug.assertTrue(null != keyAttribute, 130 "null != keyAttribute"); 131 key.add(keyAttribute); 132 } 133 _keys.add(key); 134 } 135 } 136 } 137 138 protected Attribute findAttribute( 139 org.xquark.extractor.metadata.Attribute attributeInMetadata) { 140 Attribute retVal = null; 141 142 org.xquark.extractor.metadata.Attribute cursor = null; 143 for (int i = 0; i < _attributes.size(); i++) { 144 cursor = ((Attribute) _attributes.get(i)).getAttribute(); 145 if (cursor.equals(attributeInMetadata)) { 146 retVal = (Attribute) _attributes.get(i); 147 break; 148 } 149 } 150 return retVal; 151 } 152 153 158 public List getAttributes() { 159 return _attributes; 160 } 161 162 public List getOperands() { 163 return null; 164 } 165 166 172 public void setAttributes(List aAttributes) { 173 _attributes = aAttributes; 174 } 175 176 public String getName() { 177 return _table.getName(); 178 } 179 180 public String getAlias() { 181 return _table.getAlias(); 182 } 183 184 public String getSchemaName() { 185 return _table.getFather().getName(); 186 } 187 188 public String getCatalogName() { 189 return _table.getFather().getFather().getName(); 190 } 191 192 public Set providedTableInstances() { 193 return _providedTableInstances; 194 } 195 196 public Set visibleTableInstances() { 197 return providedTableInstances(); 198 } 199 200 public AttributeExpression findNonNullAttribute() { 201 Iterator it = _attributes.iterator(); 202 Attribute att = null; 203 AttributeExpression ret = null; 204 205 while (it.hasNext() && ret == null) { 206 att = (Attribute) it.next(); 207 if (!att.getAttribute().getNullable()) { 208 ret = new AttributeExpression(null, att.getName()); 209 ret.setUnderlyingExpr(att); 210 } 211 } 212 return ret; 213 } 214 215 public List getKeys() { 216 return _keys; 217 } 218 219 public List nameTest(String name) { 220 List retVal = null; 221 222 if (name.equalsIgnoreCase("*")) { 223 retVal = _attributes; 224 } else if (null == name) { 225 retVal = null; 226 } else { 227 retVal = new ArrayList(); 228 229 Iterator iter = _attributes.iterator(); 230 Attribute item; 231 while (iter.hasNext()) { 232 item = (Attribute) iter.next(); 233 if (item.getAlias().equalsIgnoreCase(name)) { 234 retVal.add(item); 235 } 236 } 237 if (0 == retVal.size()) { 238 retVal = null; 239 } 240 } 241 242 return retVal; 243 } 244 245 public String pprintStructure() { 246 StringBuffer retVal = new StringBuffer (); 247 248 String newLine = System.getProperty("line.separator"); 249 250 retVal.append("Table : "); 251 retVal.append(_table.getName()); 252 retVal.append(newLine); 253 254 for (int i = 0; i < _attributes.size(); i++) { 255 retVal.append("\t"); 256 retVal.append(((Attribute) _attributes.get(i)).pprint()); 257 retVal.append(newLine); 258 } 259 return retVal.toString(); 260 } 261 262 public void setUsedKeyAttributeList(List list) { 263 _usedKeysAttributeList = list; 264 } 265 266 public List getUsedKeyAttributeList() { 267 return _usedKeysAttributeList; 268 } 269 270 public SqlExpression accept(GenSqlVisitor visitor) 271 throws SqlWrapperException { 272 return visitor.visit(this); 273 } 274 275 public void accept(AlgebraVisitor visitor) throws SqlWrapperException { 276 visitor.visit(this); 277 } 278 279 282 public boolean deepEquals(Object o) { 283 if (o instanceof Table) { 284 Table cast = (Table) o; 285 return _table.equals(cast.getTable()); } 289 return false; 290 } 291 } | Popular Tags |