KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import org.apache.cayenne.access.OperationObserver;
25 import org.apache.cayenne.dba.DbAdapter;
26 import org.apache.cayenne.ejbql.EJBQLBaseVisitor;
27 import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
28 import org.apache.cayenne.ejbql.EJBQLExpression;
29 import org.apache.cayenne.map.EntityResolver;
30 import org.apache.cayenne.query.EJBQLQuery;
31 import org.apache.cayenne.query.SQLActionVisitor;
32 import org.apache.cayenne.query.SQLTemplate;
33
34 /**
35  * Parses an EJBQL statement, converting it to SQL. Executes the resulting SQL.
36  *
37  * @since 3.0
38  * @author Andrus Adamchik
39  */

40 public class EJBQLAction extends BaseSQLAction {
41
42     protected SQLActionVisitor actionFactory;
43     protected EJBQLQuery query;
44
45     public EJBQLAction(EJBQLQuery query, SQLActionVisitor actionFactory,
46             DbAdapter adapter, EntityResolver entityResolver) {
47         super(adapter, entityResolver);
48
49         this.query = query;
50         this.actionFactory = actionFactory;
51     }
52
53     public void performAction(Connection JavaDoc connection, OperationObserver observer)
54             throws SQLException JavaDoc, Exception JavaDoc {
55         EJBQLCompiledExpression compiledExpression = query
56                 .getExpression(getEntityResolver());
57         final EJBQLTranslationContext context = new EJBQLTranslationContext(
58                 compiledExpression,
59                 query.getParameters());
60
61         compiledExpression.getExpression().visit(new EJBQLBaseVisitor(false) {
62
63             public boolean visitSelect(EJBQLExpression expression) {
64                 EJBQLSelectTranslator visitor = new EJBQLSelectTranslator(context);
65                 expression.visit(visitor);
66                 return false;
67             }
68
69             public boolean visitDelete(EJBQLExpression expression) {
70                 throw new UnsupportedOperationException JavaDoc("Not yet implemented");
71             }
72
73             public boolean visitUpdate(EJBQLExpression expression) {
74                 throw new UnsupportedOperationException JavaDoc("Not yet implemented");
75             }
76         });
77
78         SQLTemplate sqlQuery = context.getQuery();
79         actionFactory.sqlAction(sqlQuery).performAction(connection, observer);
80     }
81 }
82
Popular Tags