1 19 package org.apache.cayenne.access.jdbc; 20 21 import org.apache.cayenne.ejbql.EJBQLBaseVisitor; 22 import org.apache.cayenne.ejbql.EJBQLException; 23 import org.apache.cayenne.ejbql.EJBQLExpression; 24 import org.apache.cayenne.ejbql.EJBQLExpressionVisitor; 25 import org.apache.cayenne.ejbql.parser.EJBQLAggregateColumn; 26 import org.apache.cayenne.ejbql.parser.EJBQLPath; 27 import org.apache.cayenne.map.DbEntity; 28 import org.apache.cayenne.map.ObjAttribute; 29 30 34 class EJBQLAggregateColumnTranslator extends EJBQLBaseVisitor { 35 36 private EJBQLTranslationContext context; 37 private String attributeType; 38 39 EJBQLAggregateColumnTranslator(EJBQLTranslationContext context) { 40 this.context = context; 41 } 42 43 public boolean visitCount(EJBQLAggregateColumn expression) { 44 visitAggregateColumn(expression, new CountColumnVisitor()); 45 return false; 46 } 47 48 public boolean visitAverage(EJBQLAggregateColumn expression) { 49 visitAggregateColumn(expression, new FieldPathTranslator()); 50 return false; 51 } 52 53 public boolean visitMax(EJBQLAggregateColumn expression) { 54 visitAggregateColumn(expression, new FieldPathTranslator()); 55 return false; 56 } 57 58 public boolean visitMin(EJBQLAggregateColumn expression) { 59 visitAggregateColumn(expression, new FieldPathTranslator()); 60 return false; 61 } 62 63 public boolean visitSum(EJBQLAggregateColumn expression) { 64 visitAggregateColumn(expression, new FieldPathTranslator()); 65 return false; 66 } 67 68 private void visitAggregateColumn( 69 EJBQLAggregateColumn column, 70 EJBQLExpressionVisitor pathVisitor) { 71 72 context.append(" #result('").append(column.getFunction()).append('('); 73 74 column.visit(pathVisitor); 76 77 context 78 .append(")' '") 79 .append(column.getJavaType(attributeType)) 80 .append("' '") 81 .append(context.nextColumnAlias()) 82 .append("')"); 83 } 84 85 class FieldPathTranslator extends EJBQLPathTranslator { 86 87 FieldPathTranslator() { 88 super(EJBQLAggregateColumnTranslator.this.context); 89 } 90 91 public boolean visitDistinct(EJBQLExpression expression) { 92 context.append("DISTINCT "); 93 return true; 94 } 95 96 protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) { 97 throw new EJBQLException("Can't use multi-column paths in column clause"); 98 } 99 100 protected void processTerminatingAttribute(ObjAttribute attribute) { 101 102 EJBQLAggregateColumnTranslator.this.attributeType = attribute.getType(); 103 104 DbEntity table = currentEntity.getDbEntity(); 105 String alias = this.lastAlias != null ? lastAlias : context.getTableAlias( 106 idPath, 107 table.getFullyQualifiedName()); 108 context.append(alias).append('.').append(attribute.getDbAttributeName()); 109 } 110 } 111 112 class CountColumnVisitor extends EJBQLBaseVisitor { 113 114 public boolean visitDistinct(EJBQLExpression expression) { 115 context.append("DISTINCT "); 116 return true; 117 } 118 119 public boolean visitIdentifier(EJBQLExpression expression) { 120 context.append('*'); 121 return false; 122 } 123 124 public boolean visitPath(EJBQLPath expression, int finishedChildIndex) { 125 expression.visit(new FieldPathTranslator()); 126 return false; 127 } 128 } 129 } 130 | Popular Tags |