KickJava   Java API By Example, From Geeks To Geeks.

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


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.DeleteBatchQuery;
32
33 /**
34  * Translator for delete BatchQueries. Creates parametrized DELETE SQL statements.
35  *
36  * @author Andriy Shapochka, Andrus Adamchik, Mike Kienenberger
37  */

38
39 public class DeleteBatchQueryBuilder extends BatchQueryBuilder {
40
41     public DeleteBatchQueryBuilder(DbAdapter adapter) {
42         super(adapter);
43     }
44
45     public String JavaDoc createSqlString(BatchQuery batch) {
46         DeleteBatchQuery deleteBatch = (DeleteBatchQuery) batch;
47         String JavaDoc table = batch.getDbEntity().getFullyQualifiedName();
48         List JavaDoc qualifierAttributes = deleteBatch.getQualifierAttributes();
49
50         StringBuffer JavaDoc query = new StringBuffer JavaDoc("DELETE FROM ");
51         query.append(table).append(" WHERE ");
52
53         Iterator JavaDoc i = qualifierAttributes.iterator();
54         while (i.hasNext()) {
55             DbAttribute attribute = (DbAttribute) i.next();
56             appendDbAttribute(query, attribute);
57             query.append(deleteBatch.isNull(attribute) ? " IS NULL" : " = ?");
58
59             if (i.hasNext()) {
60                 query.append(" AND ");
61             }
62         }
63
64         return query.toString();
65     }
66
67     /**
68      * Binds BatchQuery parameters to the PreparedStatement.
69      */

70     public void bindParameters(PreparedStatement JavaDoc statement, BatchQuery query)
71             throws SQLException JavaDoc, Exception JavaDoc {
72
73         DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
74         List JavaDoc qualifierAttributes = deleteBatch.getQualifierAttributes();
75
76         int parameterIndex = 1;
77
78         for (int i = 0; i < qualifierAttributes.size(); i++) {
79             Object JavaDoc value = query.getValue(i);
80             DbAttribute attribute = (DbAttribute) qualifierAttributes.get(i);
81
82             // skip null attributes... they are translated as "IS NULL"
83
if (deleteBatch.isNull(attribute)) {
84                 continue;
85             }
86
87             adapter.bindParameter(
88                     statement,
89                     value,
90                     parameterIndex++,
91                     attribute.getType(),
92                     attribute.getScale());
93         }
94     }
95 }
96
Popular Tags