KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.PreparedStatement JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Types JavaDoc;
25 import java.util.ArrayList 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
32 /**
33  * Superclass of batch query translators.
34  *
35  * @author Andriy Shapochka, Andrus Adamchik
36  */

37
38 public abstract class BatchQueryBuilder {
39
40     protected DbAdapter adapter;
41     protected String JavaDoc trimFunction;
42
43     public BatchQueryBuilder() {
44     }
45
46     public BatchQueryBuilder(DbAdapter adapter) {
47         this.adapter = adapter;
48     }
49
50     /**
51      * Translates BatchQuery into an SQL string formatted to use in a PreparedStatement.
52      */

53     public abstract String JavaDoc createSqlString(BatchQuery batch);
54
55     /**
56      * Appends the name of the column to the query buffer. Subclasses use this method to
57      * append column names in the WHERE clause, i.e. for the columns that are not being
58      * updated.
59      */

60     protected void appendDbAttribute(StringBuffer JavaDoc buf, DbAttribute dbAttribute) {
61
62         // TODO: (Andrus) is there a need for trimming binary types?
63
boolean trim = dbAttribute.getType() == Types.CHAR && trimFunction != null;
64         if (trim) {
65             buf.append(trimFunction).append('(');
66         }
67
68         buf.append(dbAttribute.getName());
69
70         if (trim) {
71             buf.append(')');
72         }
73     }
74
75     public void setAdapter(DbAdapter adapter) {
76         this.adapter = adapter;
77     }
78
79     public DbAdapter getAdapter() {
80         return adapter;
81     }
82
83     public String JavaDoc getTrimFunction() {
84         return trimFunction;
85     }
86
87     public void setTrimFunction(String JavaDoc string) {
88         trimFunction = string;
89     }
90
91     /**
92      * Binds parameters for the current batch iteration to the PreparedStatement.
93      *
94      * @since 1.2
95      */

96     public void bindParameters(PreparedStatement JavaDoc statement, BatchQuery query)
97             throws SQLException JavaDoc, Exception JavaDoc {
98
99         List JavaDoc dbAttributes = query.getDbAttributes();
100         int attributeCount = dbAttributes.size();
101
102         for (int i = 0; i < attributeCount; i++) {
103             Object JavaDoc value = query.getValue(i);
104             DbAttribute attribute = (DbAttribute) dbAttributes.get(i);
105             adapter.bindParameter(statement, value, i + 1, attribute.getType(), attribute
106                     .getScale());
107
108         }
109     }
110
111     /**
112      * Returns a list of values for the current batch iteration. Used primarily for
113      * logging.
114      *
115      * @since 1.2
116      */

117     public List JavaDoc getParameterValues(BatchQuery query) {
118         List JavaDoc attributes = query.getDbAttributes();
119         int len = attributes.size();
120         List JavaDoc values = new ArrayList JavaDoc(len);
121         for (int i = 0; i < len; i++) {
122             values.add(query.getValue(i));
123         }
124         return values;
125     }
126 }
127
Popular Tags