namespace IWS5.Core.Helpers;

public static class SqlValidatorHelper
{
    private static readonly string[] ForbiddenKeywords =
    {
            "INSERT ", "UPDATE ", "DELETE ", "DROP ", "ALTER ", "TRUNCATE ",
            "EXEC ", "EXECUTE ", "MERGE ", "CREATE ", "GRANT ", "REVOKE ", "xp_"
    };

    public static bool ContainsForbiddenKeywords(string sql)
    {
        if (string.IsNullOrWhiteSpace(sql))
            return false;

        return ForbiddenKeywords.Any(k =>
            sql.Contains(k, StringComparison.OrdinalIgnoreCase));
    }

    public static bool IsSafeSqlSyntax(string sql)
    {
        if (string.IsNullOrWhiteSpace(sql))
            return false;

        if (sql.Contains(';'))
            return false;

        if (!sql.Contains("FROM", StringComparison.OrdinalIgnoreCase))
            return false;

        if (sql.Contains("sys.", StringComparison.OrdinalIgnoreCase))
            return false;

        return true;
    }
}
