KickJava   Java API By Example, From Geeks To Geeks.

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


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.access.jdbc;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import org.apache.cayenne.CayenneRuntimeException;
27 import org.apache.cayenne.access.OperationObserver;
28 import org.apache.cayenne.access.QueryLogger;
29 import org.apache.cayenne.access.QueryTranslator;
30 import org.apache.cayenne.access.trans.DeleteTranslator;
31 import org.apache.cayenne.access.trans.UpdateTranslator;
32 import org.apache.cayenne.dba.DbAdapter;
33 import org.apache.cayenne.map.EntityResolver;
34 import org.apache.cayenne.query.DeleteQuery;
35 import org.apache.cayenne.query.Query;
36 import org.apache.cayenne.query.UpdateQuery;
37
38 /**
39  * @since 1.2
40  * @author Andrus Adamchik
41  */

42 public class UpdateAction extends BaseSQLAction {
43
44     protected Query query;
45
46     public UpdateAction(Query query, DbAdapter adapter, EntityResolver entityResolver) {
47         super(adapter, entityResolver);
48         this.query = query;
49     }
50
51     protected QueryTranslator createTranslator(Connection JavaDoc connection) {
52         QueryTranslator translator;
53
54         if (query instanceof UpdateQuery) {
55             translator = new UpdateTranslator();
56         }
57         else if (query instanceof DeleteQuery) {
58             translator = new DeleteTranslator();
59         }
60         else {
61             throw new CayenneRuntimeException("Can't make a translator for query "
62                     + query);
63         }
64
65         translator.setAdapter(getAdapter());
66         translator.setQuery(query);
67         translator.setEntityResolver(getEntityResolver());
68         translator.setConnection(connection);
69
70         return translator;
71     }
72
73     public void performAction(Connection JavaDoc connection, OperationObserver observer)
74             throws SQLException JavaDoc, Exception JavaDoc {
75
76         QueryTranslator translator = createTranslator(connection);
77
78         PreparedStatement JavaDoc statement = translator.createStatement();
79
80         try {
81             // execute update
82
int count = statement.executeUpdate();
83             QueryLogger.logUpdateCount(count);
84
85             // send results back to consumer
86
observer.nextCount(query, count);
87         }
88         finally {
89             statement.close();
90         }
91     }
92 }
93
Popular Tags