package com.icentris.sql;
import java.math.BigDecimal;
import java.util.*;
import java.sql.*;
// Comment_next_line_to_compile_with_Java_1.3
import java.sql.ParameterMetaData;
import java.net.URL;
import com.icentris.util.CodeTimer;
import com.icentris.util.CodeTimerSegment;
public class PreparedStatementWrapper extends StatementWrapper implements PreparedStatement {
private PreparedStatement realStatement;
private String originalSql;
private TreeMap<String,String> params = new TreeMap<String,String>();
public PreparedStatementWrapper(java.sql.PreparedStatement realStatement, String originalSql, ConnectionWrapper connection) {
super(realStatement, connection);
this.realStatement = realStatement;
this.originalSql = originalSql;
}
/** Used for debugging purposes to track the original sql for this statement. */
public String getOriginalSql() {
return originalSql;
}
/** @return a clone of the ordered Map of params that have been called with setString, setTimestamp, etc. */
public TreeMap getParams() {
return (TreeMap) params.clone();
}
public ResultSet executeQuery()
throws SQLException
{
checkConnection("executeQuery");
String thisToString = this.toString();
CodeTimer timer = null;
try {
if ( useCodeTimer() == false ) {
return new ResultSetWrapper( realStatement.executeQuery(), this );
} else {
timer = new CodeTimer();
timer.setCallersToIgnore( getCallersToIgnore() );
Properties props = new Properties();
// If the toString has a space, I'll assume it's the real sql.
if ( thisToString.indexOf(" ") > -1 ) {
props.setProperty("sqlQuery", thisToString);
// Otherwise, I'll use my own information that's almost as good.
} else {
props.setProperty("originalSql", originalSql);
props.setProperty("params", params.toString());
}
segment.setProperties( props );
CodeTimerSegment subSegment = timer.start( getCallerDepth() );
subSegment.setLabel("SQL:executeQuery()");
subSegment.setSaveAggregateData( false );
ResultSetWrapper results = new ResultSetWrapper( realStatement.executeQuery(), this );
results.setUniqueString( "SQL:ResultSet.next() + getString():" + segment.getUniqueString().substring(4) );
results.setUseCodeTimer( useCodeTimer() );
return results;
}
} catch (SQLException e) {
// If the toString doesn't have a space, I'll assume it's not the real sql.
if ( thisToString.indexOf(" ") == -1 ) {
thisToString = originalSql + ", params=" + params.toString();
}
// (I'm really sad I can't change the message without losing the stack
// trace, but for now I don't see any other way)
e = new SQLException("The statement:[" + thisToString + "] had the error: " + e.getMessage());
setMostRecentError(e);
throw e;
} finally {
if ( timer != null ) {
timer.stop(getCallerDepth());
}
}
}
public int executeUpdate()
throws SQLException
{
checkConnection("executeUpdate");
String thisToString = this.toString();
try {
return realStatement.executeUpdate();
} catch (SQLException e) {
// If the toString doesn't have a space, I'll assume it's not the real sql.
if ( thisToString.indexOf(" ") == -1 ) {
thisToString = originalSql + ", params=" + params.toString();
}
// (I'm really sad I can't change the message without losing the stack
// trace, but for now I don't see any other way)
e = new SQLException("The statement:[" + thisToString + "] had the error: " + e.getMessage());
setMostRecentError(e);
throw e;
}
}
public void setNull(int parameterIndex, int sqlType)
throws SQLException
{
checkConnection("setNull");
try {
params.put( String.valueOf(parameterIndex), null );
realStatement.setNull(parameterIndex, sqlType);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setBoolean(int parameterIndex, boolean x)
throws SQLException
{
checkConnection("setBoolean");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setBoolean(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setByte(int parameterIndex, byte x)
throws SQLException
{
checkConnection("setByte");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setByte(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setShort(int parameterIndex, short x)
throws SQLException
{
checkConnection("setShort");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setShort(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setInt(int parameterIndex, int x)
throws SQLException
{
checkConnection("setInt");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setInt(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setLong(int parameterIndex, long x)
throws SQLException
{
checkConnection("setLong");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setLong(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setFloat(int parameterIndex, float x)
throws SQLException
{
checkConnection("setFloat");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setFloat(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setDouble(int parameterIndex, double x)
throws SQLException
{
checkConnection("setDouble");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setDouble(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setBigDecimal(int parameterIndex, BigDecimal x)
throws SQLException
{
checkConnection("setBigDecimal");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setBigDecimal(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setString(int parameterIndex, String x)
throws SQLException
{
checkConnection("setString");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setString(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setBytes(int parameterIndex, byte x[])
throws SQLException
{
checkConnection("setBytes");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setBytes(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setDate(int parameterIndex, java.sql.Date x)
throws SQLException
{
checkConnection("setDate");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setDate(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setTime(int parameterIndex, java.sql.Time x)
throws SQLException
{
checkConnection("setTime");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setTime(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setTimestamp(int parameterIndex, java.sql.Timestamp x)
throws SQLException
{
checkConnection("setTimestamp");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setTimestamp(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length)
throws SQLException
{
checkConnection("setAsciiStream");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setAsciiStream(parameterIndex, x, length);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length)
throws SQLException
{
checkConnection("setUnicodeStream");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setUnicodeStream(parameterIndex, x, length);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length)
throws SQLException
{
checkConnection("setBinaryStream");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setBinaryStream(parameterIndex, x, length);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void clearParameters()
throws SQLException
{
checkConnection("clearParameters");
try {
realStatement.clearParameters();
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setObject(int parameterIndex, Object x, int targetSqlType, int scale)
throws SQLException
{
checkConnection("setObject");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setObject(parameterIndex, x, targetSqlType, scale);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setObject(int parameterIndex, Object x, int targetSqlType)
throws SQLException
{
checkConnection("setObject");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setObject(parameterIndex, x, targetSqlType);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setObject(int parameterIndex, Object x)
throws SQLException
{
checkConnection("setObject");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setObject(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public boolean execute()
throws SQLException
{
checkConnection("execute");
String thisToString = this.toString();
try {
return realStatement.execute();
} catch (SQLException e) {
// If the toString doesn't have a space, I'll assume it's not the real sql.
if ( thisToString.indexOf(" ") == -1 ) {
thisToString = originalSql + ", params=" + params.toString();
}
// (I'm really sad I can't change the message without losing the stack
// trace, but for now I don't see any other way)
e = new SQLException("The statement:[" + thisToString + "] had the error: " + e.getMessage());
setMostRecentError(e);
throw e;
}
}
public void addBatch()
throws SQLException
{
checkConnection("addBatch");
try {
realStatement.addBatch();
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setCharacterStream(int parameterIndex, java.io.Reader x, int length)
throws SQLException
{
checkConnection("setCharacterStream");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
setCharacterStream(parameterIndex, x, length);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setRef(int parameterIndex, Ref x)
throws SQLException
{
checkConnection("setRef");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setRef(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setBlob(int parameterIndex, Blob x)
throws SQLException
{
checkConnection("setBlob");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setBlob(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setClob(int parameterIndex, Clob x)
throws SQLException
{
checkConnection("setClob");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setClob(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setArray(int parameterIndex, Array x)
throws SQLException
{
checkConnection("setArray");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setArray(parameterIndex, x);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public ResultSetMetaData getMetaData()
throws SQLException
{
checkConnection("getMetaData");
try {
return realStatement.getMetaData();
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setDate(int parameterIndex, java.sql.Date x, Calendar cal)
throws SQLException
{
checkConnection("setDate");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setDate(parameterIndex, x, cal);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setTime(int parameterIndex, java.sql.Time x, Calendar cal)
throws SQLException
{
checkConnection("setTime");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setTime(parameterIndex, x, cal);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal)
throws SQLException
{
checkConnection("setTimestamp");
try {
params.put( String.valueOf(parameterIndex), String.valueOf(x) );
realStatement.setTimestamp(parameterIndex, x, cal);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public void setNull(int parameterIndex, int sqlType, String typeName)
throws SQLException
{
checkConnection("setNull");
try {
params.put( String.valueOf(parameterIndex), null );
realStatement.setNull(parameterIndex, sqlType, typeName);
} catch (SQLException e) {
setMostRecentError(e);
throw e;
}
}
public String toString() {
return realStatement.toString();
}
// #################### Java 1.4 methods ######################
// # These methods are required to compile using jdk 1.4 #
// # I have carefully implemented them so I can still compile #
// # and use jdk 1.3 also. #
// ############################################################
public void setURL(int parameterIndex, URL x)
throws SQLException
{
checkConnection("setURL");
Class[] argTypes = new Class[] { Integer.TYPE, URL.class };
Object[] args = new Object[] { new Integer(parameterIndex), x };
ConnectionWrapper.callJava14Method("setURL", realStatement, argTypes, args);
}
public ParameterMetaData getParameterMetaData()
throws SQLException
{
checkConnection("getParameterMetaData");
Class[] argTypes = new Class[0];
Object[] args = new Object[0];
return (ParameterMetaData) ConnectionWrapper.callJava14Method("getParameterMetaData", realStatement, argTypes, args);
}
// #################### End Java 1.4 methods ##################
}