// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package de.bytefish.pgbulkinsert.de.bytefish.pgbulkinsert.util;

import com.sun.istack.internal.NotNull;

import de.bytefish.pgbulkinsert.de.bytefish.pgbulkinsert.exceptions.PgConnectionException;

import org.postgresql.PGConnection;

import java.sql.Connection;
import java.sql.SQLException;

public final class PostgreSqlUtils {

    private PostgreSqlUtils() {
    }

    public static PGConnection getPGConnection(final Connection connection) throws SQLException {
        OutParameter<PGConnection> result = new OutParameter<>();
        if(!tryGetPGConnection(connection, result)) {
            throw new PgConnectionException("Could not obtain a PGConnection");
        }
        return result.get();
    }

    public static boolean tryGetPGConnection(final Connection connection, @NotNull OutParameter<PGConnection> result) throws SQLException {
        if(tryCastConnection(connection, result)) {
            return true;
        }
        if(tryUnwrapConnection(connection, result)) {
            return true;
        }
        return false;
    }

    private static boolean tryCastConnection(final Connection connection, @NotNull OutParameter<PGConnection> result) {
        if (connection instanceof PGConnection) {
            result.set((PGConnection) connection);

            return true;
        }
        return false;
    }

    private static boolean tryUnwrapConnection(final Connection connection, @NotNull OutParameter<PGConnection> result) {
        try {
            if (connection.isWrapperFor(PGConnection.class)) {
                result.set(connection.unwrap(PGConnection.class));
                return true;
            }
            return false;
        } catch(Exception e) {
            return false;
        }
    }
}
@@ -219,49 +219,8 @@
 l;%0A%0A
-import com.sun.istack.internal.NotNull;%0A%0A
 impo
@@ -302,25 +302,24 @@
 nException;%0A
-%0A
 import org.p
@@ -892,33 +892,24 @@
  connection,
- @NotNull
  OutParamete
@@ -1228,33 +1228,24 @@
  connection,
- @NotNull
  OutParamete
@@ -1428,32 +1428,32 @@
 n false;%0A    %7D%0A%0A
+
     private stat
@@ -1515,17 +1515,8 @@
 ion,
- @NotNull
  Out

Remove JVM internal import