KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > trans > UpdateTranslator


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.trans;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.map.DbAttribute;
27 import org.apache.cayenne.map.DbEntity;
28 import org.apache.cayenne.map.DbRelationship;
29 import org.apache.cayenne.query.UpdateQuery;
30
31 /** Class implements default translation mechanism of
32   * org.apache.cayenne.query.UpdateQuery
33   * objects to SQL UPDATE statements.
34   *
35   * @author Andrus Adamchik
36   */

37 public class UpdateTranslator extends QueryAssembler {
38
39     public String JavaDoc aliasForTable(DbEntity dbEnt) {
40         throw new RuntimeException JavaDoc("aliases not supported");
41     }
42
43     public void dbRelationshipAdded(DbRelationship dbRel) {
44         throw new RuntimeException JavaDoc("db relationships not supported");
45     }
46
47     /** Method that converts an update query into SQL string */
48     public String JavaDoc createSqlString() throws Exception JavaDoc {
49         StringBuffer JavaDoc queryBuf = new StringBuffer JavaDoc();
50         queryBuf.append("UPDATE ");
51
52         // 1. append table name
53
DbEntity dbEnt = getRootEntity().getDbEntity();
54         queryBuf.append(dbEnt.getFullyQualifiedName());
55
56         // 2. build "set ..." clause
57
buildSetClause(queryBuf, (UpdateQuery) query);
58
59         // 3. build qualifier
60
String JavaDoc qualifierStr =
61             adapter.getQualifierTranslator(this).doTranslation();
62         if (qualifierStr != null)
63             queryBuf.append(" WHERE ").append(qualifierStr);
64
65         return queryBuf.toString();
66     }
67
68     /** Translate updated values and relationships into
69      * "SET ATTR1 = Val1, ..." SQL statement.
70      */

71     private void buildSetClause(StringBuffer JavaDoc queryBuf, UpdateQuery query) {
72         Map JavaDoc updAttrs = query.getUpdAttributes();
73         // set of keys.. each key is supposed to be ObjAttribute
74
Iterator JavaDoc attrIt = updAttrs.entrySet().iterator();
75
76         if (!attrIt.hasNext())
77             throw new CayenneRuntimeException("Nothing to update.");
78
79         DbEntity dbEnt = getRootEntity().getDbEntity();
80         queryBuf.append(" SET ");
81
82         // append updated attribute values
83
boolean appendedSomething = false;
84
85         // now process other attrs in the loop
86
while (attrIt.hasNext()) {
87             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) attrIt.next();
88             String JavaDoc nextKey = (String JavaDoc) entry.getKey();
89             Object JavaDoc attrVal = entry.getValue();
90
91             if (appendedSomething)
92                 queryBuf.append(", ");
93
94             queryBuf.append(nextKey).append(" = ?");
95             super.addToParamList(
96                 (DbAttribute) dbEnt.getAttribute(nextKey),
97                 attrVal);
98             appendedSomething = true;
99         }
100     }
101 }
102
Popular Tags