KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > JdbcActionBuilder


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
20 package org.apache.cayenne.dba;
21
22 import org.apache.cayenne.access.jdbc.BatchAction;
23 import org.apache.cayenne.access.jdbc.EJBQLAction;
24 import org.apache.cayenne.access.jdbc.ProcedureAction;
25 import org.apache.cayenne.access.jdbc.SQLTemplateAction;
26 import org.apache.cayenne.access.jdbc.SelectAction;
27 import org.apache.cayenne.access.jdbc.UpdateAction;
28 import org.apache.cayenne.map.EntityResolver;
29 import org.apache.cayenne.query.BatchQuery;
30 import org.apache.cayenne.query.EJBQLQuery;
31 import org.apache.cayenne.query.ProcedureQuery;
32 import org.apache.cayenne.query.Query;
33 import org.apache.cayenne.query.SQLAction;
34 import org.apache.cayenne.query.SQLActionVisitor;
35 import org.apache.cayenne.query.SQLTemplate;
36 import org.apache.cayenne.query.SelectQuery;
37
38 /**
39  * A factory of default SQLActions. Adapters usually subclass JdbcActionBuilder to provide
40  * custom actions for various query types.
41  *
42  * @since 1.2
43  * @author Andrus Adamchik
44  */

45 public class JdbcActionBuilder implements SQLActionVisitor {
46
47     protected DbAdapter adapter;
48     protected EntityResolver entityResolver;
49
50     public JdbcActionBuilder(DbAdapter adapter, EntityResolver resolver) {
51         this.adapter = adapter;
52         this.entityResolver = resolver;
53     }
54
55     public SQLAction batchAction(BatchQuery query) {
56         // check run strategy...
57

58         // optimistic locking is not supported in batches due to JDBC driver limitations
59
boolean useOptimisticLock = query.isUsingOptimisticLocking();
60
61         boolean runningAsBatch = !useOptimisticLock && adapter.supportsBatchUpdates();
62         BatchAction action = new BatchAction(query, adapter, entityResolver);
63         action.setBatch(runningAsBatch);
64         return action;
65     }
66
67     public SQLAction procedureAction(ProcedureQuery query) {
68         return new ProcedureAction(query, adapter, entityResolver);
69     }
70
71     public SQLAction objectSelectAction(SelectQuery query) {
72         return new SelectAction(query, adapter, entityResolver);
73     }
74
75     public SQLAction sqlAction(SQLTemplate query) {
76         return new SQLTemplateAction(query, adapter);
77     }
78
79     public SQLAction updateAction(Query query) {
80         if (query instanceof SQLTemplate) {
81             return new SQLTemplateAction((SQLTemplate) query, adapter);
82         }
83
84         return new UpdateAction(query, adapter, entityResolver);
85     }
86
87     /**
88      * @since 3.0
89      */

90     public SQLAction ejbqlAction(EJBQLQuery query) {
91         return new EJBQLAction(query, this, adapter, entityResolver);
92     }
93
94     /**
95      * Returns DbAdapter used associated with this action builder.
96      */

97     public DbAdapter getAdapter() {
98         return adapter;
99     }
100
101     /**
102      * Returns EntityResolver that can be used to gain access to the mapping objects.
103      */

104     public EntityResolver getEntityResolver() {
105         return entityResolver;
106     }
107 }
108
Popular Tags