KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Types JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.dba.DbAdapter;
31 import org.apache.cayenne.dba.TypesMapping;
32 import org.apache.cayenne.map.DbAttribute;
33 import org.apache.cayenne.query.BatchQuery;
34
35 /**
36  * Superclass of query builders for the DML operations involving LOBs.
37  *
38  * @author Andrus Adamchik
39  */

40 public abstract class LOBBatchQueryBuilder extends BatchQueryBuilder {
41
42     protected String JavaDoc newClobFunction;
43     protected String JavaDoc newBlobFunction;
44
45     public LOBBatchQueryBuilder(DbAdapter adapter) {
46         super(adapter);
47     }
48
49     public abstract List JavaDoc getValuesForLOBUpdateParameters(BatchQuery query);
50
51     public String JavaDoc createLOBSelectString(
52             BatchQuery updateQuery,
53             List JavaDoc selectedLOBAttributes,
54             List JavaDoc qualifierAttributes) {
55
56         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
57         buf.append("SELECT ");
58
59         Iterator JavaDoc it = selectedLOBAttributes.iterator();
60         while (it.hasNext()) {
61             buf.append(((DbAttribute) it.next()).getName());
62
63             if (it.hasNext()) {
64                 buf.append(", ");
65             }
66         }
67
68         buf
69                 .append(" FROM ")
70                 .append(updateQuery.getDbEntity().getFullyQualifiedName())
71                 .append(" WHERE ");
72
73         it = qualifierAttributes.iterator();
74         while (it.hasNext()) {
75             DbAttribute attribute = (DbAttribute) it.next();
76             appendDbAttribute(buf, attribute);
77             buf.append(" = ?");
78             if (it.hasNext()) {
79                 buf.append(" AND ");
80             }
81         }
82
83         buf.append(" FOR UPDATE");
84         return buf.toString();
85     }
86
87     /**
88      * Appends parameter placeholder for the value of the column being updated. If
89      * requested, performs special handling on LOB columns.
90      */

91     protected void appendUpdatedParameter(
92             StringBuffer JavaDoc buf,
93             DbAttribute dbAttribute,
94             Object JavaDoc value) {
95
96         int type = dbAttribute.getType();
97
98         if (isUpdateableColumn(value, type)) {
99             buf.append('?');
100         }
101         else {
102             if (type == Types.CLOB) {
103                 buf.append(newClobFunction);
104             }
105             else if (type == Types.BLOB) {
106                 buf.append(newBlobFunction);
107             }
108             else {
109                 throw new CayenneRuntimeException("Unknown LOB column type: "
110                         + type
111                         + "("
112                         + TypesMapping.getSqlNameByType(type)
113                         + "). Query buffer: "
114                         + buf);
115             }
116         }
117     }
118
119     /**
120      * Binds BatchQuery parameters to the PreparedStatement.
121      */

122     public void bindParameters(PreparedStatement JavaDoc statement, BatchQuery query)
123             throws SQLException JavaDoc, Exception JavaDoc {
124
125         List JavaDoc dbAttributes = query.getDbAttributes();
126         int attributeCount = dbAttributes.size();
127
128         // i - attribute position in the query
129
// j - PreparedStatement parameter position (starts with "1")
130
for (int i = 0, j = 1; i < attributeCount; i++) {
131             Object JavaDoc value = query.getValue(i);
132             DbAttribute attribute = (DbAttribute) dbAttributes.get(i);
133             int type = attribute.getType();
134
135             // TODO: (Andrus) This works as long as there is no LOBs in qualifier
136
if (isUpdateableColumn(value, type)) {
137                 adapter
138                         .bindParameter(statement, value, j, type, attribute
139                                 .getScale());
140
141                 j++;
142             }
143         }
144     }
145
146     protected boolean isUpdateableColumn(Object JavaDoc value, int type) {
147         return value == null || (type != Types.BLOB && type != Types.CLOB);
148     }
149
150     public String JavaDoc getNewBlobFunction() {
151         return newBlobFunction;
152     }
153
154     public String JavaDoc getNewClobFunction() {
155         return newClobFunction;
156     }
157
158     public void setNewBlobFunction(String JavaDoc string) {
159         newBlobFunction = string;
160     }
161
162     public void setNewClobFunction(String JavaDoc string) {
163         newClobFunction = string;
164     }
165 }
166
Popular Tags