KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > lib > QueryTreePrinter


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2005 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, S. Chassande-Barrioz, A. Lefebvre
22  */

23
24 package org.objectweb.medor.query.lib;
25
26 import org.objectweb.medor.api.Field;
27 import org.objectweb.medor.expression.api.Expression;
28 import org.objectweb.medor.expression.api.Operator;
29 import org.objectweb.medor.filter.api.FieldOperand;
30 import org.objectweb.medor.filter.lib.ExpressionPrinter;
31 import org.objectweb.medor.query.api.CalculatedField;
32 import org.objectweb.medor.query.api.FilteredQueryTree;
33 import org.objectweb.medor.query.api.NestedField;
34 import org.objectweb.medor.query.api.OrderField;
35 import org.objectweb.medor.query.api.PropagatedField;
36 import org.objectweb.medor.query.api.QueryNode;
37 import org.objectweb.medor.query.api.QueryTree;
38 import org.objectweb.medor.query.api.QueryTreeField;
39 import org.objectweb.medor.query.rdb.api.QualifiedTable;
40 import org.objectweb.medor.query.rdb.api.RdbExpField;
41 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
42 import org.objectweb.medor.query.rdb.api.RdbField;
43 import org.objectweb.medor.query.rdb.api.RdbQueryLeaf;
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Logger;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Iterator JavaDoc;
49
50 /**
51  * @author S. Chassande-Barrioz
52  */

53 public class QueryTreePrinter {
54
55     static int id = 0;
56
57     public static void printQueryTree(QueryTree qt, Logger log, int level) {
58         if (log != null && log.isLoggable(level)) {
59             id = 0;
60             printQT("", qt, log, level);
61         }
62     }
63
64     public static void printQueryTree(QueryTree qt, Logger log) {
65         if (log != null && log.isLoggable(BasicLevel.DEBUG)) {
66             id = 0;
67             printQT("", qt, log, BasicLevel.DEBUG);
68         }
69     }
70
71     private static void printQT(String JavaDoc prefix, QueryTree qt, Logger log,
72                                 int level) {
73         id++;
74         int myId = id;
75         log.log(level, prefix + "*Node number " + myId);
76         log.log(level, prefix + " - Class " + qt);
77         log.log(level, prefix + " - Name " + qt.getName());
78         if (qt instanceof FilteredQueryTree) {
79             Expression e = ((FilteredQueryTree) qt).getQueryFilter();
80             if (e != null)
81                 log.log(level, prefix + " -Filter: " + ExpressionPrinter.e2str(e));
82         }
83         if (qt instanceof RdbQueryLeaf) {
84             try {
85                 log.log(level, prefix + " - sqlRequest: " +
86                         ((RdbQueryLeaf) qt).getSqlRequest(
87                                 null,
88                                 false,
89                                 false));
90             }
91             catch (Exception JavaDoc e) {
92                 log.log(level, prefix + " - sqlRequest: error");
93                 e.printStackTrace(System.out);
94             }
95             if (qt instanceof RdbExpQueryLeaf) {
96                 log.log(level, prefix + "(resetting SQL request)");
97                 ((RdbExpQueryLeaf) qt).resetSqlRequest();
98                 QualifiedTable[] qts = ((RdbExpQueryLeaf) qt).getQualifiedTables();
99                 for (int i = 0; i < qts.length; i++) {
100                     log.log(level, prefix + " - tableName[" + i + "]:"
101                         + qts[i].getTableName() + " as " + qts[i].getAliasName());
102                 }
103             }
104         }
105         log.log(level, prefix + " - Field names:");
106
107         Field[] fs = qt.getTupleStructure().getFields();
108         String JavaDoc msg = null;
109         for (int i = 0; i < fs.length; i++) {
110             msg = prefix + "\t-" + fs[i].getName();
111             if (fs[i] instanceof RdbField) {
112                 msg += "(" + ((RdbField) fs[i]).getColumnName();
113                 if (fs[i] instanceof RdbExpField)
114                     msg += " in "
115                         + ((RdbExpField) fs[i]).getTable().getTableName()
116                         + " as "
117                         + ((RdbExpField) fs[i]).getTable().getAliasName();
118                 msg += ")";
119             }
120             else if (fs[i] instanceof CalculatedField) {
121                 msg += " expression=" + ExpressionPrinter.e2str(((CalculatedField) fs[i]).getExpression());
122             }
123             else if (fs[i] instanceof PropagatedField) {
124                 Field[] previous = ((PropagatedField) fs[i]).getPreviousFields();
125                 msg += " previous=";
126                 for (int j = 0; j < previous.length; j++) {
127                     msg += previous[j].getName() + " ";
128                     msg += "of node " + ((QueryTreeField)previous[j]).getQueryTree();
129                 }
130             }
131             log.log(level, msg);
132         }
133         
134         if (qt instanceof Nest) {
135             log.log(level, prefix + " --Nest: group by fields");
136             Field[] gbFields = ((Nest)qt).getNestingFields();
137             for (int i = 0; i < gbFields.length; i++) {
138                 log.log(level, " ---groupBy:" + gbFields[i]);
139             }
140         }
141         OrderField[] ofs = qt.getOrderBy();
142         if (ofs != null) {
143             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144             String JavaDoc sep = "";
145             for(int i=0; i<ofs.length; i++) {
146                 sb.append(sep);
147                 sep = ", ";
148                 sb.append(ofs[i].getField().getName());
149                 sb.append((ofs[i].getDesc() ? " descending" : " ascending"));
150             }
151             log.log(level, prefix + " - Order By: " + sb.toString());
152         }
153         log.log(level, "");
154         if (qt instanceof QueryNode) {
155             for (Iterator JavaDoc it = getChildren((QueryNode) qt).iterator(); it.hasNext();) {
156                 log.log(level, prefix + " -Children of " + myId);
157                 QueryTree qtchild = (QueryTree) it.next();
158                 if (qt instanceof QueryNode) {
159                     log.log(level, prefix + "\t" +
160                         (((QueryNode) qt).isOuter(qtchild) ? "OUTER" : "INNER"));
161                 }
162                 log.log(level, prefix + "\t---------------");
163                 if (qtchild == null)
164                     log.log(level, prefix + "\tNull child !!!!!!!");
165                 else
166                     printQT(prefix + "\t", qtchild, log, level);
167             }
168         }
169     }
170
171     /**
172      * Updates the list of children by screening all Fields and the
173      * query filter.
174      */

175     public static ArrayList JavaDoc getChildren(QueryNode qn) {
176         ArrayList JavaDoc children = new ArrayList JavaDoc();
177         Field[] fs = qn.getTupleStructure().getFields();
178         //add children from Fields
179
for (int i = 0; i < fs.length; i++) {
180             if (fs[i] instanceof PropagatedField) {
181                 Field[] anc = ((PropagatedField) fs[i]).getPreviousFields();
182                 for (int j = 0; j < anc.length; j++) {
183                     QueryTree qt = ((QueryTreeField) anc[j]).getQueryTree();
184                     //System.log.log(level, "PropagatedField " + fs[i].getName() + ": anc["+j+"]=" + anc[j].getName() + " , qt=" + qt);
185
if (!children.contains(qt)) {
186                         children.add(qt);
187                     }
188                 }
189             }
190             else if (fs[i] instanceof NestedField) {
191                 Field[] grouped = ((NestedField) fs[i]).getFields();
192                 for (int j = 0; j < grouped.length; j++) {
193                     QueryTree qt = ((QueryTreeField) grouped[j]).getQueryTree();
194                     //System.log.log(level, "NestedField " + fs[i].getName() + ": grouped["+j+"]=" + grouped[j].getName() + " , qt=" + qt);
195
if (!children.contains(qt)) {
196                         children.add(qt);
197                     }
198                 }
199             }
200             else if (fs[i] instanceof CalculatedField) {
201                 addChildrenFromExpression(children, ((CalculatedField) fs[i]).getExpression());
202             }
203         }
204         addChildrenFromExpression(children, qn.getQueryFilter());
205         return children;
206     }
207
208     /**
209      * Parses an Expression and adds the QueryTrees of the FieldOperands to
210      * the children of the current QueryNode.
211      * @param exp is the Expression to be parsed.
212      */

213     private static void addChildrenFromExpression(ArrayList JavaDoc children, Expression exp) {
214         if (exp instanceof FieldOperand) {
215             Field f = ((FieldOperand) exp).getField();
216             QueryTree qt = ((QueryTreeField) f).getQueryTree();
217             //System.log.log(level, "fieldOperand " + f.getName() + " , qt=" + qt);
218
if (!children.contains(qt)) {
219                 children.add(qt);
220             }
221         }
222         else if (exp instanceof Operator) {
223             for (int i = 0; i < ((Operator) exp).getOperandNumber(); i++) {
224                 addChildrenFromExpression(children, ((Operator) exp).getExpression(i));
225             }
226         }
227     }
228
229 }
230
Popular Tags