DelegatingPreparedStatement.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbcp2;

  18. import java.io.InputStream;
  19. import java.io.Reader;
  20. import java.math.BigDecimal;
  21. import java.sql.Array;
  22. import java.sql.Blob;
  23. import java.sql.Clob;
  24. import java.sql.Date;
  25. import java.sql.NClob;
  26. import java.sql.PreparedStatement;
  27. import java.sql.Ref;
  28. import java.sql.ResultSet;
  29. import java.sql.ResultSetMetaData;
  30. import java.sql.RowId;
  31. import java.sql.SQLException;
  32. import java.sql.SQLType;
  33. import java.sql.SQLXML;
  34. import java.sql.Time;
  35. import java.sql.Timestamp;
  36. import java.util.ArrayList;
  37. import java.util.Calendar;
  38. import java.util.List;
  39. import java.util.Objects;

  40. /**
  41.  * A base delegating implementation of {@link PreparedStatement}.
  42.  * <p>
  43.  * All of the methods from the {@link PreparedStatement} interface simply check to see that the
  44.  * {@link PreparedStatement} is active, and call the corresponding method on the "delegate" provided in my constructor.
  45.  * <p>
  46.  * Extends AbandonedTrace to implement Statement tracking and logging of code which created the Statement. Tracking the
  47.  * Statement ensures that the Connection which created it can close any open Statement's on Connection close.
  48.  *
  49.  * @since 2.0
  50.  */
  51. public class DelegatingPreparedStatement extends DelegatingStatement implements PreparedStatement {

  52.     /**
  53.      * Create a wrapper for the Statement which traces this Statement to the Connection which created it and the code
  54.      * which created it.
  55.      *
  56.      * @param statement
  57.      *            the {@link PreparedStatement} to delegate all calls to.
  58.      * @param connection
  59.      *            the {@link DelegatingConnection} that created this statement.
  60.      */
  61.     public DelegatingPreparedStatement(final DelegatingConnection<?> connection, final PreparedStatement statement) {
  62.         super(connection, statement);
  63.     }

  64.     @Override
  65.     public void addBatch() throws SQLException {
  66.         checkOpen();
  67.         try {
  68.             getDelegatePreparedStatement().addBatch();
  69.         } catch (final SQLException e) {
  70.             handleException(e);
  71.         }
  72.     }

  73.     @Override
  74.     public void clearParameters() throws SQLException {
  75.         checkOpen();
  76.         try {
  77.             getDelegatePreparedStatement().clearParameters();
  78.         } catch (final SQLException e) {
  79.             handleException(e);
  80.         }
  81.     }

  82.     @Override
  83.     public boolean execute() throws SQLException {
  84.         checkOpen();
  85.         if (getConnectionInternal() != null) {
  86.             getConnectionInternal().setLastUsed();
  87.         }
  88.         try {
  89.             return getDelegatePreparedStatement().execute();
  90.         } catch (final SQLException e) {
  91.             handleException(e);
  92.             return false;
  93.         }
  94.     }

  95.     /**
  96.      * @since 2.5.0
  97.      */
  98.     @Override
  99.     public long executeLargeUpdate() throws SQLException {
  100.         checkOpen();
  101.         try {
  102.             return getDelegatePreparedStatement().executeLargeUpdate();
  103.         } catch (final SQLException e) {
  104.             handleException(e);
  105.             return 0;
  106.         }
  107.     }

  108.     @Override
  109.     public ResultSet executeQuery() throws SQLException {
  110.         checkOpen();
  111.         if (getConnectionInternal() != null) {
  112.             getConnectionInternal().setLastUsed();
  113.         }
  114.         try {
  115.             return DelegatingResultSet.wrapResultSet(this, getDelegatePreparedStatement().executeQuery());
  116.         } catch (final SQLException e) {
  117.             handleException(e);
  118.             throw new AssertionError();
  119.         }
  120.     }

  121.     @Override
  122.     public int executeUpdate() throws SQLException {
  123.         checkOpen();
  124.         if (getConnectionInternal() != null) {
  125.             getConnectionInternal().setLastUsed();
  126.         }
  127.         try {
  128.             return getDelegatePreparedStatement().executeUpdate();
  129.         } catch (final SQLException e) {
  130.             handleException(e);
  131.             return 0;
  132.         }
  133.     }

  134.     private PreparedStatement getDelegatePreparedStatement() {
  135.         return (PreparedStatement) getDelegate();
  136.     }

  137.     @Override
  138.     public ResultSetMetaData getMetaData() throws SQLException {
  139.         checkOpen();
  140.         try {
  141.             return getDelegatePreparedStatement().getMetaData();
  142.         } catch (final SQLException e) {
  143.             handleException(e);
  144.             throw new AssertionError();
  145.         }
  146.     }

  147.     @Override
  148.     public java.sql.ParameterMetaData getParameterMetaData() throws SQLException {
  149.         checkOpen();
  150.         try {
  151.             return getDelegatePreparedStatement().getParameterMetaData();
  152.         } catch (final SQLException e) {
  153.             handleException(e);
  154.             throw new AssertionError();
  155.         }
  156.     }

  157.     protected void prepareToReturn() throws SQLException {
  158.         setClosedInternal(true);
  159.         removeThisTrace(getConnectionInternal());

  160.         // The JDBC spec requires that a statement close any open
  161.         // ResultSet's when it is closed.
  162.         // FIXME The PreparedStatement we're wrapping should handle this for us.
  163.         // See DBCP-10 for what could happen when ResultSets are closed twice.
  164.         final List<AbandonedTrace> traceList = getTrace();
  165.         if (traceList != null) {
  166.             final List<Exception> thrownList = new ArrayList<>();
  167.             traceList.forEach(trace -> trace.close(thrownList::add));
  168.             clearTrace();
  169.             if (!thrownList.isEmpty()) {
  170.                 throw new SQLExceptionList(thrownList);
  171.             }
  172.         }

  173.         super.passivate();
  174.     }

  175.     @Override
  176.     public void setArray(final int i, final Array x) throws SQLException {
  177.         checkOpen();
  178.         try {
  179.             getDelegatePreparedStatement().setArray(i, x);
  180.         } catch (final SQLException e) {
  181.             handleException(e);
  182.         }
  183.     }

  184.     @Override
  185.     public void setAsciiStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
  186.         checkOpen();
  187.         try {
  188.             getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream);
  189.         } catch (final SQLException e) {
  190.             handleException(e);
  191.         }
  192.     }

  193.     @Override
  194.     public void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
  195.         checkOpen();
  196.         try {
  197.             getDelegatePreparedStatement().setAsciiStream(parameterIndex, x, length);
  198.         } catch (final SQLException e) {
  199.             handleException(e);
  200.         }
  201.     }

  202.     @Override
  203.     public void setAsciiStream(final int parameterIndex, final InputStream inputStream, final long length)
  204.             throws SQLException {
  205.         checkOpen();
  206.         try {
  207.             getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream, length);
  208.         } catch (final SQLException e) {
  209.             handleException(e);
  210.         }
  211.     }

  212.     @Override
  213.     public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException {
  214.         checkOpen();
  215.         try {
  216.             getDelegatePreparedStatement().setBigDecimal(parameterIndex, x);
  217.         } catch (final SQLException e) {
  218.             handleException(e);
  219.         }
  220.     }

  221.     @Override
  222.     public void setBinaryStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
  223.         checkOpen();
  224.         try {
  225.             getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream);
  226.         } catch (final SQLException e) {
  227.             handleException(e);
  228.         }
  229.     }

  230.     @Override
  231.     public void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
  232.         checkOpen();
  233.         try {
  234.             getDelegatePreparedStatement().setBinaryStream(parameterIndex, x, length);
  235.         } catch (final SQLException e) {
  236.             handleException(e);
  237.         }
  238.     }

  239.     @Override
  240.     public void setBinaryStream(final int parameterIndex, final InputStream inputStream, final long length)
  241.             throws SQLException {
  242.         checkOpen();
  243.         try {
  244.             getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream, length);
  245.         } catch (final SQLException e) {
  246.             handleException(e);
  247.         }
  248.     }

  249.     @Override
  250.     public void setBlob(final int i, final Blob x) throws SQLException {
  251.         checkOpen();
  252.         try {
  253.             getDelegatePreparedStatement().setBlob(i, x);
  254.         } catch (final SQLException e) {
  255.             handleException(e);
  256.         }
  257.     }

  258.     @Override
  259.     public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException {
  260.         checkOpen();
  261.         try {
  262.             getDelegatePreparedStatement().setBlob(parameterIndex, inputStream);
  263.         } catch (final SQLException e) {
  264.             handleException(e);
  265.         }
  266.     }

  267.     @Override
  268.     public void setBlob(final int parameterIndex, final InputStream inputStream, final long length)
  269.             throws SQLException {
  270.         checkOpen();
  271.         try {
  272.             getDelegatePreparedStatement().setBlob(parameterIndex, inputStream, length);
  273.         } catch (final SQLException e) {
  274.             handleException(e);
  275.         }
  276.     }

  277.     @Override
  278.     public void setBoolean(final int parameterIndex, final boolean x) throws SQLException {
  279.         checkOpen();
  280.         try {
  281.             getDelegatePreparedStatement().setBoolean(parameterIndex, x);
  282.         } catch (final SQLException e) {
  283.             handleException(e);
  284.         }
  285.     }

  286.     @Override
  287.     public void setByte(final int parameterIndex, final byte x) throws SQLException {
  288.         checkOpen();
  289.         try {
  290.             getDelegatePreparedStatement().setByte(parameterIndex, x);
  291.         } catch (final SQLException e) {
  292.             handleException(e);
  293.         }
  294.     }

  295.     @Override
  296.     public void setBytes(final int parameterIndex, final byte[] x) throws SQLException {
  297.         checkOpen();
  298.         try {
  299.             getDelegatePreparedStatement().setBytes(parameterIndex, x);
  300.         } catch (final SQLException e) {
  301.             handleException(e);
  302.         }
  303.     }

  304.     @Override
  305.     public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
  306.         checkOpen();
  307.         try {
  308.             getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader);
  309.         } catch (final SQLException e) {
  310.             handleException(e);
  311.         }
  312.     }

  313.     @Override
  314.     public void setCharacterStream(final int parameterIndex, final Reader reader, final int length)
  315.             throws SQLException {
  316.         checkOpen();
  317.         try {
  318.             getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length);
  319.         } catch (final SQLException e) {
  320.             handleException(e);
  321.         }
  322.     }

  323.     @Override
  324.     public void setCharacterStream(final int parameterIndex, final Reader reader, final long length)
  325.             throws SQLException {
  326.         checkOpen();
  327.         try {
  328.             getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length);
  329.         } catch (final SQLException e) {
  330.             handleException(e);
  331.         }
  332.     }

  333.     @Override
  334.     public void setClob(final int i, final Clob x) throws SQLException {
  335.         checkOpen();
  336.         try {
  337.             getDelegatePreparedStatement().setClob(i, x);
  338.         } catch (final SQLException e) {
  339.             handleException(e);
  340.         }
  341.     }

  342.     @Override
  343.     public void setClob(final int parameterIndex, final Reader reader) throws SQLException {
  344.         checkOpen();
  345.         try {
  346.             getDelegatePreparedStatement().setClob(parameterIndex, reader);
  347.         } catch (final SQLException e) {
  348.             handleException(e);
  349.         }
  350.     }

  351.     @Override
  352.     public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
  353.         checkOpen();
  354.         try {
  355.             getDelegatePreparedStatement().setClob(parameterIndex, reader, length);
  356.         } catch (final SQLException e) {
  357.             handleException(e);
  358.         }
  359.     }

  360.     @Override
  361.     public void setDate(final int parameterIndex, final Date x) throws SQLException {
  362.         checkOpen();
  363.         try {
  364.             getDelegatePreparedStatement().setDate(parameterIndex, x);
  365.         } catch (final SQLException e) {
  366.             handleException(e);
  367.         }
  368.     }

  369.     @Override
  370.     public void setDate(final int parameterIndex, final Date x, final Calendar cal) throws SQLException {
  371.         checkOpen();
  372.         try {
  373.             getDelegatePreparedStatement().setDate(parameterIndex, x, cal);
  374.         } catch (final SQLException e) {
  375.             handleException(e);
  376.         }
  377.     }

  378.     @Override
  379.     public void setDouble(final int parameterIndex, final double x) throws SQLException {
  380.         checkOpen();
  381.         try {
  382.             getDelegatePreparedStatement().setDouble(parameterIndex, x);
  383.         } catch (final SQLException e) {
  384.             handleException(e);
  385.         }
  386.     }

  387.     @Override
  388.     public void setFloat(final int parameterIndex, final float x) throws SQLException {
  389.         checkOpen();
  390.         try {
  391.             getDelegatePreparedStatement().setFloat(parameterIndex, x);
  392.         } catch (final SQLException e) {
  393.             handleException(e);
  394.         }
  395.     }

  396.     @Override
  397.     public void setInt(final int parameterIndex, final int x) throws SQLException {
  398.         checkOpen();
  399.         try {
  400.             getDelegatePreparedStatement().setInt(parameterIndex, x);
  401.         } catch (final SQLException e) {
  402.             handleException(e);
  403.         }
  404.     }

  405.     @Override
  406.     public void setLong(final int parameterIndex, final long x) throws SQLException {
  407.         checkOpen();
  408.         try {
  409.             getDelegatePreparedStatement().setLong(parameterIndex, x);
  410.         } catch (final SQLException e) {
  411.             handleException(e);
  412.         }
  413.     }

  414.     @Override
  415.     public void setNCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
  416.         checkOpen();
  417.         try {
  418.             getDelegatePreparedStatement().setNCharacterStream(parameterIndex, reader);
  419.         } catch (final SQLException e) {
  420.             handleException(e);
  421.         }
  422.     }

  423.     @Override
  424.     public void setNCharacterStream(final int parameterIndex, final Reader value, final long length)
  425.             throws SQLException {
  426.         checkOpen();
  427.         try {
  428.             getDelegatePreparedStatement().setNCharacterStream(parameterIndex, value, length);
  429.         } catch (final SQLException e) {
  430.             handleException(e);
  431.         }
  432.     }

  433.     @Override
  434.     public void setNClob(final int parameterIndex, final NClob value) throws SQLException {
  435.         checkOpen();
  436.         try {
  437.             getDelegatePreparedStatement().setNClob(parameterIndex, value);
  438.         } catch (final SQLException e) {
  439.             handleException(e);
  440.         }
  441.     }

  442.     @Override
  443.     public void setNClob(final int parameterIndex, final Reader reader) throws SQLException {
  444.         checkOpen();
  445.         try {
  446.             getDelegatePreparedStatement().setNClob(parameterIndex, reader);
  447.         } catch (final SQLException e) {
  448.             handleException(e);
  449.         }
  450.     }

  451.     @Override
  452.     public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
  453.         checkOpen();
  454.         try {
  455.             getDelegatePreparedStatement().setNClob(parameterIndex, reader, length);
  456.         } catch (final SQLException e) {
  457.             handleException(e);
  458.         }
  459.     }

  460.     @Override
  461.     public void setNString(final int parameterIndex, final String value) throws SQLException {
  462.         checkOpen();
  463.         try {
  464.             getDelegatePreparedStatement().setNString(parameterIndex, value);
  465.         } catch (final SQLException e) {
  466.             handleException(e);
  467.         }
  468.     }

  469.     @Override
  470.     public void setNull(final int parameterIndex, final int sqlType) throws SQLException {
  471.         checkOpen();
  472.         try {
  473.             getDelegatePreparedStatement().setNull(parameterIndex, sqlType);
  474.         } catch (final SQLException e) {
  475.             handleException(e);
  476.         }
  477.     }

  478.     @Override
  479.     public void setNull(final int paramIndex, final int sqlType, final String typeName) throws SQLException {
  480.         checkOpen();
  481.         try {
  482.             getDelegatePreparedStatement().setNull(paramIndex, sqlType, typeName);
  483.         } catch (final SQLException e) {
  484.             handleException(e);
  485.         }
  486.     }

  487.     @Override
  488.     public void setObject(final int parameterIndex, final Object x) throws SQLException {
  489.         checkOpen();
  490.         try {
  491.             getDelegatePreparedStatement().setObject(parameterIndex, x);
  492.         } catch (final SQLException e) {
  493.             handleException(e);
  494.         }
  495.     }

  496.     @Override
  497.     public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException {
  498.         checkOpen();
  499.         try {
  500.             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType);
  501.         } catch (final SQLException e) {
  502.             handleException(e);
  503.         }
  504.     }

  505.     @Override
  506.     public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scale)
  507.             throws SQLException {
  508.         checkOpen();
  509.         try {
  510.             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scale);
  511.         } catch (final SQLException e) {
  512.             handleException(e);
  513.         }
  514.     }

  515.     /**
  516.      * @since 2.5.0
  517.      */
  518.     @Override
  519.     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType) throws SQLException {
  520.         checkOpen();
  521.         try {
  522.             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType);
  523.         } catch (final SQLException e) {
  524.             handleException(e);
  525.         }
  526.     }

  527.     /**
  528.      * @since 2.5.0
  529.      */
  530.     @Override
  531.     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException {
  532.         checkOpen();
  533.         try {
  534.             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scaleOrLength);
  535.         } catch (final SQLException e) {
  536.             handleException(e);
  537.         }
  538.     }

  539.     @Override
  540.     public void setRef(final int i, final Ref x) throws SQLException {
  541.         checkOpen();
  542.         try {
  543.             getDelegatePreparedStatement().setRef(i, x);
  544.         } catch (final SQLException e) {
  545.             handleException(e);
  546.         }
  547.     }

  548.     @Override
  549.     public void setRowId(final int parameterIndex, final RowId value) throws SQLException {
  550.         checkOpen();
  551.         try {
  552.             getDelegatePreparedStatement().setRowId(parameterIndex, value);
  553.         } catch (final SQLException e) {
  554.             handleException(e);
  555.         }
  556.     }

  557.     @Override
  558.     public void setShort(final int parameterIndex, final short x) throws SQLException {
  559.         checkOpen();
  560.         try {
  561.             getDelegatePreparedStatement().setShort(parameterIndex, x);
  562.         } catch (final SQLException e) {
  563.             handleException(e);
  564.         }
  565.     }

  566.     @Override
  567.     public void setSQLXML(final int parameterIndex, final SQLXML value) throws SQLException {
  568.         checkOpen();
  569.         try {
  570.             getDelegatePreparedStatement().setSQLXML(parameterIndex, value);
  571.         } catch (final SQLException e) {
  572.             handleException(e);
  573.         }
  574.     }

  575.     @Override
  576.     public void setString(final int parameterIndex, final String x) throws SQLException {
  577.         checkOpen();
  578.         try {
  579.             getDelegatePreparedStatement().setString(parameterIndex, x);
  580.         } catch (final SQLException e) {
  581.             handleException(e);
  582.         }
  583.     }

  584.     @Override
  585.     public void setTime(final int parameterIndex, final Time x) throws SQLException {
  586.         checkOpen();
  587.         try {
  588.             getDelegatePreparedStatement().setTime(parameterIndex, x);
  589.         } catch (final SQLException e) {
  590.             handleException(e);
  591.         }
  592.     }

  593.     @Override
  594.     public void setTime(final int parameterIndex, final Time x, final Calendar cal) throws SQLException {
  595.         checkOpen();
  596.         try {
  597.             getDelegatePreparedStatement().setTime(parameterIndex, x, cal);
  598.         } catch (final SQLException e) {
  599.             handleException(e);
  600.         }
  601.     }

  602.     @Override
  603.     public void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException {
  604.         checkOpen();
  605.         try {
  606.             getDelegatePreparedStatement().setTimestamp(parameterIndex, x);
  607.         } catch (final SQLException e) {
  608.             handleException(e);
  609.         }
  610.     }

  611.     @Override
  612.     public void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException {
  613.         checkOpen();
  614.         try {
  615.             getDelegatePreparedStatement().setTimestamp(parameterIndex, x, cal);
  616.         } catch (final SQLException e) {
  617.             handleException(e);
  618.         }
  619.     }

  620.     /** @deprecated Use setAsciiStream(), setCharacterStream() or setNCharacterStream() */
  621.     @Deprecated
  622.     @Override
  623.     public void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
  624.         checkOpen();
  625.         try {
  626.             getDelegatePreparedStatement().setUnicodeStream(parameterIndex, x, length);
  627.         } catch (final SQLException e) {
  628.             handleException(e);
  629.         }
  630.     }

  631.     @Override
  632.     public void setURL(final int parameterIndex, final java.net.URL x) throws SQLException {
  633.         checkOpen();
  634.         try {
  635.             getDelegatePreparedStatement().setURL(parameterIndex, x);
  636.         } catch (final SQLException e) {
  637.             handleException(e);
  638.         }
  639.     }

  640.     /**
  641.      * Returns a String representation of this object.
  642.      *
  643.      * @return String
  644.      */
  645.     @SuppressWarnings("resource")
  646.     @Override
  647.     public synchronized String toString() {
  648.         return Objects.toString(getDelegate(), "NULL");
  649.     }
  650. }