KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > jdbc > CursorStatement


1 /*
2  * $Id: CursorStatement.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.jdbc;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31
32 import org.ofbiz.entity.transaction.GenericTransactionException;
33 import org.ofbiz.entity.transaction.TransactionUtil;
34
35 /**
36  *
37  * @version $Rev: 5462 $
38  * @since 3.1
39  */

40 public class CursorStatement extends AbstractCursorHandler {
41     
42     protected ResultSet JavaDoc currentResultSet;
43     protected Statement JavaDoc stmt;
44     protected boolean beganTransaction;
45     protected boolean autoCommit;
46
47     protected CursorStatement(Statement JavaDoc stmt, String JavaDoc cursorName, int fetchSize) throws GenericTransactionException, SQLException JavaDoc {
48         super(cursorName, fetchSize);
49         this.stmt = stmt;
50         beganTransaction = TransactionUtil.begin();
51         autoCommit = stmt.getConnection().getAutoCommit();
52         stmt.getConnection().setAutoCommit(false);
53         System.err.println("beganTransaction=" + beganTransaction + ", autoCommit=" + autoCommit);
54     }
55
56     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
57         if ("close".equals(method.getName())) {
58             stmt.getConnection().setAutoCommit(autoCommit);
59             TransactionUtil.commit(beganTransaction);
60             stmt.close();
61             return null;
62         } else if ("execute".equals(method.getName())) {
63         } else if ("executeQuery".equals(method.getName()) && args == null) {
64             PreparedStatement JavaDoc pstmt = (PreparedStatement JavaDoc) stmt;
65             pstmt.executeUpdate();
66             currentResultSet = CursorResultSet.newCursorResultSet(stmt, cursorName, fetchSize);
67             return currentResultSet;
68         } else if ("executeQuery".equals(method.getName()) && args != null) {
69             args[0] = "DECLARE " + cursorName + " CURSOR FOR " + args[0];
70             System.err.println("query=" + args[0]);
71             if (stmt.execute((String JavaDoc) args[0])) {
72                 throw new SQLException JavaDoc("DECLARE returned a ResultSet");
73             }
74             currentResultSet = CursorResultSet.newCursorResultSet(stmt, cursorName, fetchSize);
75             return currentResultSet;
76         } else if ("getMoreResults".equals(method.getName())) {
77             boolean hasMoreResults = stmt.getMoreResults();
78             if (hasMoreResults) {
79                 currentResultSet = stmt.getResultSet();
80             } else {
81                 currentResultSet = null;
82             }
83             return hasMoreResults ? Boolean.TRUE : Boolean.FALSE;
84         } else if ("getResultSet".equals(method.getName())) {
85             return currentResultSet;
86         } else if ("getCursorName".equals(method.getName())) {
87             return getCursorName();
88         } else if ("setCursorName".equals(method.getName())) {
89             setCursorName((String JavaDoc) args[0]);
90         } else if ("getFetchSize".equals(method.getName())) {
91             return new Integer JavaDoc(getFetchSize());
92         } else if ("setFetchSize".equals(method.getName())) {
93             setFetchSize(((Integer JavaDoc) args[0]).intValue());
94         }
95         return super.invoke(stmt, proxy, method, args);
96     }
97
98     public static Statement JavaDoc newCursorStatement(Statement JavaDoc stmt, String JavaDoc cursorName, int fetchSize) throws Exception JavaDoc {
99         return (Statement JavaDoc) newHandler(new CursorStatement(stmt, cursorName, fetchSize), Statement JavaDoc.class);
100     }
101
102     public static PreparedStatement JavaDoc newCursorPreparedStatement(PreparedStatement JavaDoc pstmt, String JavaDoc cursorName, int fetchSize) throws Exception JavaDoc {
103         return (PreparedStatement JavaDoc) newHandler(new CursorStatement(pstmt, cursorName, fetchSize), PreparedStatement JavaDoc.class);
104     }
105 }
106
Popular Tags