/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.resource.adapter.jdbc;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

/**
 * A wrapper for a prepared statement.
 *
 * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
 * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.4.6.2 $
 */
public class WrappedPreparedStatement extends WrappedStatement implements PreparedStatement 
{
   private final PreparedStatement ps;

   public WrappedPreparedStatement(final WrappedConnection lc, final PreparedStatement ps) 
   {
      super(lc, ps);
      this.ps = ps;
   }

   public Statement getUnderlyingStatement()
   {
      if (ps instanceof CachedPreparedStatement)
      {
         return ((CachedPreparedStatement)ps).getUnderlyingPreparedStatement();
      }
      else
      {
         return ps;
      }
   }

   public void setBoolean(int parameterIndex, boolean value) throws SQLException
   {
      try 
      {
         ps.setBoolean(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setByte(int parameterIndex, byte value) throws SQLException
   {
      try 
      {
         ps.setByte(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setShort(int parameterIndex, short value) throws SQLException
   {
      try 
      {
         ps.setShort(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setInt(int parameterIndex, int value) throws SQLException
   {
      try 
      {
         ps.setInt(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setLong(int parameterIndex, long value) throws SQLException
   {
      try 
      {
         ps.setLong(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setFloat(int parameterIndex, float value) throws SQLException
   {
      try 
      {
         ps.setFloat(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setDouble(int parameterIndex, double value) throws SQLException
   {
      try 
      {
         ps.setDouble(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setURL(int parameterIndex, URL value) throws SQLException
   {
      try 
      {
         ps.setURL(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setTime(int parameterIndex, Time value) throws SQLException
   {
      try 
      {
         ps.setTime(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setTime(int parameterIndex, Time value, Calendar calendar) throws SQLException
   {
      try 
      {
         ps.setTime(parameterIndex, value, calendar);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public boolean execute() throws SQLException
   {
      checkTransaction();
      try 
      {
         checkConfiguredQueryTimeout();
         return ps.execute();         
      }
      catch (SQLException e)
      {
         checkException(e);
         return false;
      }
   }

   public ResultSetMetaData getMetaData() throws SQLException
   {
      try 
      {
         return ps.getMetaData();         
      }
      catch (SQLException e)
      {
         checkException(e);
         return null;
      }
   }

   public ResultSet executeQuery() throws SQLException
   {
      checkTransaction();
      try 
      {
         checkConfiguredQueryTimeout();
         ResultSet resultSet = ps.executeQuery();
         return registerResultSet(resultSet);
      }
      catch (SQLException e)
      {
         checkException(e);
         return null;
      }
   }

   public int executeUpdate() throws SQLException
   {
      checkTransaction();
      try 
      {
         checkConfiguredQueryTimeout();
         return ps.executeUpdate();         
      }
      catch (SQLException e)
      {
         checkException(e);
         return 0;
      }
   }

   public void addBatch() throws SQLException
   {
      try 
      {
         ps.addBatch();         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setNull(int parameterIndex, int sqlType) throws SQLException
   {
      try 
      {
         ps.setNull(parameterIndex, sqlType);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException
   {
      try 
      {
         ps.setNull(parameterIndex, sqlType, typeName);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setBigDecimal(int parameterIndex, BigDecimal value) throws SQLException
   {
      try 
      {
         ps.setBigDecimal(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setString(int parameterIndex, String value) throws SQLException
   {
      try 
      {
         ps.setString(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setBytes(int parameterIndex, byte[] value) throws SQLException
   {
      try 
      {
         ps.setBytes(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setDate(int parameterIndex, Date value) throws SQLException
   {
      try 
      {
         ps.setDate(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setDate(int parameterIndex, Date value, Calendar calendar) throws SQLException
   {
      try 
      {
         ps.setDate(parameterIndex, value, calendar);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setTimestamp(int parameterIndex, Timestamp value) throws SQLException
   {
      try 
      {
         ps.setTimestamp(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setTimestamp(int parameterIndex, Timestamp value, Calendar calendar) throws SQLException
   {
      try 
      {
         ps.setTimestamp(parameterIndex, value, calendar);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setAsciiStream(int parameterIndex, InputStream stream, int length) throws SQLException
   {
      try 
      {
         ps.setAsciiStream(parameterIndex, stream, length);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setUnicodeStream(int parameterIndex, InputStream stream, int length) throws SQLException
   {
      try 
      {
         ps.setUnicodeStream(parameterIndex, stream, length);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setBinaryStream(int parameterIndex, InputStream stream, int length) throws SQLException
   {
      try 
      {
         ps.setBinaryStream(parameterIndex, stream, length);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void clearParameters() throws SQLException
   {
      try 
      {
         ps.clearParameters();         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setObject(int parameterIndex, Object value, int sqlType, int scale) throws SQLException
   {
      try 
      {
         ps.setObject(parameterIndex, value, sqlType, scale);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setObject(int parameterIndex, Object value, int sqlType) throws SQLException
   {
      try 
      {
         ps.setObject(parameterIndex, value, sqlType);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setObject(int parameterIndex, Object value) throws SQLException
   {
      try 
      {
         ps.setObject(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException
   {
      try 
      {
         ps.setCharacterStream(parameterIndex, reader, length);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setRef(int parameterIndex, Ref value) throws SQLException
   {
      try 
      {
         ps.setRef(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setBlob(int parameterIndex, Blob value) throws SQLException
   {
      try 
      {
         ps.setBlob(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setClob(int parameterIndex, Clob value) throws SQLException
   {
      try 
      {
         ps.setClob(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public void setArray(int parameterIndex, Array value) throws SQLException
   {
      try 
      {
         ps.setArray(parameterIndex, value);         
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   }

   public ParameterMetaData getParameterMetaData() throws SQLException
   {
      try 
      {
         return ps.getParameterMetaData();         
      }
      catch (SQLException e)
      {
         checkException(e);
         return null;
      }
   }
}
