/*-----------------------------------------------------------------------------
 *
 * Enhydra JdbcS
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright 1997-2000 Lutris Technologies Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * www.enhydra.org/license/epl.html
 *
 * Software distributed under the License is distributed on an "ASIS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * -----------------------------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------------------------
 */
package org.enhydra.jdbc.core;

//import java.io.*;
//import java.math.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Map;

/**
 * A very simple implementation of Statement. When created
 * it is supplied with another Statement to which all
 * of this class' methods delegate their work. 
 */
public abstract class CoreStatement implements Statement {
    
    protected Statement	statement;	// the Statement which does most of the work.

    public void addBatch(String s) throws SQLException {
	preInvoke();
	try {
	    statement.addBatch(s);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method addBatch

    public void cancel() throws SQLException {
	preInvoke();
	try {
	    statement.cancel();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method cancel

    public void clearBatch() throws SQLException {
	preInvoke();
	try {
	    statement.clearBatch();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method clearBatch

    public void clearWarnings() throws SQLException {
	preInvoke();
	try {
	    statement.clearWarnings();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method clearWarnings

    public void close() throws SQLException {
	if (statement != null) {
	    statement.close();
	}
    } // method close

    public boolean execute(String s) throws SQLException {
	preInvoke();
	try {
	    return statement.execute(s);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return false;
    } // method execute

    public int[] executeBatch() throws SQLException {
	preInvoke();
	try {
	    return statement.executeBatch();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return null;
    } // method executeBatch

    public ResultSet executeQuery(String s) throws SQLException {
	preInvoke();
	try {
	    return statement.executeQuery(s);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return null;
    } // method executeQuery
    
    public int executeUpdate(String s) throws SQLException {
	preInvoke();
	try {
	    return statement.executeUpdate(s);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method executeUpdate

    public Connection getConnection() throws SQLException {
	preInvoke();
	try {
	    return statement.getConnection();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return null;
    } // method getConnection

    public int getFetchDirection() throws SQLException {
	preInvoke();
	try {
	    return statement.getFetchDirection();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getFetchDirection

    public int getFetchSize() throws SQLException {
	preInvoke();
	try {
	    return statement.getFetchSize();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getFetchSize

    public int getMaxFieldSize() throws SQLException {
	 preInvoke();
	 try {
	    return statement.getMaxFieldSize();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getMaxFieldSize

    public int getMaxRows() throws SQLException {
	 preInvoke();
	 try {
	    return statement.getMaxRows();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getMaxRows

    public boolean getMoreResults() throws SQLException {
	preInvoke();
	try {
	    return statement.getMoreResults();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return false;
    } // method getMoreResults

    public int getQueryTimeout() throws SQLException {
	 preInvoke();
	 try {
	    return statement.getQueryTimeout();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getQueryTimeout

    public ResultSet getResultSet() throws SQLException {
	preInvoke();
	try {
	    return statement.getResultSet();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return null;
    } // method getResultSet

    public int getResultSetConcurrency() throws SQLException {
	preInvoke();
	try {
	    return statement.getResultSetConcurrency();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getResultSetConcurrency
	
    public int getResultSetType() throws SQLException {
	preInvoke();
	try {
	    return statement.getResultSetType();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getResultSetType

    public int getUpdateCount() throws SQLException {
	preInvoke();
	try {
	    return statement.getUpdateCount();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return 0;
    } // method getUpdateCount

    public SQLWarning getWarnings() throws SQLException {
	preInvoke();
	try {
	    return statement.getWarnings();
	} catch (SQLException e) {
	    catchInvoke(e);
	}
	return null;
    } // method getWarnings

    public void setCursorName(String name) throws SQLException {
	preInvoke();
	try {
	    statement.setCursorName(name);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setCursorName

    public void setEscapeProcessing(boolean enable) throws SQLException {
	 preInvoke();
	 try {
	    statement.setEscapeProcessing(enable);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setEscapeProcessing

    public void setFetchDirection(int direction) throws SQLException {
	preInvoke();
	try {
	    statement.setFetchDirection(direction);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setFetchDirection
    
    public void setFetchSize(int rows) throws SQLException {
	preInvoke();
	try {
	    statement.setFetchSize(rows);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setFetchSize

    public void setMaxFieldSize(int max) throws SQLException {
	preInvoke();
	try {
	    statement.setMaxFieldSize(max);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setMaxFieldSize

    public void setMaxRows(int max) throws SQLException {
	preInvoke();
	try {
	    statement.setMaxRows(max);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setMaxRows

    public void setQueryTimeout(int seconds) throws SQLException {
	preInvoke();
	try {
	    statement.setQueryTimeout(seconds);
	} catch (SQLException e) {
	    catchInvoke(e);
	}
    } // method setQueryTimeout
	
    /**
     * Methods used to do some works before and during the catch
     * clause, to prevent the pool that a connection is broken.
     */
    abstract public void preInvoke() throws SQLException;
    abstract public void catchInvoke(SQLException e) throws SQLException;

} // class CoreStatement 
