/*-----------------------------------------------------------------------------
 *
 * 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.standard;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.ConnectionEvent;
import org.enhydra.jdbc.core.CorePreparedStatement;

/**
 * A very simple implementation of PreparedStatement. When created
 * it is supplied with another PreparedStatement to which nearly all
 * of this class' methods delegate their work. 
 *
 * Close() is overridden to prevent the statement from actually
 * being closed.
 */
public class StandardPreparedStatement extends CorePreparedStatement {
    
    public Object key;		// key that StandardConnectionHandle uses to return to cache
    private StandardConnectionHandle con; // the StandardConnectionHandle that created this object
    public boolean closed;	// true when the PreparedStatement has been closed

    /**
     * Constructor.
     */
    StandardPreparedStatement(StandardConnectionHandle con, PreparedStatement preparedStatement, Object key) {
	this.con = con;
	this.key = key;
	ps = preparedStatement;
    } // constructor

    StandardPreparedStatement() {
	super();
    } // constructor

    /**
     * Close this statement.
     */
    public void close() throws SQLException {
	// Note no check for already closed - some servers make mistakes
	closed = true;
	if (con.preparedStmtCacheSize == 0) {
	    // no cache, so we just close
	    if (ps != null) {
		ps.close();
	    }
	} else {
	    con.returnToCache(key);		// return the underlying statement to the cache
	}
    } // method close
     
    /** 
     * Pre-invokation of the delegation, in case of the Statement is
     * closed, we throw an exception
     */
    public void preInvoke() throws SQLException {
	if (closed)
	    throw new SQLException("Prepare Statement is closed");
    } // method preInvoke

    /**
     * Exception management : catch or throw the exception
     */
    public void catchInvoke(SQLException sqlException) throws SQLException {
	//ConnectionEvent event = new ConnectionEvent(con.pooledCon);
	//con.pooledCon.connectionErrorOccurred(event);
	throw(sqlException);
    } // method catchInvoke

} // class StandardPreparedStatement
