KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at 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
30 public final class AddKeys extends DefaultAlgebraVisitor {
31     private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
32     private static final String JavaDoc RCSName = "$Name: $";
33
34     private Stack _returnStack = new Stack();
35     private boolean _cleBidon = false;
36
37     public AddKeys() {
38     }
39
40     public void reinit() {
41         _returnStack.clear();
42         _cleBidon = false;
43     }
44
45     public boolean isHeavyEliminationNeeded() {
46         return _cleBidon;
47     }
48
49     /**
50      * @return a list of keys, each key is a Expression
51      */

52     public List getKeys() {
53         List retVal = (List) _returnStack.peek();
54         Debug.assertTrue(retVal.size() > 0, "retVal.size()>0");
55         return retVal;
56     }
57
58     public void visit(DummyTable arg) throws SqlWrapperException {
59         // TODO: Change it ?: keys == null is used to say if a relation is Dummy by MySQL rewriting
60
// throw new SqlWrapperException(MessageLibrary.getMessage("IE_ERR","DummyTable can not provide key."));
61
}
62
63     public void visit(Expression arg) throws SqlWrapperException {
64         //Trace.enter(this,"visit(Expression arg)", arg);
65
Debug.assertTrue(false, "AddKeys.visit() not implemented for " + arg.pprint());
66         //Trace.exit(this,"visit(Expression arg)", arg);
67
}
68
69     public void visit(BinOpOuterJoin arg) throws SqlWrapperException {
70         //Trace.enter(this,"visit(BinOpOuterJoin arg)", arg);
71
List allKeys = new ArrayList();
72
73         List keys;
74         Expression operand;
75
76         operand = arg.getLeftOperand();
77         operand.accept(this);
78         keys = (List) _returnStack.pop();
79         allKeys.addAll(keys);
80
81         operand = arg.getRightOperand();
82         operand.accept(this);
83         keys = (List) _returnStack.pop();
84         allKeys.addAll(keys);
85
86         arg.setKeys(allKeys);
87         _returnStack.push(allKeys);
88         //Trace.exit(this,"visit(BinOpOuterJoin arg)", arg);
89
}
90
91     public void visit(Join arg) throws SqlWrapperException {
92         //Trace.enter(this,"visit(Join arg)", arg);
93

94         List allKeys = new ArrayList();
95         List keys;
96
97         Iterator iter = arg.getOperandList().iterator();
98         Expression operand = null;
99         while (iter.hasNext()) {
100             operand = (Expression) iter.next();
101             operand.accept(this);
102             keys = (List) _returnStack.pop();
103             allKeys.addAll(keys);
104         }
105
106         arg.setKeys(allKeys);
107         _returnStack.push(allKeys);
108         //Trace.exit(this,"visit(Join arg)", arg);
109

110     }
111
112     public void visit(RenameRelation arg) throws SqlWrapperException {
113         //Trace.enter(this,"visit(RenameRelation arg)", arg);
114

115         arg.getOperand().accept(this);
116         List allKeys = (List) _returnStack.peek();
117
118         List keys = null;
119         try {
120             keys = AlgebraTools.clone(allKeys); // QUESTION: why cloning ?
121
} catch (CloneNotSupportedException JavaDoc ex) {
122             throw new SqlWrapperException(ex.getMessage(), ex);
123         }
124
125         /* set the _tableInstance property of all AttributeExpression to current node */
126         Iterator iter = keys.iterator();
127         while (iter.hasNext()) {
128             ((AttributeExpression) iter.next()).setTableInstance(arg);
129         }
130
131         arg.setKeys(keys);
132         _returnStack.push(keys);
133         //Trace.exit(this,"visit(RenameRelation arg)", arg);
134
}
135
136     public void visit(Table arg) throws SqlWrapperException {
137         //Trace.enter(this,"visit(Table arg)", arg);
138

139         List allKeys = new ArrayList();
140         /** @todo from non-oracle DB */
141         List keysList = arg.getKeys();
142         List key = null;
143         if (null == keysList || keysList.isEmpty()) {
144             key = arg.getAttributes();
145             _cleBidon = true;
146         } else
147             key = (List) keysList.get(0); // TODO: do not take the pseudo-id if PK is a single column ? (to limit column retrieval)
148

149         arg.setUsedKeyAttributeList(key);
150
151         Attribute keyAttribute = null;
152         AttributeExpression keyAttributeExpression = null;
153
154         for (int i = 0; i < key.size(); i++) {
155             keyAttribute = (Attribute) key.get(i);
156             if (keyAttribute instanceof PseudoAttribute) {
157                 keyAttributeExpression = new Rowid(null, keyAttribute.getName());
158             } else {
159                 keyAttributeExpression = new AttributeExpression(null, keyAttribute.getName());
160             }
161             keyAttributeExpression.setUnderlyingExpr(keyAttribute);
162             allKeys.add(keyAttributeExpression);
163         }
164
165         _returnStack.push(allKeys);
166         //Trace.exit(this,"visit(Table arg)", arg);
167
}
168
169     public void visit(UnaryOperator arg) throws SqlWrapperException {
170         //Trace.enter(this,"visit(UnaryOperator arg)", arg);
171
arg.getOperand().accept(this);
172         //Trace.exit(this,"visit(UnaryOperator arg)", arg);
173
}
174
175     public void visit(UnOpProject arg) throws SqlWrapperException {
176         //Trace.enter(this,"visit(UnOpProject arg)", arg);
177

178         List retVal = null;
179         AttributeExpression newAttrItem;
180
181         if (arg.getDistinct()) {
182             /* if the UnOproject is SELECT DISTINCT:
183             * 1 we can not add other columns to the select List .
184             * 2 there will not be duplicate tuples in the rerult ,
185             * so the select List can play the role of key
186             */

187             List selectList = arg.getItemList();
188             List allKeys = new ArrayList();
189             Iterator iter = selectList.iterator();
190             Expression item;
191             while (iter.hasNext()) {
192                 item = (Expression) iter.next();
193                 newAttrItem = new AttributeExpression(null, item.getName());
194                 newAttrItem.setUnderlyingExpr(item);
195                 allKeys.add(newAttrItem);
196             }
197             retVal = allKeys;
198         } else {
199             /* if the UnOproject is SELECT ALL:
200             * add key columns to the select List .
201             */

202             if (null == arg.getKeys()) {
203                 arg.getOperand().accept(this);
204                 List keys = (List) _returnStack.pop();
205
206                 List newKeys = new ArrayList();
207
208                 Iterator iter = keys.iterator();
209                 AttributeExpression newItem;
210                 Expression selectItem, keyItem;
211                 List selectList = arg.getItemList();
212                 while (iter.hasNext()) {
213                     keyItem = (AttributeExpression) iter.next();
214                     selectItem = UnOpProject.identicalExpr(selectList, (AttributeExpression) keyItem);
215                     if (null == selectItem) {
216                         keyItem = arg.addItem(keyItem);
217                     } else {
218                         keyItem = selectItem;
219                     }
220                     newAttrItem = new AttributeExpression(null, keyItem.getName());
221                     newAttrItem.setUnderlyingExpr(keyItem);
222
223                     newKeys.add(newAttrItem);
224                 }
225                 arg.setKeys(newKeys);
226                 retVal = newKeys;
227             } else {
228                 retVal = arg.getKeys();
229             }
230         }
231
232         _returnStack.push(retVal);
233         //Trace.exit(this,"visit(UnOpProject arg)", arg);
234
}
235
236     public void visit(UnOpGroup arg) throws SqlWrapperException {
237         //Trace.enter(this,"visit(UnOpGroup arg)", arg);
238
List keys = null;
239
240         try {
241             keys = AlgebraTools.clone(arg.getKeys());
242         } catch (CloneNotSupportedException JavaDoc ex) {
243             throw new SqlWrapperException(ex.getMessage(), ex);
244         }
245
246         _returnStack.push(keys);
247         //Trace.exit(this,"visit(UnOpGroup arg)", arg);
248
}
249 }
250
Popular Tags