KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.access.trans;
58
59 import java.sql.PreparedStatement JavaDoc;
60 import java.sql.SQLException JavaDoc;
61 import java.sql.Types JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64
65 import org.objectstyle.cayenne.CayenneRuntimeException;
66 import org.objectstyle.cayenne.dba.DbAdapter;
67 import org.objectstyle.cayenne.dba.TypesMapping;
68 import org.objectstyle.cayenne.map.DbAttribute;
69 import org.objectstyle.cayenne.query.BatchQuery;
70
71 /**
72  * Superclass of query builders for the DML operations involving LOBs.
73  *
74  * @author Andrei Adamchik
75  */

76 public abstract class LOBBatchQueryBuilder extends BatchQueryBuilder {
77
78     protected String JavaDoc newClobFunction;
79     protected String JavaDoc newBlobFunction;
80
81     public LOBBatchQueryBuilder(DbAdapter adapter) {
82         super(adapter);
83     }
84
85     public abstract List JavaDoc getValuesForLOBUpdateParameters(BatchQuery query);
86
87     public String JavaDoc createLOBSelectString(
88             BatchQuery updateQuery,
89             List JavaDoc selectedLOBAttributes,
90             List JavaDoc qualifierAttributes) {
91
92         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93         buf.append("SELECT ");
94
95         Iterator JavaDoc it = selectedLOBAttributes.iterator();
96         while (it.hasNext()) {
97             buf.append(((DbAttribute) it.next()).getName());
98
99             if (it.hasNext()) {
100                 buf.append(", ");
101             }
102         }
103
104         buf
105                 .append(" FROM ")
106                 .append(updateQuery.getDbEntity().getFullyQualifiedName())
107                 .append(" WHERE ");
108
109         it = qualifierAttributes.iterator();
110         while (it.hasNext()) {
111             DbAttribute attribute = (DbAttribute) it.next();
112             appendDbAttribute(buf, attribute);
113             buf.append(" = ?");
114             if (it.hasNext()) {
115                 buf.append(" AND ");
116             }
117         }
118
119         buf.append(" FOR UPDATE");
120         return buf.toString();
121     }
122
123     /**
124      * Appends parameter placeholder for the value of the column being updated. If
125      * requested, performs special handling on LOB columns.
126      */

127     protected void appendUpdatedParameter(
128             StringBuffer JavaDoc buf,
129             DbAttribute dbAttribute,
130             Object JavaDoc value) {
131
132         int type = dbAttribute.getType();
133
134         if (isUpdateableColumn(value, type)) {
135             buf.append('?');
136         }
137         else {
138             if (type == Types.CLOB) {
139                 buf.append(newClobFunction);
140             }
141             else if (type == Types.BLOB) {
142                 buf.append(newBlobFunction);
143             }
144             else {
145                 throw new CayenneRuntimeException("Unknown LOB column type: "
146                         + type
147                         + "("
148                         + TypesMapping.getSqlNameByType(type)
149                         + "). Query buffer: "
150                         + buf);
151             }
152         }
153     }
154
155     /**
156      * Binds BatchQuery parameters to the PreparedStatement.
157      */

158     public void bindParameters(PreparedStatement JavaDoc statement, BatchQuery query)
159             throws SQLException JavaDoc, Exception JavaDoc {
160
161         List JavaDoc dbAttributes = query.getDbAttributes();
162         int attributeCount = dbAttributes.size();
163
164         // i - attribute position in the query
165
// j - PreparedStatement parameter position (starts with "1")
166
for (int i = 0, j = 1; i < attributeCount; i++) {
167             Object JavaDoc value = query.getValue(i);
168             DbAttribute attribute = (DbAttribute) dbAttributes.get(i);
169             int type = attribute.getType();
170
171             // TODO: (Andrus) This works as long as there is no LOBs in qualifier
172
if (isUpdateableColumn(value, type)) {
173                 adapter
174                         .bindParameter(statement, value, j, type, attribute
175                                 .getPrecision());
176
177                 j++;
178             }
179         }
180     }
181
182     protected boolean isUpdateableColumn(Object JavaDoc value, int type) {
183         return value == null || (type != Types.BLOB && type != Types.CLOB);
184     }
185
186     public String JavaDoc getNewBlobFunction() {
187         return newBlobFunction;
188     }
189
190     public String JavaDoc getNewClobFunction() {
191         return newClobFunction;
192     }
193
194     public void setNewBlobFunction(String JavaDoc string) {
195         newBlobFunction = string;
196     }
197
198     public void setNewClobFunction(String JavaDoc string) {
199         newClobFunction = string;
200     }
201 }
Popular Tags