using System.Data;
using Npgsql;

namespace GBDD;

internal static class Db
{
    private const string ConnectionString =
        "Host=edu-pg.itiscaf.ru;Port=5432;Database=dbwork;Username=yufrikov_mi;Password=$$c&wC&%i3H";

    public static DataTable Select(string sql)
    {
        using var connection = new NpgsqlConnection(ConnectionString);
        using var adapter = new NpgsqlDataAdapter(sql, connection);

        var table = new DataTable();
        adapter.Fill(table);
        return table;
    }

    public static object? Scalar(string sql)
    {
        using var connection = new NpgsqlConnection(ConnectionString);
        connection.Open();

        using var command = new NpgsqlCommand(sql, connection);
        return command.ExecuteScalar();
    }

    public static void Execute(string sql)
    {
        using var connection = new NpgsqlConnection(ConnectionString);
        connection.Open();

        using var command = new NpgsqlCommand(sql, connection);
        command.ExecuteNonQuery();
    }
}
