001: /*
002: * $Id: IntrospectingCallableStatementResultSetHandler.java,v 1.7 2004/10/05 20:13:04 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/test/java/mock/IntrospectingCallableStatementResultSetHandler.java,v $
004: * SQLUnit - a test harness for unit testing database stored procedures.
005: * Copyright (C) 2003 The SQLUnit Team
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: package net.sourceforge.sqlunit.test.mock;
022:
023: import com.mockrunner.jdbc.CallableStatementResultSetHandler;
024: import com.mockrunner.mock.jdbc.MockResultSet;
025:
026: import org.apache.log4j.Logger;
027:
028: import java.sql.SQLException;
029:
030: /**
031: * Extends the CallableStatementResultSetHandler to use introspection.
032: * @author Sujit Pal (spal@users.sourceforge.net)
033: * @version $Revision: 1.7 $
034: */
035: public class IntrospectingCallableStatementResultSetHandler extends
036: CallableStatementResultSetHandler {
037:
038: private static final Logger LOG = Logger
039: .getLogger(IntrospectingCallableStatementResultSetHandler.class);
040:
041: private IntrospectingResultSetFactory factory;
042: private String sql;
043: private int resultSetIndex;
044: private String catalog;
045:
046: /**
047: * Instantiate a CallableStatementResultSetHandler that depends on
048: * Introspection.
049: * @param catalog the name of the class to introspect.
050: */
051: public IntrospectingCallableStatementResultSetHandler(
052: final String catalog) {
053: LOG.debug(">> [IntrospectingCallableStatementResultSetHandler("
054: + catalog + ")]");
055: this .catalog = catalog;
056: factory = new IntrospectingResultSetFactory(this .catalog);
057: }
058:
059: /**
060: * Returns the current result set for the SQL call.
061: * @param sqlString the SQL string to execute.
062: * @return a MockResultSetObject.
063: */
064: public final MockResultSet getResultSet(final String sqlString) {
065: LOG.debug(">> getResultSet(" + sqlString + ")");
066: this .sql = sqlString;
067: return factory.create(sql, resultSetIndex);
068: }
069:
070: /**
071: * Returns true of there are more result sets available for this SQL
072: * call. Also increments the result set index as a side effect.
073: * @param sqlString the SQL string to look up.
074: * @return true if there are more resultsets, false if not.
075: * @exception SQLException if an error occurs.
076: */
077: public final boolean hasMoreResults(final String sqlString)
078: throws SQLException {
079: LOG.debug(">> hasMoreResults()");
080: this .resultSetIndex = factory.incrementIndex();
081: int numResultSets = factory.getNumberOfResultSets(sqlString);
082: return (resultSetIndex < (numResultSets + 1));
083: }
084:
085: /**
086: * Returns the result set at the specified index. No index increment
087: * or decrement occurs during this method call. This is useful for
088: * pulling out outparams (at negative resultSetIndex positions) that
089: * masquerade as resultsets.
090: * @param sqlString the SQL string to look up.
091: * @param pIndex the parameter index (1-based).
092: * @param sqlType the SQL Type of the parameter.
093: * @return the Object at the specified parameter index.
094: * @exception SQLException if an error occurs.
095: */
096: public final Object getOutParam(final String sqlString,
097: final int pIndex, final int sqlType) throws SQLException {
098: LOG.debug(">> getOutParam(" + sqlString + "," + pIndex + ","
099: + sqlType + ")");
100: return factory.getOutParamValueAt(sqlString, pIndex, sqlType);
101: }
102:
103: /**
104: * Returns the update count for the specified SQL string.
105: * @param sqlString the SQL string to look up.
106: * @return an updatecount as an Integer.
107: */
108: public final Integer getUpdateCount(final String sqlString) {
109: LOG.debug(">> getUpdateCount(" + sqlString + ")");
110: return new Integer(factory.getUpdateCount(sqlString));
111: }
112: }
|