KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > rdb > lib > AggregateRdbQueryNode


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre, S. Chassande-Barrioz
22  */

23
24 package org.objectweb.medor.query.rdb.lib;
25
26 import org.objectweb.medor.api.MedorException;
27 import org.objectweb.medor.api.QueryLeafException;
28 import org.objectweb.medor.datasource.api.DataStore;
29 import org.objectweb.medor.expression.api.Expression;
30 import org.objectweb.medor.expression.api.ExpressionException;
31 import org.objectweb.medor.expression.api.ParameterOperand;
32 import org.objectweb.medor.query.api.NestQueryNode;
33 import org.objectweb.medor.query.api.NestedField;
34 import org.objectweb.medor.query.api.PropagatedField;
35 import org.objectweb.medor.query.api.QueryTreeField;
36 import org.objectweb.medor.query.lib.Nest;
37 import org.objectweb.medor.query.rdb.api.RdbExpField;
38 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
39 import org.objectweb.medor.query.rdb.api.RdbQueryLeaf;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * This class represents a QueryLeaf which contains aggregate functions.
46  * <p> It is built on top of a RbdQueryLeaf.
47  */

48 public class AggregateRdbQueryNode extends Nest
49         implements RdbQueryLeaf, NestQueryNode {
50
51     protected DataStore ds;
52     protected RdbExpField groupByField;
53     protected RdbExpQueryLeaf subRdbQL;
54     protected String JavaDoc query = null;
55     private boolean isSubquery = false;
56
57     public AggregateRdbQueryNode() {
58     }
59
60     /**
61      * Constructs a RDB QueryNode for an aggregation.
62      * <p>The SQL request (string) will be calculated.
63      * @param ds is the DataStore associated to the QueryLeaf to be created
64      * @throws QueryLeafException if the QualifiedTables have all the same
65      * names, and in this case also the same aliases.
66      */

67     public AggregateRdbQueryNode(QueryTreeField[] groupedFields,
68                 String JavaDoc groupedFieldName,
69                 QueryTreeField[] groupByFields,
70                 BasicRdbExpQueryLeaf subQL,
71                 DataStore ds,
72                 String JavaDoc nodeName)
73             throws MedorException {
74         super(groupedFields, groupedFieldName, groupByFields, nodeName);
75         this.ds = ds;
76         this.subRdbQL = subQL;
77         //removing the nested field
78
removeField(groupedFieldName);
79         //adds the groupby fields
80
for (int i = 0; i < groupByFields.length; i++) {
81             subQL.addGroupBy((RdbExpField) groupByFields[i]);
82         }
83     }
84
85     public Object JavaDoc clone(Object JavaDoc clone,
86                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
87         clone = super.clone(clone, obj2clone);
88         AggregateRdbQueryNode a = (AggregateRdbQueryNode) clone;
89         a.ds = ds;
90         a.groupByField = (RdbExpField) getClone(groupByField, obj2clone);
91         a.subRdbQL = (RdbExpQueryLeaf) getClone(subRdbQL, obj2clone);
92         a.query = query;
93         a.isSubquery = isSubquery;
94         return clone;
95     }
96
97     public String JavaDoc getSqlRequest(ParameterOperand[] pos,
98                                 ArrayList JavaDoc al,
99                                 boolean rangeStartAt,
100                                 boolean rangeSize)
101             throws MedorException, ExpressionException {
102         if (al == null) {
103             return getSqlRequest(pos, rangeStartAt, rangeSize);
104         } else {
105             throw new MedorException("Request with selectList not supported");
106         }
107     }
108
109     /**
110      * Builds and returns the SQL query as a String corresponding to the
111      * current RdbExpQueryLeaf.
112      * <p>
113      * The SQL query is computed using the aggregate fields and relies on
114      * the child RdbQueryLeaf.
115      * @return the SQL query as a String.
116      */

117     public String JavaDoc getSqlRequest(ParameterOperand[] pos,
118             boolean rangeStartAt,
119             boolean rangeSize)
120             throws MedorException, ExpressionException {
121         ArrayList JavaDoc selectFields = new ArrayList JavaDoc();
122         for (int i = 0; i < fields.size(); i++) {
123             if (!(fields.get(i) instanceof NestedField)) {
124                 if (fields.get(i) instanceof PropagatedField) {
125                     selectFields.add(((PropagatedField)fields.get(i)).getPreviousFields()[0]);
126                 } else {
127                     selectFields.add(fields.get(i));
128                 }
129             }
130         }
131         if (query != null) {
132             return query;
133         }
134         if (subRdbQL.isSubquery()) {
135             //Constructs a subquery only if there is a count on multiple fields.
136
//Note: in this case, no other aggregates are supported.
137
boolean subRdbQLDistinct = subRdbQL.getDistinct();
138             subRdbQL.setDistinct(this.getDistinct());
139             query = subRdbQL.getSelectList("SELECT ", selectFields, false);
140             subRdbQL.setDistinct(subRdbQLDistinct);
141             subRdbQL.setNoGroupBy(true);
142
143             query += " FROM (";
144             query += subRdbQL.getSqlRequest(pos, null, rangeStartAt, rangeSize);
145             query += ")";
146             if (subRdbQL.getRdbAdapter() != null) {
147                 query += subRdbQL.getRdbAdapter().getSubqueryAlias();
148             }
149             query += subRdbQL.getGroupBy();
150         } else {
151             query = subRdbQL.getSqlRequest(pos,
152                     selectFields,
153                     rangeStartAt,
154                     rangeSize);
155         }
156         return query;
157     }
158
159     public String JavaDoc getSelectList(String JavaDoc selectList, ArrayList JavaDoc selectFields,
160                                 boolean qualified)
161         throws MedorException {
162         throw new QueryLeafException("Unsupported method getSelectList for AggregateRdbQueryNode");
163     }
164
165     public DataStore getDataStore() {
166         return ds;
167     }
168
169     public void setIsSubquery(boolean subquery) {
170         isSubquery = subquery;
171     }
172
173     public boolean isSubquery() {
174         return isSubquery;
175     }
176
177     /**
178      * Redefines getQueryFilter using the filter of the subquery.
179      * @return the filter of the subquery.
180      */

181     public Expression getQueryFilter() {
182         if (subRdbQL != null) {
183             return subRdbQL.getQueryFilter();
184         } else {
185             return null;
186         }
187     }
188 }
Popular Tags