KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > EJBQLAggregateColumnTranslator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

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 /**
31  * @since 3.0
32  * @author Andrus Adamchik
33  */

34 class EJBQLAggregateColumnTranslator extends EJBQLBaseVisitor {
35
36     private EJBQLTranslationContext context;
37     private String JavaDoc 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         // path visitor must set attributeType ivar
75
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 JavaDoc 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