KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > Table


1 /*
2  * This file belongs to the XQuark distribution. Copyright (C) 2003 Universite
3  * de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the Free
7  * Software Foundation; either version 2.1 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it at
18  * http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

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 JavaDoc RCSRevision = "$Revision: 1.5 $";
34
35     private static final String JavaDoc 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     /*
46      * when a key is once used(asked by AddKeys), it is is recorded in
47      * _usedKeysAttributeList
48      */

49     protected List _usedKeysAttributeList = null;
50
51     public Table(org.xquark.extractor.metadata.Table table) {
52         setTable(table);
53     }
54
55     synchronized Object JavaDoc clone(Map clonedExprs)
56             throws CloneNotSupportedException JavaDoc {
57         Table retVal = (Table) super.clone(clonedExprs);
58         clonedExprs.put(this, retVal);
59         return retVal;
60
61     }
62
63     /**
64      * Access method for the _table property.
65      *
66      * @return the current value of the _table property
67      */

68     public org.xquark.extractor.metadata.Table getTable() {
69         return _table;
70     }
71
72     /**
73      * Sets the value of the _table property.
74      *
75      * @param aTable
76      * the new value of the _table property
77      */

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         /* create _attributes */
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         /* create _keys */
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     /**
154      * Access method for the _attributes property.
155      *
156      * @return the current value of the _attributes property
157      */

158     public List getAttributes() {
159         return _attributes;
160     }
161
162     public List getOperands() {
163         return null;
164     }
165
166     /**
167      * Sets the value of the _attributes property.
168      *
169      * @param aAttributes
170      * the new value of the _attributes property
171      */

172     public void setAttributes(List aAttributes) {
173         _attributes = aAttributes;
174     }
175
176     public String JavaDoc getName() {
177         return _table.getName();
178     }
179
180     public String JavaDoc getAlias() {
181         return _table.getAlias();
182     }
183
184     public String JavaDoc getSchemaName() {
185         return _table.getFather().getName();
186     }
187
188     public String JavaDoc 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 JavaDoc 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 JavaDoc pprintStructure() {
246         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
247
248         String JavaDoc 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     /**
280      * @see Expression#deepEquals(Object)
281      */

282     public boolean deepEquals(Object JavaDoc o) {
283         if (o instanceof Table) {
284             Table cast = (Table) o;
285             return _table.equals(cast.getTable()); // NOTE: assuming metadata
286
// object are not
287
// duplicated...
288
}
289         return false;
290     }
291 }
Popular Tags