KickJava   Java API By Example, From Geeks To Geeks.

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


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.parser.EJBQLPath;
25 import org.apache.cayenne.map.DbAttribute;
26 import org.apache.cayenne.map.DbEntity;
27 import org.apache.cayenne.map.ObjAttribute;
28
29 /**
30  * Translator of the EJBQL select clause.
31  *
32  * @author Andrus Adamchik
33  * @since 3.0
34  */

35 class EJBQLSelectColumnsTranslator extends EJBQLBaseVisitor {
36
37     private EJBQLTranslationContext context;
38     private int expressionsCount;
39
40     EJBQLSelectColumnsTranslator(EJBQLTranslationContext context) {
41         this.context = context;
42     }
43
44     public boolean visitSelectExpression(EJBQLExpression expression) {
45         if (expressionsCount++ > 0) {
46             context.append(",");
47         }
48
49         return true;
50     }
51
52     public boolean visitAggregate(EJBQLExpression expression) {
53         expression.visit(new EJBQLAggregateColumnTranslator(context));
54         return false;
55     }
56
57     public boolean visitPath(EJBQLPath expression, int finishedChildIndex) {
58
59         EJBQLPathTranslator pathTranslator = new EJBQLPathTranslator(context) {
60
61             protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
62                 throw new EJBQLException("Can't use multi-column paths in column clause");
63             }
64
65             protected void processTerminatingAttribute(ObjAttribute attribute) {
66                 DbEntity table = currentEntity.getDbEntity();
67                 String JavaDoc alias = this.lastAlias != null ? lastAlias : context
68                         .getTableAlias(idPath, table.getFullyQualifiedName());
69
70                 DbAttribute dbAttribute = (DbAttribute) attribute.getDbAttribute();
71
72                 // TODO: andrus 6/27/2007 - the last parameter is an unofficial "jdbcType"
73
// pending CAY-813 implementation, switch to #column directive
74
String JavaDoc columnAlias = context.nextColumnAlias();
75                 context
76                         .append(" #result('")
77                         .append(alias)
78                         .append('.')
79                         .append(dbAttribute.getName())
80                         .append("' '")
81                         .append(attribute.getType())
82                         .append("' '")
83                         .append(columnAlias)
84                         .append("' '")
85                         .append(columnAlias)
86                         .append("' " + dbAttribute.getType())
87                         .append(")");
88             }
89         };
90
91         expression.visit(pathTranslator);
92         return false;
93     }
94
95     public boolean visitIdentifier(EJBQLExpression expression) {
96         expression.visit(new EJBQLIdentifierColumnsTranslator(context, true));
97         return false;
98     }
99 }
100
Popular Tags