package io.github.bucket4j.postgresql;

import io.github.bucket4j.BucketExceptions;
import io.github.bucket4j.distributed.proxy.generic.select_for_update.AbstractLockBasedProxyManager;
import io.github.bucket4j.distributed.proxy.generic.select_for_update.LockBasedTransaction;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Objects;

public class PostgreSQLProxyManager extends AbstractLockBasedProxyManager<Long> {

    private final DataSource dataSource;
    private final PostgreSQLProxyConfiguration configuration;

    public PostgreSQLProxyManager(PostgreSQLProxyConfiguration configuration) {
        super(configuration.getClientSideConfig());
        this.dataSource = Objects.requireNonNull(configuration.getDataSource());
        this.configuration = configuration;
    }

    @Override
    protected LockBasedTransaction allocateTransaction(Long key) {
        try {
            return new PostgreSQLLockBasedTransactionFactory(key, configuration, dataSource.getConnection()).getLockBasedTransaction(configuration.getLockBasedTransactionType());
        } catch (SQLException e) {
            throw new BucketExceptions.BucketExecutionException(e);
        }
    }

    @Override
    protected void releaseTransaction(LockBasedTransaction transaction) {
        try {
            ((AbstractPostgreSQLLockBasedTransaction) transaction).getConnection().close();
        } catch (SQLException e) {
            throw new BucketExceptions.BucketExecutionException(e);
        }
    }

    @Override
    public void removeProxy(Long key) {
        // TODO implement removal
        throw new UnsupportedOperationException();
    }
}

@@ -316,28 +316,123 @@
 ava.sql.
-SQLException
+Connection;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.SQLException;%0Aimport java.text.MessageFormat
 ;%0Aimport
@@ -637,16 +637,57 @@
 uration;
+%0A    private final String removeSqlQuery;
 %0A%0A    pu
@@ -933,24 +933,166 @@
 figuration;%0A
+        this.removeSqlQuery = MessageFormat.format(%22DELETE FROM %7B0%7D WHERE %7B1%7D = ?%22, configuration.getTableName(), configuration.getIdName());%0A
     %7D%0A%0A    @
@@ -1860,68 +1860,373 @@
     
-// TODO implement removal%0A        throw new UnsupportedOpera
+try %7B%0A            Connection connection = dataSource.getConnection();%0A            try(PreparedStatement removeStatement = connection.prepareStatement(removeSqlQuery)) %7B%0A                removeStatement.setLong(1, key);%0A                removeStatement.executeQuery();%0A            %7D%0A        %7D catch (SQLException e) %7B%0A            throw new BucketExceptions.BucketExecu
 tion
@@ -2231,23 +2231,34 @@
 onException(
+e
 );%0A
+        %7D%0A
     %7D%0A%7D%0A

implement removeProxy