KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.cayenne.dba.DbAdapter;
30 import org.apache.cayenne.map.DbAttribute;
31 import org.apache.cayenne.query.BatchQuery;
32
33 /**
34  * Translator of InsertBatchQueries.
35  *
36  * @author Andriy Shapochka
37  * @author Andrus Adamchik
38  */

39 public class InsertBatchQueryBuilder extends BatchQueryBuilder {
40
41     public InsertBatchQueryBuilder(DbAdapter adapter) {
42         super.setAdapter(adapter);
43     }
44
45     /**
46      * Binds parameters for the current batch iteration to the PreparedStatement. Performs
47      * filtering of attributes based on column generation rules.
48      *
49      * @since 1.2
50      */

51     public void bindParameters(PreparedStatement JavaDoc statement, BatchQuery query)
52             throws SQLException JavaDoc, Exception JavaDoc {
53
54         List JavaDoc dbAttributes = query.getDbAttributes();
55         int attributeCount = dbAttributes.size();
56
57         // must use an independent counter "j" for prepared statement index
58
for (int i = 0, j = 0; i < attributeCount; i++) {
59             DbAttribute attribute = (DbAttribute) dbAttributes.get(i);
60             if (includeInBatch(attribute)) {
61                 j++;
62                 Object JavaDoc value = query.getValue(i);
63                 adapter.bindParameter(statement, value, j, attribute.getType(), attribute
64                         .getScale());
65             }
66         }
67     }
68
69     /**
70      * Returns a list of values for the current batch iteration. Performs filtering of
71      * attributes based on column generation rules. Used primarily for logging.
72      *
73      * @since 1.2
74      */

75     public List JavaDoc getParameterValues(BatchQuery query) {
76         List JavaDoc attributes = query.getDbAttributes();
77         int len = attributes.size();
78         List JavaDoc values = new ArrayList JavaDoc(len);
79         for (int i = 0; i < len; i++) {
80             DbAttribute attribute = (DbAttribute) attributes.get(i);
81             if (includeInBatch(attribute)) {
82                 values.add(query.getValue(i));
83             }
84         }
85         return values;
86     }
87
88     public String JavaDoc createSqlString(BatchQuery batch) {
89         String JavaDoc table = batch.getDbEntity().getFullyQualifiedName();
90         List JavaDoc dbAttributes = batch.getDbAttributes();
91
92         StringBuffer JavaDoc query = new StringBuffer JavaDoc("INSERT INTO ");
93         query.append(table).append(" (");
94
95         int columnCount = 0;
96         Iterator JavaDoc it = dbAttributes.iterator();
97
98         while (it.hasNext()) {
99             DbAttribute attribute = (DbAttribute) it.next();
100
101             // attribute inclusion rule - one of the rules below must be true:
102
// (1) attribute not generated
103
// (2) attribute is generated and PK and adapter does not support generated
104
// keys
105

106             if (includeInBatch(attribute)) {
107
108                 if (columnCount > 0) {
109                     query.append(", ");
110                 }
111                 query.append(attribute.getName());
112                 columnCount++;
113             }
114         }
115
116         query.append(") VALUES (");
117
118         for (int i = 0; i < columnCount; i++) {
119             if (i > 0) {
120                 query.append(", ");
121             }
122
123             query.append('?');
124         }
125         query.append(')');
126         return query.toString();
127     }
128
129     /**
130      * Returns true if an attribute should be included in the batch.
131      *
132      * @since 1.2
133      */

134     protected boolean includeInBatch(DbAttribute attribute) {
135         // attribute inclusion rule - one of the rules below must be true:
136
// (1) attribute not generated
137
// (2) attribute is generated and PK and adapter does not support generated
138
// keys
139

140         return !attribute.isGenerated()
141                 || (attribute.isPrimaryKey() && !adapter.supportsGeneratedKeys());
142     }
143 }
144
Popular Tags