1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import java.util.*; 26 27 import oracle.toplink.essentials.exceptions.EJBQLException; 29 import oracle.toplink.essentials.queryframework.ReportQuery; 30 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery; 31 32 39 public class GroupByNode extends MajorNode { 40 41 List groupByItems = null; 42 43 46 public GroupByNode() { 47 super(); 48 } 49 50 54 public void validate(ParseTreeContext context, SelectNode selectNode) { 55 for (Iterator i = groupByItems.iterator(); i.hasNext(); ) { 56 Node item = (Node)i.next(); 57 item.validate(context); 58 } 59 60 List selectExprs = selectNode.getSelectExpressions(); 61 for (Iterator i = selectExprs.iterator(); i.hasNext(); ) { 63 Node selectExpr = (Node)i.next(); 64 if (!isValidSelectExpr(selectExpr)) { 65 throw EJBQLException.invalidSelectForGroupByQuery( 66 selectExpr.getAsString(), getAsString()); 67 } 68 } 69 } 70 71 75 private void addGroupByItem(Object theNode) { 76 getGroupByItems().add(theNode); 77 } 78 79 83 public void addGroupingToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) { 84 if (theQuery.isReportQuery()) { 85 Iterator iter = getGroupByItems().iterator(); 86 while (iter.hasNext()) { 87 Node nextNode = (Node)iter.next(); 88 ((ReportQuery)theQuery).addGrouping(nextNode.generateExpression(context)); 89 } 90 } 91 } 92 93 97 public boolean isValidHavingExpr(Node expr) { 98 if (expr.isDotNode() || expr.isVariableNode()) { 99 return isGroupbyItem(expr); 100 } else { 101 Node left = expr.getLeft(); 103 Node right = expr.getRight(); 104 return ((left == null) || isValidHavingExpr(left)) && 105 ((right == null) || isValidHavingExpr(right)); 106 } 107 } 108 109 113 private boolean isValidSelectExpr(Node expr) { 114 if (expr.isAggregateNode()) { 115 return true; 116 } else if (expr.isConstructorNode()) { 117 List args = ((ConstructorNode)expr).getConstructorItems(); 118 for (Iterator i = args.iterator(); i.hasNext(); ) { 119 Node arg = (Node)i.next(); 120 if (!isValidSelectExpr(arg)) { 121 return false; 122 } 123 } 124 return true; 125 } 126 return isGroupbyItem(expr); 127 } 128 129 133 private boolean isGroupbyItem(Node expr) { 134 if (expr.isDotNode() || expr.isVariableNode()) { 135 String exprRepr = expr.getAsString(); 136 for (Iterator i = groupByItems.iterator(); i.hasNext();) { 137 Node item = (Node)i.next(); 138 String itemRepr = item.getAsString(); 139 if (exprRepr.equals(itemRepr)) { 140 return true; 141 } 142 } 143 } 144 return false; 145 } 146 147 151 public List getGroupByItems() { 152 if (groupByItems == null) { 153 setGroupByItems(new Vector()); 154 } 155 return groupByItems; 156 } 157 158 162 public void setGroupByItems(List newItems) { 163 groupByItems = newItems; 164 } 165 166 170 public String getAsString() { 171 StringBuffer repr = new StringBuffer (); 172 for (Iterator i = groupByItems.iterator(); i.hasNext(); ) { 173 Node expr = (Node)i.next(); 174 if (repr.length() > 0) { 175 repr.append(", "); 176 } 177 repr.append(expr.getAsString()); 178 } 179 return "GROUP BY " + repr.toString(); 180 } 181 182 } 183 | Popular Tags |