KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > microsoft > MicrosoftGenSqlVisitor


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.microsoft;
24
25 import org.xquark.extractor.algebra.*;
26 import org.xquark.extractor.runtime.IDProvider;
27 import org.xquark.extractor.sql.*;
28 import org.xquark.jdbc.typing.DbType;
29
30 public class MicrosoftGenSqlVisitor extends AbstractGenSqlVisitor {
31
32     private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35
36     public MicrosoftGenSqlVisitor(IDProvider relIDProvider) {
37         super((SqlFactory)MicrosoftFactory.getInstance(), relIDProvider);
38     }
39
40     protected SqlSelect selectAddFromClause (SqlSelect select, DummyTable operand)
41     {
42         //Trace.enter(this,"selectAddFromClause (SqlSelect select, DummyTable operand)");
43

44         //Trace.exit(this,"selectAddFromClause (SqlSelect select, DummyTable operand)");
45
return select;
46     }
47     public SqlExpression visit(BinOpArithmetic arg)
48     {
49         //Trace.enter(this,"visit(BinOpArithmetic arg)");
50

51         SqlExpression retVal = null;
52
53         Expression aLOprnd = arg.getLeftOperand();
54         SqlExpression sLOprnd = aLOprnd.accept(this);
55
56         Expression aROprnd = arg.getRightOperand();
57         SqlExpression sROprnd = aROprnd.accept(this);
58
59         int oprtr = arg.getOperator();
60
61         if (MODULO_ARITHMETICS == oprtr ) {
62             retVal = visitModulo(sLOprnd, (SqlTypeAtom)aLOprnd.getType(), sROprnd, (SqlTypeAtom)aROprnd.getType());
63         }
64         else {
65             if ( DIVIDE_ARITHMETICS == oprtr &&
66                 aLOprnd.getType().isInteger() &&
67                 aROprnd.getType().isInteger() ) {
68                 /* SQL Server will execute integer division while XQuery expect decimal result.
69                    So we cast left operand to decimal type before the division */

70                 sLOprnd = _factory.createConvert(sLOprnd,
71                             new DbType(java.sql.Types.INTEGER),
72                             new DbType(java.sql.Types.DECIMAL));
73             }
74             retVal = _factory.createBinOpArithmetic(oprtr,sLOprnd, sROprnd);
75             retVal = _factory.createBinOpArithmetic(oprtr,sLOprnd, sROprnd);
76         }
77
78         //Trace.exit(this,"visit(BinOpArithmetic arg)");
79
return retVal;
80     }
81
82     protected SqlExpression visitModulo(SqlExpression lOprnd, SqlTypeAtom lOprndType,
83                                         SqlExpression rOprnd, SqlTypeAtom rOprndType) {
84         SqlBinOpArithmetic retVal = null;
85         if (lOprndType.isInteger() && rOprndType.isInteger()) {
86             retVal = _factory.createBinOpArithmetic(MODULO_ARITHMETICS,lOprnd,rOprnd);
87         }
88         else {
89             /* sign(lOprnd) * ( abs(lOprnd)- floor(abs(lOprnd/rOprnd))*abs(rOprnd) */
90
91             SfSign signL = _factory.createSfSign(lOprnd);
92
93             SfAbs absL = _factory.createSfAbs(lOprnd);
94
95             SqlBinOpArithmetic LdivR = _factory.createBinOpArithmetic(DIVIDE_ARITHMETICS,lOprnd, rOprnd);
96             SfAbs absLdivR = _factory.createSfAbs(LdivR);
97             SfFloor floorAbs = _factory.createSfFloor(absLdivR);
98             SfAbs absR = _factory.createSfAbs(rOprnd);
99
100             SqlBinOpArithmetic floorMultiR = _factory.createBinOpArithmetic(MULTIPLY_ARITHMETICS,floorAbs, absR);
101
102             SqlBinOpArithmetic AbsMinusFloor = _factory.createBinOpArithmetic(MINUS_ARITHMETICS,absL, floorMultiR);
103
104             retVal = _factory.createBinOpArithmetic(MULTIPLY_ARITHMETICS,signL, AbsMinusFloor);
105         }
106         return retVal;
107     }
108
109
110     public SqlExpression visit(SortSpecification arg)
111     {
112         //Trace.enter(this,"visit(SortSpecification arg)");
113
SqlSortSpecification retVal = null;
114         Expression sortOperand = ((UnOpSort)arg.getFather()).getOperand();
115         if (sortOperand instanceof BinaryAlgebra && !(sortOperand instanceof BinOpOuterJoin)) {
116             Expression sortExpr = arg.getSortExpression();
117     // if (sortExpr instanceof AttributeExpression) {
118
// sortExpr = ((AttributeExpression)sortExpr).getUnderlyinExpr();
119
// }
120
SqlExpression sqlSortExpr = sortExpr.accept(this);
121
122             retVal = _factory.createSortSpecification(sqlSortExpr,arg.getSortDirection());
123         }
124         else {
125             Expression sortExpr = arg.getSortExpression();
126             Expression underlyingExpr = null;
127             if (sortExpr instanceof AttributeExpression) {
128                 underlyingExpr = ((AttributeExpression)sortExpr).getUnderlyinExpr();
129                 if (underlyingExpr instanceof AttributeExpression) {
130                     sortExpr = underlyingExpr;
131                 }
132             }
133             SqlExpression sqlSortExpr = sortExpr.accept(this);
134
135             retVal = _factory.createSortSpecification(sqlSortExpr,arg.getSortDirection());
136         }
137         //Trace.exit(this,"visit(SortSpecification arg)");
138
return retVal;
139     }
140
141
142     protected SqlSelect selectAddFromClause (SqlSelect select, BinaryAlgebra operand)
143     {
144         //Trace.enter(this,"selectAddFromClause (SqlSelect select, BinAlgebra operand)",operand.toString());
145

146         SqlExpression sRelation = operand.accept(this);
147         SqlRenameRelation sRR = _factory.createRenameRelation( sRelation,"BinaryAlgebra");
148
149         select.addFromClause(sRR);
150
151         //Trace.exit(this,"selectAddFromClause (SqlSelect select, BinAlgebra operand)",operand.toString());
152
return select;
153     }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 }
169
Popular Tags