1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import java.math.BigDecimal ; 25 import java.math.BigInteger ; 26 27 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery; 28 import oracle.toplink.essentials.queryframework.ReportQuery; 29 import oracle.toplink.essentials.expressions.Expression; 30 31 38 public class SumNode extends AggregateNode { 39 40 44 public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) { 45 if (theQuery.isReportQuery()) { 46 ReportQuery reportQuery = (ReportQuery)theQuery; 47 reportQuery.addSum(resolveAttribute(), 48 getArgumentExpression(context), 49 calculateReturnType(context)); 50 51 } 52 } 53 54 58 public void validate(ParseTreeContext context) { 59 if (left != null) { 60 left.validate(context); 61 TypeHelper typeHelper = context.getTypeHelper(); 62 setType(calculateReturnType(left.getType(), typeHelper)); 63 } 64 } 65 66 70 public Expression generateExpression(GenerationContext context) { 71 return getArgumentExpression(context).sum(); 72 } 73 74 78 protected Class calculateReturnType(GenerationContext context) { 79 Class returnType = null; 80 if (getLeft().isDotNode()){ 81 DotNode arg = (DotNode)getLeft(); 82 Class fieldType = arg.getTypeOfDirectToField(context); 83 TypeHelper helper = context.getParseTreeContext().getTypeHelper(); 84 returnType = (Class )calculateReturnType(fieldType, helper); 85 } 86 return returnType; 87 } 88 89 93 protected Object calculateReturnType(Object argType, TypeHelper helper) { 94 Object returnType = null; 95 if (helper.isIntegralType(argType)) { 96 returnType = helper.getLongClassType(); 97 } else if (helper.isFloatingPointType(argType)) { 98 returnType = helper.getDoubleClassType(); 99 } else if (helper.isBigIntegerType(argType)) { 100 returnType = helper.getBigIntegerType(); 101 } else if (helper.isBigDecimalType(argType)) { 102 returnType = helper.getBigDecimalType(); 103 } 104 return returnType; 105 } 106 107 111 public String getAsString() { 112 return "SUM(" + left.getAsString() + ")"; 113 } 114 } 115 | Popular Tags |