KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > lib > EjbqlSelectVisitor


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EjbqlSelectVisitor.java,v 1.14 2004/10/22 11:50:06 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_ejb.lib;
26
27 import java.util.Map JavaDoc;
28 import java.util.Stack JavaDoc;
29
30 import org.objectweb.jonas_ejb.deployment.ejbql.ASTAggregateSelectExpression;
31 import org.objectweb.jonas_ejb.deployment.ejbql.ASTCmpPathExpression;
32 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL;
33 import org.objectweb.jonas_ejb.deployment.ejbql.ASTIdentificationVariable;
34 import org.objectweb.jonas_ejb.deployment.ejbql.ASTPath;
35 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSelectClause;
36 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSelectExpression;
37 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSingleValuedCmrPathExpression;
38 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSingleValuedPathExpression;
39 import org.objectweb.jonas_ejb.deployment.ejbql.EJBQLConstants;
40 import org.objectweb.jonas_ejb.deployment.ejbql.SimpleNode;
41 import org.objectweb.jorm.type.api.PType;
42 import org.objectweb.jorm.type.api.PTypeSpace;
43 import org.objectweb.medor.api.Field;
44 import org.objectweb.medor.filter.api.AggregateOperator;
45 import org.objectweb.medor.filter.lib.Avg;
46 import org.objectweb.medor.filter.lib.BasicFieldOperand;
47 import org.objectweb.medor.filter.lib.Count;
48 import org.objectweb.medor.filter.lib.Max;
49 import org.objectweb.medor.filter.lib.Min;
50 import org.objectweb.medor.filter.lib.Sum;
51 import org.objectweb.medor.query.api.QueryTree;
52 import org.objectweb.medor.query.api.QueryTreeField;
53 import org.objectweb.medor.query.lib.Nest;
54 import org.objectweb.medor.query.lib.SelectProject;
55
56 /**
57  * Implementation of a visitor that creates the selection projection for
58  * corresponding to a given WHERE clause. Created on Sep 6, 2002
59  * @author Christophe Ney [cney@batisseurs.com]: Initial developer
60  * @author Helene Joanin: Take into account the EJBQL version 2.1 syntax.
61  * @author Helene Joanin: Take into account the aggregate select expression.
62  */

63 public class EjbqlSelectVisitor extends EjbqlAbstractVisitor {
64
65     Map JavaDoc fields;
66     AggregateOperator aggregateOp = null;
67     QueryTree qt;
68
69     /**
70      * Constructor
71      * @param ejbql root of the lexical tree of the query
72      * @param _fields Map with (identifier,JormExtent) pairs
73      */

74     public EjbqlSelectVisitor(ASTEJBQL ejbql, Map JavaDoc _fields, SelectProject sp) throws Exception JavaDoc {
75         this.qt = sp;
76         fields = _fields;
77         visit(ejbql);
78     }
79
80     /**
81      * get the query tree that was built from visiting the lexical tree. This is
82      * a nest if there is an aggregate function or a select projet otherwise.
83      */

84     public QueryTree getQueryTree() {
85         return qt;
86     }
87
88     /**
89      * Visit child node. SELECT [ DISTINCT ] {select_expression | OBJECT
90      * (identification_variable)}
91      */

92     public Object JavaDoc visit(ASTSelectClause node, Object JavaDoc data) {
93         visit((SimpleNode) node, data);
94         try {
95             boolean distinct = node.distinct;
96             qt.setDistinct(distinct);
97             SelectProject sp = (SelectProject) qt;
98             QueryTreeField qtf = (QueryTreeField) fields.get((String JavaDoc) ((Stack JavaDoc) data).pop());
99             QueryTreeField qtf2 = sp.addPropagatedField(qtf.getName(), qtf.getType(), new QueryTreeField[] {qtf});
100             //TraceEjb.query.log(BasicLevel.DEBUG, "The SelectProjectQueryTree: ");
101
//QueryTreePrinter.printQueryTree(sp, TraceEjb.query, BasicLevel.DEBUG);
102
if (aggregateOp != null) {
103                 Nest n = new Nest(new QueryTreeField[] {qtf2}, "grouped_fields", new QueryTreeField[0], "aggregate_node");
104                 PType type = null;
105                 if (aggregateOp instanceof Sum) {
106                     if (qtf.getType().equals(PTypeSpace.FLOAT) || qtf.getType().equals(PTypeSpace.DOUBLE)) {
107                         type = PTypeSpace.DOUBLE;
108                     } else if (qtf.getType().equals(PTypeSpace.BIGINTEGER)) {
109                         type = PTypeSpace.BIGINTEGER;
110                     } else if (qtf.getType().equals(PTypeSpace.BIGDECIMAL)) {
111                         type = PTypeSpace.BIGDECIMAL;
112                     } else {
113                         type = PTypeSpace.LONG;
114                     }
115                 } else if ((aggregateOp instanceof Max) || (aggregateOp instanceof Min)) {
116                     type = qtf.getType();
117                 } else if (aggregateOp instanceof Avg) {
118                     type = PTypeSpace.DOUBLE;
119                 } else if (aggregateOp instanceof Count) {
120                     type = PTypeSpace.LONG;
121                 }
122                 //replace the field in the aggregateOp by the propagated field
123
aggregateOp.setExpression(0, new BasicFieldOperand(qtf2));
124                 n.addCalculatedField("calculed_field", type, aggregateOp);
125                 qt = n;
126             }
127         } catch (Exception JavaDoc e) {
128             throw new VisitorException(e);
129         }
130         return null;
131     }
132
133     /**
134      * Visit child node
135      */

136     public Object JavaDoc visit(ASTSelectExpression node, Object JavaDoc data) {
137         return visit((SimpleNode) node, data);
138     }
139
140     /**
141      * Visit child node
142      */

143     public Object JavaDoc visit(ASTAggregateSelectExpression node, Object JavaDoc data) {
144         visit((SimpleNode) node, data);
145         Stack JavaDoc s = (Stack JavaDoc) data;
146         int operator = ((Integer JavaDoc) node.ops.get(0)).intValue();
147         boolean distinct = node.distinct;
148         Field field = (Field) fields.get((String JavaDoc) s.peek());
149         BasicFieldOperand fieldOp = new BasicFieldOperand(field);
150         switch (operator) {
151             case EJBQLConstants.AVG:
152                 aggregateOp = new Avg(fieldOp, distinct);
153                 break;
154             case EJBQLConstants.COUNT:
155                 aggregateOp = new Count(fieldOp, distinct);
156                 break;
157             case EJBQLConstants.MAX:
158                 aggregateOp = new Max(fieldOp, distinct);
159                 break;
160             case EJBQLConstants.MIN:
161                 aggregateOp = new Min(fieldOp, distinct);
162                 break;
163             case EJBQLConstants.SUM:
164                 aggregateOp = new Sum(fieldOp, distinct);
165                 break;
166             default:
167                 throw new Error JavaDoc("AggregateOperator '" + operator + "' unknown");
168         }
169         return null;
170     }
171
172     /**
173      * Visit child node
174      */

175     public Object JavaDoc visit(ASTCmpPathExpression node, Object JavaDoc data) {
176         return visit((SimpleNode) node, data);
177     }
178
179     /**
180      * Visit child node
181      */

182     public Object JavaDoc visit(ASTSingleValuedCmrPathExpression node, Object JavaDoc data) {
183         return visit((SimpleNode) node, data);
184     }
185
186     /**
187      * Visit child node
188      */

189     public Object JavaDoc visit(ASTSingleValuedPathExpression node, Object JavaDoc data) {
190         return visit((SimpleNode) node, data);
191     }
192
193     /**
194      * Push the Node to the stack
195      */

196     public Object JavaDoc visit(ASTIdentificationVariable node, Object JavaDoc data) {
197         ((Stack JavaDoc) data).push((String JavaDoc) node.value + "." + Field.PNAMENAME);
198         return null;
199     }
200
201     /**
202      * Push the Node to the stack
203      */

204     public Object JavaDoc visit(ASTPath node, Object JavaDoc data) {
205         ((Stack JavaDoc) data).push(node.value);
206         return null;
207     }
208 }
Popular Tags