KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > oracle > OracleProcedureAction


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.dba.oracle;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.cayenne.access.OperationObserver;
31 import org.apache.cayenne.access.QueryLogger;
32 import org.apache.cayenne.access.jdbc.ColumnDescriptor;
33 import org.apache.cayenne.access.jdbc.ProcedureAction;
34 import org.apache.cayenne.access.jdbc.RowDescriptor;
35 import org.apache.cayenne.access.types.ExtendedType;
36 import org.apache.cayenne.dba.DbAdapter;
37 import org.apache.cayenne.map.EntityResolver;
38 import org.apache.cayenne.map.ProcedureParameter;
39 import org.apache.cayenne.query.ProcedureQuery;
40
41 /**
42  * Oracle-specific ProcedureAction that supports ResultSet OUT parameters.
43  *
44  * @since 1.2
45  * @author Andrus Adamchik
46  */

47 class OracleProcedureAction extends ProcedureAction {
48
49     OracleProcedureAction(ProcedureQuery query, DbAdapter adapter,
50             EntityResolver entityResolver) {
51         super(query, adapter, entityResolver);
52     }
53
54     /**
55      * Helper method that reads OUT parameters of a CallableStatement.
56      */

57     protected void readProcedureOutParameters(
58             CallableStatement JavaDoc statement,
59             OperationObserver delegate) throws SQLException JavaDoc, Exception JavaDoc {
60
61         long t1 = System.currentTimeMillis();
62
63         // build result row...
64
Map JavaDoc result = null;
65         List JavaDoc parameters = getProcedure().getCallParameters();
66         for (int i = 0; i < parameters.size(); i++) {
67             ProcedureParameter parameter = (ProcedureParameter) parameters.get(i);
68
69             if (!parameter.isOutParam()) {
70                 continue;
71             }
72
73             // ==== start Oracle-specific part
74
if (parameter.getType() == OracleAdapter.getOracleCursorType()) {
75                 ResultSet JavaDoc rs = (ResultSet JavaDoc) statement.getObject(i + 1);
76
77                 try {
78                     RowDescriptor rsDescriptor = describeResultSet(
79                             rs,
80                             processedResultSets++);
81                     readResultSet(rs, rsDescriptor, query, delegate);
82                 }
83                 finally {
84                     try {
85                         rs.close();
86                     }
87                     catch (SQLException JavaDoc ex) {
88                     }
89                 }
90             }
91             // ==== end Oracle-specific part
92
else {
93                 if (result == null) {
94                     result = new HashMap JavaDoc();
95                 }
96
97                 ColumnDescriptor descriptor = new ColumnDescriptor(parameter);
98                 ExtendedType type = getAdapter().getExtendedTypes().getRegisteredType(
99                         descriptor.getJavaClass());
100                 Object JavaDoc val = type.materializeObject(statement, i + 1, descriptor
101                         .getJdbcType());
102
103                 result.put(descriptor.getLabel(), val);
104             }
105         }
106
107         if (result != null && !result.isEmpty()) {
108             // treat out parameters as a separate data row set
109
QueryLogger.logSelectCount(1, System.currentTimeMillis() - t1);
110             delegate.nextDataRows(query, Collections.singletonList(result));
111         }
112     }
113 }
114
Popular Tags