001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.jdbc;
019:
020: import java.lang.reflect.Method;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025:
026: import org.ofbiz.entity.transaction.GenericTransactionException;
027: import org.ofbiz.entity.transaction.TransactionUtil;
028:
029: public class CursorStatement extends AbstractCursorHandler {
030:
031: protected ResultSet currentResultSet;
032: protected Statement stmt;
033: protected boolean beganTransaction;
034: protected boolean autoCommit;
035:
036: protected CursorStatement(Statement stmt, String cursorName,
037: int fetchSize) throws GenericTransactionException,
038: SQLException {
039: super (cursorName, fetchSize);
040: this .stmt = stmt;
041: beganTransaction = TransactionUtil.begin();
042: autoCommit = stmt.getConnection().getAutoCommit();
043: stmt.getConnection().setAutoCommit(false);
044: System.err.println("beganTransaction=" + beganTransaction
045: + ", autoCommit=" + autoCommit);
046: }
047:
048: public Object invoke(Object proxy, Method method, Object[] args)
049: throws Throwable {
050: if ("close".equals(method.getName())) {
051: stmt.getConnection().setAutoCommit(autoCommit);
052: TransactionUtil.commit(beganTransaction);
053: stmt.close();
054: return null;
055: } else if ("execute".equals(method.getName())) {
056: } else if ("executeQuery".equals(method.getName())
057: && args == null) {
058: PreparedStatement pstmt = (PreparedStatement) stmt;
059: pstmt.executeUpdate();
060: currentResultSet = CursorResultSet.newCursorResultSet(stmt,
061: cursorName, fetchSize);
062: return currentResultSet;
063: } else if ("executeQuery".equals(method.getName())
064: && args != null) {
065: args[0] = "DECLARE " + cursorName + " CURSOR FOR "
066: + args[0];
067: System.err.println("query=" + args[0]);
068: if (stmt.execute((String) args[0])) {
069: throw new SQLException("DECLARE returned a ResultSet");
070: }
071: currentResultSet = CursorResultSet.newCursorResultSet(stmt,
072: cursorName, fetchSize);
073: return currentResultSet;
074: } else if ("getMoreResults".equals(method.getName())) {
075: boolean hasMoreResults = stmt.getMoreResults();
076: if (hasMoreResults) {
077: currentResultSet = stmt.getResultSet();
078: } else {
079: currentResultSet = null;
080: }
081: return hasMoreResults ? Boolean.TRUE : Boolean.FALSE;
082: } else if ("getResultSet".equals(method.getName())) {
083: return currentResultSet;
084: } else if ("getCursorName".equals(method.getName())) {
085: return getCursorName();
086: } else if ("setCursorName".equals(method.getName())) {
087: setCursorName((String) args[0]);
088: } else if ("getFetchSize".equals(method.getName())) {
089: return new Integer(getFetchSize());
090: } else if ("setFetchSize".equals(method.getName())) {
091: setFetchSize(((Integer) args[0]).intValue());
092: }
093: return super .invoke(stmt, proxy, method, args);
094: }
095:
096: public static Statement newCursorStatement(Statement stmt,
097: String cursorName, int fetchSize) throws Exception {
098: return (Statement) newHandler(new CursorStatement(stmt,
099: cursorName, fetchSize), Statement.class);
100: }
101:
102: public static PreparedStatement newCursorPreparedStatement(
103: PreparedStatement pstmt, String cursorName, int fetchSize)
104: throws Exception {
105: return (PreparedStatement) newHandler(new CursorStatement(
106: pstmt, cursorName, fetchSize), PreparedStatement.class);
107: }
108: }
|