1 24 package org.ofbiz.entity.jdbc; 25 26 import java.lang.reflect.Method ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.Statement ; 31 32 import org.ofbiz.entity.transaction.GenericTransactionException; 33 import org.ofbiz.entity.transaction.TransactionUtil; 34 35 40 public class CursorStatement extends AbstractCursorHandler { 41 42 protected ResultSet currentResultSet; 43 protected Statement stmt; 44 protected boolean beganTransaction; 45 protected boolean autoCommit; 46 47 protected CursorStatement(Statement stmt, String cursorName, int fetchSize) throws GenericTransactionException, SQLException { 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 invoke(Object proxy, Method method, Object [] args) throws Throwable { 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 pstmt = (PreparedStatement ) 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 ) args[0])) { 72 throw new SQLException ("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 ) args[0]); 90 } else if ("getFetchSize".equals(method.getName())) { 91 return new Integer (getFetchSize()); 92 } else if ("setFetchSize".equals(method.getName())) { 93 setFetchSize(((Integer ) args[0]).intValue()); 94 } 95 return super.invoke(stmt, proxy, method, args); 96 } 97 98 public static Statement newCursorStatement(Statement stmt, String cursorName, int fetchSize) throws Exception { 99 return (Statement ) newHandler(new CursorStatement(stmt, cursorName, fetchSize), Statement .class); 100 } 101 102 public static PreparedStatement newCursorPreparedStatement(PreparedStatement pstmt, String cursorName, int fetchSize) throws Exception { 103 return (PreparedStatement ) newHandler(new CursorStatement(pstmt, cursorName, fetchSize), PreparedStatement .class); 104 } 105 } 106 | Popular Tags |