package me.shawlaf.banmanager.core.database.util;

import me.shawlaf.banmanager.core.async.Multithreading;
import me.shawlaf.banmanager.core.async.SimpleFuture;
import me.shawlaf.banmanager.core.database.sql.AbstractSqlTable;
import me.shawlaf.banmanager.core.database.sql.SqlConnectionManager;
import me.shawlaf.banmanager.core.indev.SQLQueryOnMainThreadException;

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

/**
 * Created by Florian on 21.05.2017.
 */
public interface DatabaseExecuteable<T> {

    default SimpleFuture<T> executeAsync(AbstractSqlTable table) throws SQLException {
        return executeAsync(table.connection(), table.table);
    }

    default T executeSync(AbstractSqlTable table) throws SQLException {
        if (SqlConnectionManager.checkMainThread()) {
            SQLQueryOnMainThreadException e = new SQLQueryOnMainThreadException();
            Multithreading.runAsync(() -> e.createReport());
        }

        return executeSync(table.connection(), table.table);
    }

    SimpleFuture<T> executeAsync(Connection connection, String table) throws SQLException;

    T executeSync(Connection connection, String table) throws SQLException;
}