KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
22
23 import org.apache.cayenne.ejbql.EJBQLBaseVisitor;
24 import org.apache.cayenne.ejbql.EJBQLException;
25 import org.apache.cayenne.ejbql.EJBQLExpression;
26 import org.apache.cayenne.ejbql.parser.EJBQLFromItem;
27 import org.apache.cayenne.ejbql.parser.EJBQLJoin;
28 import org.apache.cayenne.map.DbJoin;
29 import org.apache.cayenne.map.DbRelationship;
30 import org.apache.cayenne.map.ObjRelationship;
31 import org.apache.cayenne.reflect.ClassDescriptor;
32
33 /**
34  * @since 3.0
35  * @author Andrus Adamchik
36  */

37 class EJBQLFromTranslator extends EJBQLBaseVisitor {
38
39     private EJBQLTranslationContext context;
40     private String JavaDoc lastId;
41
42     static String JavaDoc makeJoinTailMarker(String JavaDoc id) {
43         return "FROM_TAIL" + id;
44     }
45
46     EJBQLFromTranslator(EJBQLTranslationContext context) {
47         super(true);
48         this.context = context;
49     }
50
51     public boolean visitFrom(EJBQLExpression expression, int finishedChildIndex) {
52         if (finishedChildIndex + 1 == expression.getChildrenCount()) {
53             if (lastId != null) {
54                 context.markCurrentPosition(makeJoinTailMarker(lastId));
55             }
56         }
57
58         return true;
59     }
60
61     public boolean visitFromItem(EJBQLFromItem expression) {
62
63         String JavaDoc id = expression.getId();
64
65         if (lastId != null) {
66             context.append(',');
67             context.markCurrentPosition(makeJoinTailMarker(lastId));
68         }
69
70         this.lastId = id;
71         appendTable(id);
72         return false;
73     }
74
75     public boolean visitInnerFetchJoin(EJBQLJoin join) {
76         // TODO: andrus, 4/9/2007 - support for prefetching
77
return visitInnerJoin(join);
78     }
79
80     public boolean visitInnerJoin(EJBQLJoin join) {
81         appendJoin(join, "INNER JOIN");
82         return false;
83     }
84
85     public boolean visitOuterFetchJoin(EJBQLJoin join) {
86         // TODO: andrus, 4/9/2007 - support for prefetching
87
return visitOuterJoin(join);
88     }
89
90     public boolean visitOuterJoin(EJBQLJoin join) {
91         appendJoin(join, "LEFT OUTER JOIN");
92         return false;
93     }
94
95     private void appendJoin(EJBQLJoin join, String JavaDoc semantics) {
96
97         String JavaDoc rhsId = join.getRightHandSideId();
98
99         ObjRelationship joinRelationship = context
100                 .getCompiledExpression()
101                 .getIncomingRelationship(rhsId);
102         if (joinRelationship == null) {
103             throw new EJBQLException("No join configured for id " + rhsId);
104         }
105
106         // TODO: andrus, 4/8/2007 - support for flattened relationships
107
DbRelationship incomingDB = (DbRelationship) joinRelationship
108                 .getDbRelationships()
109                 .get(0);
110
111         String JavaDoc lhsId = join.getLeftHandSideId();
112         String JavaDoc sourceAlias = context.getTableAlias(lhsId, incomingDB
113                 .getSourceEntity()
114                 .getName());
115
116         context.append(" ").append(semantics);
117         String JavaDoc targetAlias = appendTable(rhsId);
118         context.append(" ON (");
119
120         Iterator JavaDoc it = incomingDB.getJoins().iterator();
121         if (it.hasNext()) {
122             DbJoin dbJoin = (DbJoin) it.next();
123             context
124                     .append(sourceAlias)
125                     .append('.')
126                     .append(dbJoin.getSourceName())
127                     .append(" = ")
128                     .append(targetAlias)
129                     .append('.')
130                     .append(dbJoin.getTargetName());
131         }
132
133         while (it.hasNext()) {
134             context.append(", ");
135             DbJoin dbJoin = (DbJoin) it.next();
136             context
137                     .append(sourceAlias)
138                     .append('.')
139                     .append(dbJoin.getSourceName())
140                     .append(" = ")
141                     .append(targetAlias)
142                     .append('.')
143                     .append(dbJoin.getTargetName());
144         }
145
146         context.append(")");
147     }
148
149     private String JavaDoc appendTable(String JavaDoc id) {
150         ClassDescriptor descriptor = context.getCompiledExpression().getEntityDescriptor(
151                 id);
152
153         String JavaDoc tableName = descriptor.getEntity().getDbEntity().getFullyQualifiedName();
154         String JavaDoc alias = context.getTableAlias(id, tableName);
155
156         // not using "AS" to separate table name and alias name - OpenBase doesn't support
157
// "AS", and the rest of the databases do not care
158
context.append(' ').append(tableName).append(' ').append(alias);
159         return alias;
160     }
161 }
162
Popular Tags