KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.PreparedStatement JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.dba.DbAdapter;
29 import org.apache.cayenne.map.DbAttribute;
30 import org.apache.cayenne.query.BatchQuery;
31 import org.apache.cayenne.query.UpdateBatchQuery;
32
33 /**
34  * A translator for UpdateBatchQueries that produces parameterized SQL.
35  *
36  * @author Andriy Shapochka, Andrus Adamchik, Mike Kienenberger
37  */

38
39 public class UpdateBatchQueryBuilder extends BatchQueryBuilder {
40
41     public UpdateBatchQueryBuilder(DbAdapter adapter) {
42         super(adapter);
43     }
44
45     public String JavaDoc createSqlString(BatchQuery batch) {
46         UpdateBatchQuery updateBatch = (UpdateBatchQuery) batch;
47         String JavaDoc table = batch.getDbEntity().getFullyQualifiedName();
48         List JavaDoc qualifierAttributes = updateBatch.getQualifierAttributes();
49         List JavaDoc updatedDbAttributes = updateBatch.getUpdatedAttributes();
50
51         StringBuffer JavaDoc query = new StringBuffer JavaDoc("UPDATE ");
52         query.append(table).append(" SET ");
53
54         int len = updatedDbAttributes.size();
55         for (int i = 0; i < len; i++) {
56             if (i > 0) {
57                 query.append(", ");
58             }
59
60             DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i);
61             query.append(attribute.getName()).append(" = ?");
62         }
63
64         query.append(" WHERE ");
65
66         Iterator JavaDoc i = qualifierAttributes.iterator();
67         while (i.hasNext()) {
68             DbAttribute attribute = (DbAttribute) i.next();
69             appendDbAttribute(query, attribute);
70             query.append(updateBatch.isNull(attribute) ? " IS NULL" : " = ?");
71
72             if (i.hasNext()) {
73                 query.append(" AND ");
74             }
75         }
76
77         return query.toString();
78     }
79
80     /**
81      * Binds BatchQuery parameters to the PreparedStatement.
82      */

83     public void bindParameters(PreparedStatement JavaDoc statement, BatchQuery query)
84             throws SQLException JavaDoc, Exception JavaDoc {
85
86         UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
87         List JavaDoc qualifierAttributes = updateBatch.getQualifierAttributes();
88         List JavaDoc updatedDbAttributes = updateBatch.getUpdatedAttributes();
89
90         int len = updatedDbAttributes.size();
91         int parameterIndex = 1;
92         for (int i = 0; i < len; i++) {
93             Object JavaDoc value = query.getValue(i);
94
95             DbAttribute attribute = (DbAttribute) updatedDbAttributes.get(i);
96             adapter.bindParameter(
97                     statement,
98                     value,
99                     parameterIndex++,
100                     attribute.getType(),
101                     attribute.getScale());
102         }
103
104         for (int i = 0; i < qualifierAttributes.size(); i++) {
105             Object JavaDoc value = query.getValue(len + i);
106             DbAttribute attribute = (DbAttribute) qualifierAttributes.get(i);
107
108             // skip null attributes... they are translated as "IS NULL"
109
if (updateBatch.isNull(attribute)) {
110                 continue;
111             }
112
113             adapter.bindParameter(
114                     statement,
115                     value,
116                     parameterIndex++,
117                     attribute.getType(),
118                     attribute.getScale());
119         }
120     }
121 }
122
Popular Tags