KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > progress > ProgressGenSqlVisitor


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.progress;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.xquark.extractor.algebra.*;
30 import org.xquark.extractor.common.Debug;
31 import org.xquark.extractor.common.SqlWrapperException;
32 import org.xquark.extractor.runtime.IDProvider;
33 import org.xquark.extractor.sql.*;
34 import org.xquark.extractor.xfunctions.XfContains;
35
36 public final class ProgressGenSqlVisitor extends AbstractGenSqlVisitor {
37     private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
38     private static final String JavaDoc RCSName = "$Name: $";
39
40     public ProgressGenSqlVisitor(IDProvider relIDProvider) {
41         super((SqlFactory) ProgressFactory.getInstance(), relIDProvider);
42     }
43
44     public SqlExpression visit(AttributeExpression arg) {
45         //Trace.enter(this, "visit(AttributeExpression arg)");
46

47         SqlExpression retVal = null;
48         String JavaDoc attrName = arg.getName();
49
50         String JavaDoc tableInstanceName;
51         RenameRelation rr = arg.getTableInstance();
52         if (null != rr) {
53             tableInstanceName = rr.getUniqueName();
54         } else {
55             tableInstanceName = null;
56         }
57
58         if (attrName.equals("*")) {
59             retVal = _factory.createAttributeExpression(null, attrName);
60         } else {
61             retVal = _factory.createAttributeExpression(tableInstanceName, attrName);
62         }
63         //Trace.exit(this, "visit(AttributeExpression arg)");
64
return retVal;
65     }
66
67     public SqlExpression visit(BinOpArithmetic arg) {
68         //Trace.enter(this, "visit(BinOpArithmetic arg)");
69

70         SqlExpression retVal = null;
71
72         Expression aLOprnd = arg.getLeftOperand();
73         SqlExpression sLOprnd = aLOprnd.accept(this);
74
75         Expression aROprnd = arg.getRightOperand();
76         SqlExpression sROprnd = aROprnd.accept(this);
77
78         int oprtr = arg.getOperator();
79
80         retVal = _factory.createBinOpArithmetic(oprtr, sLOprnd, sROprnd);
81
82         if (false /** @todo INT_DIVIDE_ARITHMETICS == oprtr &&
83                     aLOprnd.getType().isInteger() &&
84                     aROprnd.getType().isInteger()*/

85             ) {
86             /** @todo pay attention that the round funcion on negative values */
87             retVal = _factory.createSfRound(retVal);
88         }
89
90         //Trace.exit(this, "visit(BinOpArithmetic arg)");
91
return retVal;
92     }
93
94     public SqlExpression visit(SortSpecification arg) {
95         //Trace.enter(this, "visit(SortSpecification arg)", arg);
96
SqlSortSpecification retVal = null;
97         Expression sortOperand = ((UnOpSort) arg.getFather()).getOperand();
98         if (sortOperand instanceof BinaryAlgebra) {
99             Expression sortExpr = arg.getSortExpression();
100
101             SqlExpression sqlSortExpr = sortExpr.accept(this);
102
103             retVal = _factory.createSortSpecification(sqlSortExpr, arg.getSortDirection());
104         } else {
105             Expression sortExpr = arg.getSortExpression();
106             Expression underlyingExpr = null;
107             if (sortExpr instanceof AttributeExpression) {
108                 underlyingExpr = ((AttributeExpression) sortExpr).getUnderlyinExpr();
109                 if (underlyingExpr instanceof AttributeExpression) {
110                     sortExpr = underlyingExpr;
111                 } else if (underlyingExpr instanceof RenameItem) {
112                     Expression operandOfUnderlyingExpr = ((RenameItem) underlyingExpr).getOperand();
113                     if (operandOfUnderlyingExpr instanceof AttributeExpression) {
114                         sortExpr = operandOfUnderlyingExpr;
115                     }
116                 }
117             }
118             SqlExpression sqlSortExpr = sortExpr.accept(this);
119
120             retVal = _factory.createSortSpecification(sqlSortExpr, arg.getSortDirection());
121         }
122         //Trace.exit(this, "visit(SortSpecification arg)", arg);
123
return retVal;
124     }
125
126     public SqlExpression visit(XfContains arg) throws SqlWrapperException {
127         //Trace.enter(this, "visit(XfContains arg)");
128

129         Debug.assertTrue(2 == arg.getArgumentNumber(), "2 == arg.getArgumentNumber()");
130         org.xquark.extractor.progress.sql.SfInStr inStr = new org.xquark.extractor.progress.sql.SfInStr();
131
132         Expression aArg0 = (Expression) arg.getArgument(0);
133         SqlExpression sArg0 = aArg0.accept(this);
134
135         Expression aArg1 = (Expression) arg.getArgument(1);
136         SqlExpression sArg1 = aArg1.accept(this);
137
138         inStr.setArgument(0, sArg0);
139         inStr.setArgument(1, sArg1);
140
141         SqlBinOpCompare retVal = _factory.createBinOpCompare(GT_COMPOP, inStr, _factory.createLitInteger(new Integer JavaDoc(0)));
142
143         //Trace.exit(this, "visit(XfContains arg)");
144
return retVal;
145     }
146
147     protected org.xquark.extractor.sql.SqlSelect selectAddFromClause(org.xquark.extractor.sql.SqlSelect select, BinOpOuterJoin arg) {
148         //Trace.enter(this, "selectAddFromClause (SqlSelect select, BinOpOuterJoin arg)", arg);
149

150         org.xquark.extractor.sql.SqlExpression newFrom = arg.accept(this);
151         select.addFromClause(newFrom);
152
153         //Trace.exit(this, "selectAddFromClause (SqlSelect select, BinOpOuterJoin arg)", arg);
154
return select;
155     }
156
157     public org.xquark.extractor.sql.SqlExpression visit(BinOpOuterJoin arg) {
158         //Trace.enter(this, "visit(BinOpOuterJoin arg)");
159

160         Expression[] aOprnds = { arg.getLeftOperand(), arg.getRightOperand()};
161         org.xquark.extractor.sql.SqlExpression[] sOprnd = new SqlExpression[2];
162         Set JavaDoc providedTableInstances;
163
164         /* Generates operands */
165         for (int i = 0; i < aOprnds.length; i++) {
166             providedTableInstances = ((Relation) aOprnds[i]).providedTableInstances();
167             if (1 == providedTableInstances.size()) {
168                 // Debug.assertTrue(ti instanceof RenameRelation, "ti instanceof RenameRelation");
169
RenameRelation ti = (RenameRelation) providedTableInstances.toArray()[0];
170
171                 if (!(aOprnds[i] instanceof RenameRelation)) {
172                     aOprnds[i] = new RenameRelation(aOprnds[i], ti.getName());
173                 }
174
175                 sOprnd[i] = aOprnds[i].accept(this);
176             } else if (aOprnds[i] instanceof BinOpOuterJoin) {
177                 sOprnd[i] = aOprnds[i].accept(this);
178             } else {
179                 Debug.nyi("complex outerjoin");
180             }
181         }
182
183         int outerJoinType = arg.getJoinType();
184
185         /* Outer join predicate creation */
186         List JavaDoc aPredicateList = arg.getPredicateList();
187         List JavaDoc sPredicateList = null;
188         if (null != aPredicateList) {
189             /* translate predicates */
190             sPredicateList = new ArrayList JavaDoc();
191             Expression aPredicate = null;
192             org.xquark.extractor.sql.SqlExpression sPredicate = null;
193
194             for (int i = 0; i < aPredicateList.size(); i++) {
195                 aPredicate = (Expression) aPredicateList.get(i);
196                 sPredicate = aPredicate.accept(this);
197                 sPredicateList.add(sPredicate);
198             }
199         }
200
201         /* Outer join clause creation */
202         org.xquark.extractor.sql.SqlExpression retVal = new org.xquark.extractor.sql.SqlBinOpOuterJoin(sOprnd[0], sOprnd[1], outerJoinType, sPredicateList);
203
204         //Trace.enter(this, "visit(BinOpOuterJoin arg)");
205
return retVal;
206     }
207
208     protected org.xquark.extractor.sql.SqlSelect selectAddFromClause(org.xquark.extractor.sql.SqlSelect select, DummyTable operand) {
209         //Trace.enter(this, "selectAddFromClause (SqlSelect select, DummyTable operand)");
210

211         org.xquark.extractor.sql.SqlTable newFrom = _factory.createTable("dual");
212         select.addFromClause(newFrom);
213
214         //Trace.exit(this, "selectAddFromClause (SqlSelect select, DummyTable operand)");
215
return select;
216     }
217 }
218
Popular Tags