KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.cayenne.CayenneRuntimeException;
27 import org.apache.cayenne.dba.TypesMapping;
28 import org.apache.cayenne.ejbql.EJBQLBaseVisitor;
29 import org.apache.cayenne.ejbql.EJBQLExpression;
30 import org.apache.cayenne.map.DbAttribute;
31 import org.apache.cayenne.map.DbEntity;
32 import org.apache.cayenne.map.DbJoin;
33 import org.apache.cayenne.map.DbRelationship;
34 import org.apache.cayenne.map.ObjAttribute;
35 import org.apache.cayenne.map.ObjRelationship;
36 import org.apache.cayenne.reflect.ArcProperty;
37 import org.apache.cayenne.reflect.AttributeProperty;
38 import org.apache.cayenne.reflect.ClassDescriptor;
39 import org.apache.cayenne.reflect.PropertyVisitor;
40 import org.apache.cayenne.reflect.ToManyProperty;
41 import org.apache.cayenne.reflect.ToOneProperty;
42
43 /**
44  * @since 3.0
45  * @author Andrus Adamchik
46  */

47 class EJBQLIdentifierColumnsTranslator extends EJBQLBaseVisitor {
48
49     private EJBQLTranslationContext context;
50     private Set JavaDoc columns;
51     private boolean resultColumns;
52
53     EJBQLIdentifierColumnsTranslator(EJBQLTranslationContext context,
54             boolean resultColumns) {
55         this.context = context;
56         this.resultColumns = resultColumns;
57     }
58
59     public boolean visitIdentifier(EJBQLExpression expression) {
60
61         final String JavaDoc idVar = expression.getText();
62
63         // append all table columns
64
ClassDescriptor descriptor = context.getCompiledExpression().getEntityDescriptor(
65                 idVar);
66
67         PropertyVisitor visitor = new PropertyVisitor() {
68
69             public boolean visitAttribute(AttributeProperty property) {
70                 ObjAttribute oa = property.getAttribute();
71                 Iterator JavaDoc dbPathIterator = oa.getDbPathIterator();
72                 while (dbPathIterator.hasNext()) {
73                     Object JavaDoc pathPart = dbPathIterator.next();
74                     if (pathPart instanceof DbRelationship) {
75                         // DbRelationship rel = (DbRelationship) pathPart;
76
// dbRelationshipAdded(rel);
77
}
78                     else if (pathPart instanceof DbAttribute) {
79                         DbAttribute dbAttr = (DbAttribute) pathPart;
80                         if (dbAttr == null) {
81                             throw new CayenneRuntimeException(
82                                     "ObjAttribute has no DbAttribute: " + oa.getName());
83                         }
84
85                         appendColumn(idVar, dbAttr, oa.getType());
86                     }
87                 }
88                 return true;
89             }
90
91             public boolean visitToMany(ToManyProperty property) {
92                 visitRelationship(property);
93                 return true;
94             }
95
96             public boolean visitToOne(ToOneProperty property) {
97                 visitRelationship(property);
98                 return true;
99             }
100
101             private void visitRelationship(ArcProperty property) {
102                 ObjRelationship rel = property.getRelationship();
103                 DbRelationship dbRel = (DbRelationship) rel.getDbRelationships().get(0);
104
105                 List JavaDoc joins = dbRel.getJoins();
106                 int len = joins.size();
107                 for (int i = 0; i < len; i++) {
108                     DbJoin join = (DbJoin) joins.get(i);
109                     DbAttribute src = join.getSource();
110
111                     appendColumn(idVar, src);
112                 }
113             }
114         };
115
116         // EJBQL queries are polimorphic by definition - there is no distinction between
117
// inheritance/no-inheritance fetch
118
descriptor.visitAllProperties(visitor);
119
120         // append id columns ... (some may have been appended already via relationships)
121

122         DbEntity table = descriptor.getEntity().getDbEntity();
123         Iterator JavaDoc it = table.getPrimaryKey().iterator();
124         while (it.hasNext()) {
125             DbAttribute pk = (DbAttribute) it.next();
126             appendColumn(idVar, pk);
127         }
128
129         return false;
130     }
131
132     private void appendColumn(String JavaDoc identifier, DbAttribute column) {
133         appendColumn(identifier, column, null);
134     }
135
136     private void appendColumn(String JavaDoc identifier, DbAttribute column, String JavaDoc javaType) {
137         DbEntity table = (DbEntity) column.getEntity();
138         String JavaDoc alias = context.getTableAlias(identifier, table.getFullyQualifiedName());
139         String JavaDoc columnName = alias + "." + column.getName();
140
141         Set JavaDoc columns = getColumns();
142
143         if (columns.add(columnName)) {
144
145             context.append(columns.size() > 1 ? ", " : " ");
146
147             if (resultColumns) {
148                 context.append("#result('");
149             }
150
151             context.append(columnName);
152
153             if (resultColumns) {
154                 if (javaType == null) {
155                     javaType = TypesMapping.getJavaBySqlType(column.getType());
156                 }
157
158                 // TODO: andrus 6/27/2007 - the last parameter is an unofficial "jdbcType"
159
// pending CAY-813 implementation, switch to #column directive
160
context.append("' '").append(javaType).append("' '").append(
161                         column.getName()).append("' '").append(column.getName()).append(
162                         "' " + column.getType()).append(")");
163             }
164         }
165     }
166
167     private Set JavaDoc getColumns() {
168
169         if (columns == null) {
170             columns = new HashSet JavaDoc();
171         }
172
173         return columns;
174     }
175 }
176
Popular Tags