1 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 ; 48 import java.util.Iterator ; 49 50 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 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 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 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 sb = new StringBuffer (); 144 String 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 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 175 public static ArrayList getChildren(QueryNode qn) { 176 ArrayList children = new ArrayList (); 177 Field[] fs = qn.getTupleStructure().getFields(); 178 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 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 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 213 private static void addChildrenFromExpression(ArrayList children, Expression exp) { 214 if (exp instanceof FieldOperand) { 215 Field f = ((FieldOperand) exp).getField(); 216 QueryTree qt = ((QueryTreeField) f).getQueryTree(); 217 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 |