1 25 package org.objectweb.jonas_ejb.lib; 26 27 import java.util.Map ; 28 import java.util.Stack ; 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 63 public class EjbqlSelectVisitor extends EjbqlAbstractVisitor { 64 65 Map fields; 66 AggregateOperator aggregateOp = null; 67 QueryTree qt; 68 69 74 public EjbqlSelectVisitor(ASTEJBQL ejbql, Map _fields, SelectProject sp) throws Exception { 75 this.qt = sp; 76 fields = _fields; 77 visit(ejbql); 78 } 79 80 84 public QueryTree getQueryTree() { 85 return qt; 86 } 87 88 92 public Object visit(ASTSelectClause node, Object 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 ) ((Stack ) data).pop()); 99 QueryTreeField qtf2 = sp.addPropagatedField(qtf.getName(), qtf.getType(), new QueryTreeField[] {qtf}); 100 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 aggregateOp.setExpression(0, new BasicFieldOperand(qtf2)); 124 n.addCalculatedField("calculed_field", type, aggregateOp); 125 qt = n; 126 } 127 } catch (Exception e) { 128 throw new VisitorException(e); 129 } 130 return null; 131 } 132 133 136 public Object visit(ASTSelectExpression node, Object data) { 137 return visit((SimpleNode) node, data); 138 } 139 140 143 public Object visit(ASTAggregateSelectExpression node, Object data) { 144 visit((SimpleNode) node, data); 145 Stack s = (Stack ) data; 146 int operator = ((Integer ) node.ops.get(0)).intValue(); 147 boolean distinct = node.distinct; 148 Field field = (Field) fields.get((String ) 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 ("AggregateOperator '" + operator + "' unknown"); 168 } 169 return null; 170 } 171 172 175 public Object visit(ASTCmpPathExpression node, Object data) { 176 return visit((SimpleNode) node, data); 177 } 178 179 182 public Object visit(ASTSingleValuedCmrPathExpression node, Object data) { 183 return visit((SimpleNode) node, data); 184 } 185 186 189 public Object visit(ASTSingleValuedPathExpression node, Object data) { 190 return visit((SimpleNode) node, data); 191 } 192 193 196 public Object visit(ASTIdentificationVariable node, Object data) { 197 ((Stack ) data).push((String ) node.value + "." + Field.PNAMENAME); 198 return null; 199 } 200 201 204 public Object visit(ASTPath node, Object data) { 205 ((Stack ) data).push(node.value); 206 return null; 207 } 208 } | Popular Tags |