KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 import org.apache.cayenne.access.OperationObserver;
29 import org.apache.cayenne.access.QueryLogger;
30 import org.apache.cayenne.access.jdbc.SQLStatement;
31 import org.apache.cayenne.access.jdbc.SQLTemplateAction;
32 import org.apache.cayenne.dba.DbAdapter;
33 import org.apache.cayenne.query.SQLTemplate;
34
35 /**
36  * A SQLTemplateAction that addresses Oracle 8 driver limitations.
37  *
38  * @since 1.2
39  * @author Andrus Adamchik
40  */

41 class Oracle8SQLTemplateAction extends SQLTemplateAction {
42
43     Oracle8SQLTemplateAction(SQLTemplate query, DbAdapter adapter) {
44         super(query, adapter);
45     }
46
47     /**
48      * Overrides super implementation to guess whether the query is selecting or not and
49      * execute it appropriately. Super implementation relied on generic JDBC mechanism,
50      * common for selecting and updating statements that does not work in Oracle 8.*
51      * drivers.
52      */

53     protected void execute(
54             Connection JavaDoc connection,
55             OperationObserver callback,
56             SQLStatement compiled,
57             Collection JavaDoc updateCounts) throws SQLException JavaDoc, Exception JavaDoc {
58
59         String JavaDoc sql = compiled.getSql().trim();
60         boolean select = sql.length() > "SELECT".length()
61                 && sql.substring(0, "SELECT".length()).equalsIgnoreCase("SELECT");
62
63         long t1 = System.currentTimeMillis();
64         boolean iteratedResult = callback.isIteratedResult();
65         PreparedStatement JavaDoc statement = connection.prepareStatement(compiled.getSql());
66         try {
67             bind(statement, compiled.getBindings());
68
69             // start - code different from super
70
if (select) {
71
72                 ResultSet JavaDoc resultSet = statement.executeQuery();
73                 try {
74                     processSelectResult(
75                             compiled,
76                             connection,
77                             statement,
78                             resultSet,
79                             callback,
80                             t1);
81                 }
82                 finally {
83                     if (!iteratedResult) {
84                         resultSet.close();
85                     }
86                 }
87             }
88             else {
89                 int updateCount = statement.executeUpdate();
90                 updateCounts.add(new Integer JavaDoc(updateCount));
91                 QueryLogger.logUpdateCount(updateCount);
92             }
93
94             // end - code different from super
95
}
96         finally {
97             if (!iteratedResult) {
98                 statement.close();
99             }
100         }
101     }
102 }
103
Popular Tags