package src.models.squirrel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.function.Consumer;

public class Database {
    private static Connection conn;
    public static Consumer<Connection> migrate;

    public static Connection getConnection() throws SQLException {
        if (conn == null || conn.isClosed()) {
            String url = "jdbc:sqlite:instance/test.db";
            conn = DriverManager.getConnection(url);
            if (migrate != null) {
                migrate.accept(conn);
            }
        }
        return conn;
    }

    public static void close() {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
