1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.dbcp;
  17. import java.math.BigDecimal;
  18. import java.sql.Array;
  19. import java.sql.Blob;
  20. import java.sql.Clob;
  21. import java.sql.PreparedStatement;
  22. import java.sql.Ref;
  23. import java.sql.ResultSet;
  24. import java.sql.ResultSetMetaData;
  25. import java.sql.SQLException;
  26. import java.util.Calendar;
  27. /**
  28. * A base delegating implementation of {@link PreparedStatement}.
  29. * <p>
  30. * All of the methods from the {@link PreparedStatement} interface
  31. * simply check to see that the {@link PreparedStatement} is active,
  32. * and call the corresponding method on the "delegate"
  33. * provided in my constructor.
  34. * <p>
  35. * Extends AbandonedTrace to implement Statement tracking and
  36. * logging of code which created the Statement. Tracking the
  37. * Statement ensures that the Connection which created it can
  38. * close any open Statement's on Connection close.
  39. *
  40. * @author Rodney Waldhoff
  41. * @author Glenn L. Nielsen
  42. * @author James House
  43. * @author Dirk Verbeeck
  44. * @version $Revision: 1.22 $ $Date: 2004/03/06 13:35:31 $
  45. */
  46. public class DelegatingPreparedStatement extends DelegatingStatement
  47. implements PreparedStatement {
  48. /** My delegate. */
  49. protected PreparedStatement _stmt = null;
  50. /**
  51. * Create a wrapper for the Statement which traces this
  52. * Statement to the Connection which created it and the
  53. * code which created it.
  54. *
  55. * @param s the {@link PreparedStatement} to delegate all calls to.
  56. * @param c the {@link DelegatingConnection} that created this statement.
  57. */
  58. public DelegatingPreparedStatement(DelegatingConnection c,
  59. PreparedStatement s) {
  60. super(c, s);
  61. _stmt = s;
  62. }
  63. public boolean equals(Object obj) {
  64. PreparedStatement delegate = (PreparedStatement) getInnermostDelegate();
  65. if (delegate == null) {
  66. return false;
  67. }
  68. if (obj instanceof DelegatingPreparedStatement) {
  69. DelegatingPreparedStatement s = (DelegatingPreparedStatement) obj;
  70. return delegate.equals(s.getInnermostDelegate());
  71. }
  72. else {
  73. return delegate.equals(obj);
  74. }
  75. }
  76. /** Sets my delegate. */
  77. public void setDelegate(PreparedStatement s) {
  78. super.setDelegate(s);
  79. _stmt = s;
  80. }
  81. public ResultSet executeQuery() throws SQLException {
  82. checkOpen();
  83. try {
  84. return DelegatingResultSet.wrapResultSet(this,_stmt.executeQuery());
  85. }
  86. catch (SQLException e) {
  87. handleException(e);
  88. return null;
  89. }
  90. }
  91. public int executeUpdate() throws SQLException
  92. { checkOpen(); try { return _stmt.executeUpdate(); } catch (SQLException e) { handleException(e); return 0; } }
  93. public void setNull(int parameterIndex, int sqlType) throws SQLException
  94. { checkOpen(); try { _stmt.setNull(parameterIndex,sqlType); } catch (SQLException e) { handleException(e); } }
  95. public void setBoolean(int parameterIndex, boolean x) throws SQLException
  96. { checkOpen(); try { _stmt.setBoolean(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  97. public void setByte(int parameterIndex, byte x) throws SQLException
  98. { checkOpen(); try { _stmt.setByte(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  99. public void setShort(int parameterIndex, short x) throws SQLException
  100. { checkOpen(); try { _stmt.setShort(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  101. public void setInt(int parameterIndex, int x) throws SQLException
  102. { checkOpen(); try { _stmt.setInt(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  103. public void setLong(int parameterIndex, long x) throws SQLException
  104. { checkOpen(); try { _stmt.setLong(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  105. public void setFloat(int parameterIndex, float x) throws SQLException
  106. { checkOpen(); try { _stmt.setFloat(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  107. public void setDouble(int parameterIndex, double x) throws SQLException
  108. { checkOpen(); try { _stmt.setDouble(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  109. public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
  110. { checkOpen(); try { _stmt.setBigDecimal(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  111. public void setString(int parameterIndex, String x) throws SQLException
  112. { checkOpen(); try { _stmt.setString(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  113. public void setBytes(int parameterIndex, byte[] x) throws SQLException
  114. { checkOpen(); try { _stmt.setBytes(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  115. public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
  116. { checkOpen(); try { _stmt.setDate(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  117. public void setTime(int parameterIndex, java.sql.Time x) throws SQLException
  118. { checkOpen(); try { _stmt.setTime(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  119. public void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException
  120. { checkOpen(); try { _stmt.setTimestamp(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
  121. public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
  122. { checkOpen(); try { _stmt.setAsciiStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
  123. /** @deprecated */
  124. public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
  125. { checkOpen(); try { _stmt.setUnicodeStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
  126. public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
  127. { checkOpen(); try { _stmt.setBinaryStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
  128. public void clearParameters() throws SQLException
  129. { checkOpen(); try { _stmt.clearParameters(); } catch (SQLException e) { handleException(e); } }
  130. public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
  131. { checkOpen(); try { _stmt.setObject(parameterIndex, x, targetSqlType, scale); } catch (SQLException e) { handleException(e); } }
  132. public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException
  133. { checkOpen(); try { _stmt.setObject(parameterIndex, x, targetSqlType); } catch (SQLException e) { handleException(e); } }
  134. public void setObject(int parameterIndex, Object x) throws SQLException
  135. { checkOpen(); try { _stmt.setObject(parameterIndex, x); } catch (SQLException e) { handleException(e); } }
  136. public boolean execute() throws SQLException
  137. { checkOpen(); try { return _stmt.execute(); } catch (SQLException e) { handleException(e); return false; } }
  138. public void addBatch() throws SQLException
  139. { checkOpen(); try { _stmt.addBatch(); } catch (SQLException e) { handleException(e); } }
  140. public void setCharacterStream(int parameterIndex, java.io.Reader reader, int length) throws SQLException
  141. { checkOpen(); try { _stmt.setCharacterStream(parameterIndex,reader,length); } catch (SQLException e) { handleException(e); } }
  142. public void setRef(int i, Ref x) throws SQLException
  143. { checkOpen(); try { _stmt.setRef(i,x); } catch (SQLException e) { handleException(e); } }
  144. public void setBlob(int i, Blob x) throws SQLException
  145. { checkOpen(); try { _stmt.setBlob(i,x); } catch (SQLException e) { handleException(e); } }
  146. public void setClob(int i, Clob x) throws SQLException
  147. { checkOpen(); try { _stmt.setClob(i,x); } catch (SQLException e) { handleException(e); } }
  148. public void setArray(int i, Array x) throws SQLException
  149. { checkOpen(); try { _stmt.setArray(i,x); } catch (SQLException e) { handleException(e); } }
  150. public ResultSetMetaData getMetaData() throws SQLException
  151. { checkOpen(); try { return _stmt.getMetaData(); } catch (SQLException e) { handleException(e); return null; } }
  152. public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException
  153. { checkOpen(); try { _stmt.setDate(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
  154. public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException
  155. { checkOpen(); try { _stmt.setTime(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
  156. public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException
  157. { checkOpen(); try { _stmt.setTimestamp(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
  158. public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException
  159. { checkOpen(); try { _stmt.setNull(paramIndex,sqlType,typeName); } catch (SQLException e) { handleException(e); } }
  160. // ------------------- JDBC 3.0 -----------------------------------------
  161. // Will be commented by the build process on a JDBC 2.0 system
  162. /* JDBC_3_ANT_KEY_BEGIN */
  163. public void setURL(int parameterIndex, java.net.URL x) throws SQLException
  164. { checkOpen(); try { _stmt.setURL(parameterIndex, x); } catch (SQLException e) { handleException(e); } }
  165. public java.sql.ParameterMetaData getParameterMetaData() throws SQLException
  166. { checkOpen(); try { return _stmt.getParameterMetaData(); } catch (SQLException e) { handleException(e); return null; } }
  167. /* JDBC_3_ANT_KEY_END */
  168. }