KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dba > sqlserver > SQLServerProcedureAction


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 package org.objectstyle.cayenne.dba.sqlserver;
57
58 import java.sql.CallableStatement JavaDoc;
59 import java.sql.Connection JavaDoc;
60 import java.sql.ResultSet JavaDoc;
61 import java.sql.SQLException JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65
66 import org.apache.log4j.Level;
67 import org.objectstyle.cayenne.access.OperationObserver;
68 import org.objectstyle.cayenne.access.QueryLogger;
69 import org.objectstyle.cayenne.access.ResultIterator;
70 import org.objectstyle.cayenne.access.jdbc.ProcedureAction;
71 import org.objectstyle.cayenne.access.jdbc.RowDescriptor;
72 import org.objectstyle.cayenne.access.trans.ProcedureTranslator;
73 import org.objectstyle.cayenne.dba.DbAdapter;
74 import org.objectstyle.cayenne.map.EntityResolver;
75 import org.objectstyle.cayenne.query.ProcedureQuery;
76 import org.objectstyle.cayenne.query.Query;
77
78 /**
79  * ProcedureAction for SQLServer MS JDBC driver. Customizes OUT parameter processing - it
80  * has to be done AFTER the ResultSets are read (note that jTDS driver works fine with
81  * normal ProcedureAction).
82  * <p>
83  * <i>See JIRA CAY-251 for details. </i>
84  * </p>
85  *
86  * @since 1.2
87  * @author Andrei Adamchik
88  */

89 public class SQLServerProcedureAction extends ProcedureAction {
90
91     public SQLServerProcedureAction(ProcedureQuery query, DbAdapter adapter,
92             EntityResolver entityResolver) {
93         super(query, adapter, entityResolver);
94     }
95
96     public void performAction(Connection JavaDoc connection, OperationObserver observer)
97             throws SQLException JavaDoc, Exception JavaDoc {
98
99         ProcedureTranslator transl = new ProcedureTranslator();
100         transl.setAdapter(getAdapter());
101         transl.setQuery(query);
102         transl.setEntityResolver(this.getEntityResolver());
103         transl.setConnection(connection);
104
105         CallableStatement JavaDoc statement = (CallableStatement JavaDoc) transl.createStatement(query
106                 .getLoggingLevel());
107
108         try {
109             // stored procedure may contain a mixture of update counts and result sets,
110
// and out parameters. Read out parameters first, then
111
// iterate until we exhaust all results
112
boolean hasResultSet = statement.execute();
113
114             // local observer to cache results and provide them to the external observer
115
// in the order consistent with other adapters.
116

117             Observer localObserver = new Observer(observer);
118
119             // read query, using local observer
120

121             while (true) {
122                 if (hasResultSet) {
123                     ResultSet JavaDoc rs = statement.getResultSet();
124                     try {
125                         RowDescriptor descriptor = describeResultSet(
126                                 rs,
127                                 processedResultSets++);
128                         readResultSet(rs, descriptor, query, localObserver);
129                     }
130                     finally {
131                         try {
132                             rs.close();
133                         }
134                         catch (SQLException JavaDoc ex) {
135                         }
136                     }
137                 }
138                 else {
139                     int updateCount = statement.getUpdateCount();
140                     if (updateCount == -1) {
141                         break;
142                     }
143                     QueryLogger.logUpdateCount(query.getLoggingLevel(), updateCount);
144                     localObserver.nextCount(query, updateCount);
145                 }
146
147                 hasResultSet = statement.getMoreResults();
148             }
149
150             // read out parameters to the main observer ... AFTER the main result set
151
// TODO: I hope SQLServer does not support ResultSets as OUT parameters, otherwise
152
// the order of custom result descriptors will be messed up
153
readProcedureOutParameters(statement, observer);
154
155             // add results back to main observer
156
localObserver.flushResults(query);
157         }
158         finally {
159             try {
160                 statement.close();
161             }
162             catch (SQLException JavaDoc ex) {
163
164             }
165         }
166     }
167
168     class Observer implements OperationObserver {
169
170         List JavaDoc results;
171         List JavaDoc counts;
172         OperationObserver observer;
173
174         Observer(OperationObserver observer) {
175             this.observer = observer;
176         }
177
178         void flushResults(Query query) {
179             if (results != null) {
180                 Iterator JavaDoc it = results.iterator();
181                 while (it.hasNext()) {
182                     observer.nextDataRows(query, (List JavaDoc) it.next());
183                 }
184
185                 results = null;
186             }
187
188             if (counts != null) {
189                 Iterator JavaDoc it = counts.iterator();
190                 while (it.hasNext()) {
191                     observer.nextCount(query, ((Number JavaDoc) it.next()).intValue());
192                 }
193
194                 counts = null;
195             }
196         }
197
198         public void nextBatchCount(Query query, int[] resultCount) {
199             observer.nextBatchCount(query, resultCount);
200         }
201
202         public void nextCount(Query query, int resultCount) {
203             // does not delegate to wrapped observer
204
// but instead caches results locally.
205
if (counts == null) {
206                 counts = new ArrayList JavaDoc();
207             }
208
209             counts.add(new Integer JavaDoc(resultCount));
210         }
211
212         public void nextDataRows(Query query, List JavaDoc dataRows) {
213             // does not delegate to wrapped observer
214
// but instead caches results locally.
215
if (results == null) {
216                 results = new ArrayList JavaDoc();
217             }
218
219             results.add(dataRows);
220         }
221
222         public void nextDataRows(Query q, ResultIterator it) {
223             observer.nextDataRows(q, it);
224         }
225
226         public void nextGlobalException(Exception JavaDoc ex) {
227             observer.nextGlobalException(ex);
228         }
229
230         public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) {
231             observer.nextGeneratedDataRows(query, keysIterator);
232         }
233
234         public void nextQueryException(Query query, Exception JavaDoc ex) {
235             observer.nextQueryException(query, ex);
236         }
237
238         public Level getLoggingLevel() {
239             return observer.getLoggingLevel();
240         }
241
242         public boolean isIteratedResult() {
243             return observer.isIteratedResult();
244         }
245     }
246 }
Popular Tags