01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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: *******************************************************************************/package org.ofbiz.entity.jdbc;
19:
20: import java.lang.reflect.Method;
21: import java.sql.ResultSet;
22: import java.sql.SQLException;
23: import java.sql.Statement;
24:
25: public class CursorResultSet extends AbstractCursorHandler {
26:
27: protected ResultSet rs;
28: protected Statement stmt;
29: protected String query;
30:
31: protected CursorResultSet(Statement stmt, String cursorName,
32: int fetchSize) throws SQLException {
33: super (cursorName, fetchSize);
34: this .stmt = stmt;
35: query = "FETCH FORWARD " + fetchSize + " IN " + cursorName;
36: System.err.println("executing page fetch(1)");
37: rs = stmt.executeQuery(query);
38: }
39:
40: public Object invoke(Object proxy, Method method, Object[] args)
41: throws Throwable {
42: if ("close".equals(method.getName())) {
43: close();
44: return null;
45: } else if ("next".equals(method.getName())) {
46: return next() ? Boolean.TRUE : Boolean.FALSE;
47: }
48: return super .invoke(rs, proxy, method, args);
49: }
50:
51: protected boolean next() throws SQLException {
52: if (rs.next())
53: return true;
54: System.err.println("executing page fetch(2)");
55: rs = stmt.executeQuery(query);
56: return rs.next();
57: }
58:
59: protected void close() throws SQLException {
60: stmt.executeUpdate("CLOSE " + cursorName);
61: rs.close();
62: }
63:
64: public static ResultSet newCursorResultSet(Statement stmt,
65: String cursorName, int fetchSize) throws SQLException,
66: Exception {
67: return (ResultSet) newHandler(new CursorResultSet(stmt,
68: cursorName, fetchSize), ResultSet.class);
69: }
70: }
|