content: /*
   * Copyright (c) 2020 Adam Gaj
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.adgadev.jplusone.core.proxy.datasource;
  
  import com.adgadev.jplusone.core.proxy.StateListener;
  import lombok.RequiredArgsConstructor;
  import lombok.experimental.Delegate;
  import lombok.extern.slf4j.Slf4j;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  @Slf4j
  @RequiredArgsConstructor
  class ConnectionProxy implements Connection {
  
      @Delegate(excludes = ConnectionOverwrite.class)
      private final Connection delegate;
  
      private final StateListener stateListener;
  
      @Override
      public Statement createStatement() throws SQLException {
          Statement statement = delegate.createStatement();
          return new StatementProxy(statement, stateListener);
      }
  
      @Override
      public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
          Statement statement = delegate.createStatement(resultSetType, resultSetConcurrency);
          return new StatementProxy(statement, stateListener);
      }
  
      @Override
      public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
          Statement statement = delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
          return new StatementProxy(statement, stateListener);
      }
  
      @Override
      public PreparedStatement prepareStatement(String sql) throws SQLException {
          PreparedStatement preparedStatement = delegate.prepareStatement(sql);
          return new PreparedStatementProxy(preparedStatement, stateListener, sql);
      }
  
      @Override
      public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
          PreparedStatement preparedStatement = delegate.prepareStatement(sql, resultSetType, resultSetConcurrency);
          return new PreparedStatementProxy(preparedStatement, stateListener, sql);
      }
  
      @Override
      public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
          PreparedStatement preparedStatement = delegate.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
          return new PreparedStatementProxy(preparedStatement, stateListener, sql);
      }
  
      @Override
      public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
          PreparedStatement preparedStatement = delegate.prepareStatement(sql, autoGeneratedKeys);
          return new PreparedStatementProxy(preparedStatement, stateListener, sql);
      }
  
      @Override
      public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
          PreparedStatement preparedStatement = delegate.prepareStatement(sql, columnIndexes);
          return new PreparedStatementProxy(preparedStatement, stateListener, sql);
      }
  
      @Override
      public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
          PreparedStatement preparedStatement = delegate.prepareStatement(sql, columnNames);
          return new PreparedStatementProxy(preparedStatement, stateListener, sql);
      }
  
      private interface ConnectionOverwrite {
  
          Statement createStatement() throws SQLException;
  
          Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException;
  
          Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException;
          
          PreparedStatement prepareStatement(String sql) throws SQLException;
  
          PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException;
  
          PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException;
  
          PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException;
  
          PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException;
  
          PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException;
  
      }
  
  }
  