KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.apache.cayenne.access.trans;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.cayenne.dba.DbAdapter;
28 import org.apache.cayenne.map.DbAttribute;
29 import org.apache.cayenne.query.BatchQuery;
30 import org.apache.cayenne.query.UpdateBatchQuery;
31
32 /**
33  * @author Andrus Adamchik
34  */

35 public class LOBUpdateBatchQueryBuilder extends LOBBatchQueryBuilder {
36
37     public LOBUpdateBatchQueryBuilder(DbAdapter adapter) {
38         super(adapter);
39     }
40
41     public List JavaDoc getValuesForLOBUpdateParameters(BatchQuery query) {
42         int len = query.getDbAttributes().size();
43         UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
44
45         List JavaDoc values = new ArrayList JavaDoc(len);
46         List JavaDoc qualifierAttributes = updateBatch.getQualifierAttributes();
47         List JavaDoc updatedDbAttributes = updateBatch.getUpdatedAttributes();
48
49         int updatedLen = updatedDbAttributes.size();
50         int qualifierLen = qualifierAttributes.size();
51         for (int i = 0; i < updatedLen; i++) {
52             DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i);
53             Object JavaDoc value = query.getValue(i);
54             if(isUpdateableColumn(value, attribute.getType())) {
55                 values.add(value);
56             }
57         }
58
59         for (int i = 0; i < qualifierLen; i++) {
60             values.add(query.getValue(updatedLen + i));
61         }
62
63         return values;
64     }
65
66     public String JavaDoc createSqlString(BatchQuery batch) {
67         UpdateBatchQuery updateBatch = (UpdateBatchQuery) batch;
68         String JavaDoc table = batch.getDbEntity().getFullyQualifiedName();
69         List JavaDoc idDbAttributes = updateBatch.getQualifierAttributes();
70         List JavaDoc updatedDbAttributes = updateBatch.getUpdatedAttributes();
71         StringBuffer JavaDoc query = new StringBuffer JavaDoc("UPDATE ");
72         query.append(table).append(" SET ");
73
74         int len = updatedDbAttributes.size();
75         for (int i = 0; i < len; i++) {
76             if (i > 0) {
77                 query.append(", ");
78             }
79
80             DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i);
81             query.append(attribute.getName()).append(" = ");
82             appendUpdatedParameter(query, attribute, batch.getValue(i));
83         }
84
85         query.append(" WHERE ");
86         Iterator JavaDoc i = idDbAttributes.iterator();
87         while (i.hasNext()) {
88             DbAttribute attribute = (DbAttribute) i.next();
89             appendDbAttribute(query, attribute);
90             query.append(" = ?");
91             if (i.hasNext()) {
92                 query.append(" AND ");
93             }
94         }
95         return query.toString();
96     }
97 }
98
Popular Tags