package org.apache.commons.dbcp2;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DelegatingStatement extends AbandonedTrace implements Statement {
private Statement statement;
private DelegatingConnection<?> connection;
private boolean closed = false;
public DelegatingStatement(final DelegatingConnection<?> connection, final Statement statement) {
super(connection);
this.statement = statement;
this.connection = connection;
}
public void activate() throws SQLException {
if (statement instanceof DelegatingStatement) {
((DelegatingStatement) statement).activate();
}
}
@Override
public void addBatch(final String sql) throws SQLException {
checkOpen();
try {
statement.addBatch(sql);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void cancel() throws SQLException {
checkOpen();
try {
statement.cancel();
} catch (final SQLException e) {
handleException(e);
}
}
protected void checkOpen() throws SQLException {
if (isClosed()) {
throw new SQLException(this.getClass().getName() + " with address: \"" + this.toString() + "\" is closed.");
}
}
@Override
public void clearBatch() throws SQLException {
checkOpen();
try {
statement.clearBatch();
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void clearWarnings() throws SQLException {
checkOpen();
try {
statement.clearWarnings();
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void close() throws SQLException {
if (isClosed()) {
return;
}
final List<Exception> thrownList = new ArrayList<>();
try {
if (connection != null) {
connection.removeTrace(this);
connection = null;
}
final List<AbandonedTrace> resultSetList = getTrace();
if (resultSetList != null) {
final int size = resultSetList.size();
final ResultSet[] resultSets = resultSetList.toArray(new ResultSet[size]);
for (final ResultSet resultSet : resultSets) {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e) {
if (connection != null) {
connection.handleExceptionNoThrow(e);
}
thrownList.add(e);
}
}
}
clearTrace();
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
if (connection != null) {
connection.handleExceptionNoThrow(e);
}
thrownList.add(e);
}
}
} finally {
closed = true;
statement = null;
if (!thrownList.isEmpty()) {
throw new SQLExceptionList(thrownList);
}
}
}
@Override
public void closeOnCompletion() throws SQLException {
checkOpen();
try {
Jdbc41Bridge.closeOnCompletion(statement);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public boolean execute(final String sql) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.execute(sql);
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.execute(sql, autoGeneratedKeys);
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public boolean execute(final String sql, final int columnIndexes[]) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.execute(sql, columnIndexes);
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public boolean execute(final String sql, final String columnNames[]) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.execute(sql, columnNames);
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public int[] executeBatch() throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeBatch();
} catch (final SQLException e) {
handleException(e);
throw new AssertionError();
}
}
@Override
public long[] executeLargeBatch() throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeLargeBatch();
} catch (final SQLException e) {
handleException(e);
return null;
}
}
@Override
public long executeLargeUpdate(final String sql) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeLargeUpdate(sql);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeLargeUpdate(sql, autoGeneratedKeys);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public long executeLargeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeLargeUpdate(sql, columnIndexes);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public long executeLargeUpdate(final String sql, final String[] columnNames) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeLargeUpdate(sql, columnNames);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public ResultSet executeQuery(final String sql) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return DelegatingResultSet.wrapResultSet(this, statement.executeQuery(sql));
} catch (final SQLException e) {
handleException(e);
throw new AssertionError();
}
}
@Override
public int executeUpdate(final String sql) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeUpdate(sql);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeUpdate(sql, autoGeneratedKeys);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int executeUpdate(final String sql, final int columnIndexes[]) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeUpdate(sql, columnIndexes);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int executeUpdate(final String sql, final String columnNames[]) throws SQLException {
checkOpen();
setLastUsedInParent();
try {
return statement.executeUpdate(sql, columnNames);
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
protected void finalize() throws Throwable {
close();
super.finalize();
}
@Override
public Connection getConnection() throws SQLException {
checkOpen();
return getConnectionInternal();
}
protected DelegatingConnection<?> getConnectionInternal() {
return connection;
}
public Statement getDelegate() {
return statement;
}
@Override
public int getFetchDirection() throws SQLException {
checkOpen();
try {
return statement.getFetchDirection();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int getFetchSize() throws SQLException {
checkOpen();
try {
return statement.getFetchSize();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
checkOpen();
try {
return DelegatingResultSet.wrapResultSet(this, statement.getGeneratedKeys());
} catch (final SQLException e) {
handleException(e);
throw new AssertionError();
}
}
@SuppressWarnings("resource")
public Statement getInnermostDelegate() {
Statement s = statement;
while (s != null && s instanceof DelegatingStatement) {
s = ((DelegatingStatement) s).getDelegate();
if (this == s) {
return null;
}
}
return s;
}
@Override
public long getLargeMaxRows() throws SQLException {
checkOpen();
try {
return statement.getLargeMaxRows();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public long getLargeUpdateCount() throws SQLException {
checkOpen();
try {
return statement.getLargeUpdateCount();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int getMaxFieldSize() throws SQLException {
checkOpen();
try {
return statement.getMaxFieldSize();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int getMaxRows() throws SQLException {
checkOpen();
try {
return statement.getMaxRows();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public boolean getMoreResults() throws SQLException {
checkOpen();
try {
return statement.getMoreResults();
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public boolean getMoreResults(final int current) throws SQLException {
checkOpen();
try {
return statement.getMoreResults(current);
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public int getQueryTimeout() throws SQLException {
checkOpen();
try {
return statement.getQueryTimeout();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public ResultSet getResultSet() throws SQLException {
checkOpen();
try {
return DelegatingResultSet.wrapResultSet(this, statement.getResultSet());
} catch (final SQLException e) {
handleException(e);
throw new AssertionError();
}
}
@Override
public int getResultSetConcurrency() throws SQLException {
checkOpen();
try {
return statement.getResultSetConcurrency();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int getResultSetHoldability() throws SQLException {
checkOpen();
try {
return statement.getResultSetHoldability();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int getResultSetType() throws SQLException {
checkOpen();
try {
return statement.getResultSetType();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public int getUpdateCount() throws SQLException {
checkOpen();
try {
return statement.getUpdateCount();
} catch (final SQLException e) {
handleException(e);
return 0;
}
}
@Override
public SQLWarning getWarnings() throws SQLException {
checkOpen();
try {
return statement.getWarnings();
} catch (final SQLException e) {
handleException(e);
throw new AssertionError();
}
}
protected void handleException(final SQLException e) throws SQLException {
if (connection != null) {
connection.handleException(e);
} else {
throw e;
}
}
@Override
public boolean isClosed() throws SQLException {
return closed;
}
protected boolean isClosedInternal() {
return closed;
}
@Override
public boolean isCloseOnCompletion() throws SQLException {
checkOpen();
try {
return Jdbc41Bridge.isCloseOnCompletion(statement);
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public boolean isPoolable() throws SQLException {
checkOpen();
try {
return statement.isPoolable();
} catch (final SQLException e) {
handleException(e);
return false;
}
}
@Override
public boolean isWrapperFor(final Class<?> iface) throws SQLException {
if (iface.isAssignableFrom(getClass())) {
return true;
} else if (iface.isAssignableFrom(statement.getClass())) {
return true;
} else {
return statement.isWrapperFor(iface);
}
}
public void passivate() throws SQLException {
if (statement instanceof DelegatingStatement) {
((DelegatingStatement) statement).passivate();
}
}
protected void setClosedInternal(final boolean closed) {
this.closed = closed;
}
@Override
public void setCursorName(final String name) throws SQLException {
checkOpen();
try {
statement.setCursorName(name);
} catch (final SQLException e) {
handleException(e);
}
}
public void setDelegate(final Statement statement) {
this.statement = statement;
}
@Override
public void setEscapeProcessing(final boolean enable) throws SQLException {
checkOpen();
try {
statement.setEscapeProcessing(enable);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void setFetchDirection(final int direction) throws SQLException {
checkOpen();
try {
statement.setFetchDirection(direction);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void setFetchSize(final int rows) throws SQLException {
checkOpen();
try {
statement.setFetchSize(rows);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void setLargeMaxRows(final long max) throws SQLException {
checkOpen();
try {
statement.setLargeMaxRows(max);
} catch (final SQLException e) {
handleException(e);
}
}
private void setLastUsedInParent() {
if (connection != null) {
connection.setLastUsed();
}
}
@Override
public void setMaxFieldSize(final int max) throws SQLException {
checkOpen();
try {
statement.setMaxFieldSize(max);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void setMaxRows(final int max) throws SQLException {
checkOpen();
try {
statement.setMaxRows(max);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void setPoolable(final boolean poolable) throws SQLException {
checkOpen();
try {
statement.setPoolable(poolable);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public void setQueryTimeout(final int seconds) throws SQLException {
checkOpen();
try {
statement.setQueryTimeout(seconds);
} catch (final SQLException e) {
handleException(e);
}
}
@Override
public synchronized String toString() {
return statement == null ? "NULL" : statement.toString();
}
@Override
public <T> T unwrap(final Class<T> iface) throws SQLException {
if (iface.isAssignableFrom(getClass())) {
return iface.cast(this);
} else if (iface.isAssignableFrom(statement.getClass())) {
return iface.cast(statement);
} else {
return statement.unwrap(iface);
}
}
}