no-mistakes-postgres-sql 0.109.131

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use crate::idents::{ident_key, object_name_ident, unwrap_expr};
use sqlparser::ast::{
    BinaryOperator, Distinct, Expr, FunctionArguments, Query, SetExpr, UnaryOperator,
};

pub(in super::super) fn membership_query(expr: &Expr) -> Option<&Query> {
    match unwrap_expr(expr) {
        Expr::Subquery(query) => Some(query),
        Expr::Function(call)
            if object_name_ident(&call.name).is_some_and(|name| ident_key(name) == "array") =>
        {
            match &call.args {
                FunctionArguments::Subquery(query) => Some(query),
                _ => None,
            }
        }
        _ => None,
    }
}

pub(in super::super) fn predicates_of<'a>(predicate: &'a Expr, out: &mut Vec<&'a Expr>) {
    match unwrap_expr(predicate) {
        Expr::BinaryOp {
            left,
            op: BinaryOperator::And | BinaryOperator::Or,
            right,
        } => {
            predicates_of(left, out);
            predicates_of(right, out);
        }
        Expr::UnaryOp {
            op: UnaryOperator::Not,
            expr,
        } => predicates_of(expr, out),
        expr => out.push(expr),
    }
}

pub(in super::super) fn unrestricted_projection(
    query: &Query,
    positions: super::super::PlaceholderPositions<'_>,
) -> bool {
    matches!(query.body.as_ref(), SetExpr::Select(select) if select.selection.is_none() && select.having.is_none() && !matches!(select.distinct, Some(Distinct::On(_))))
        && query.limit_clause.is_none()
        && query.fetch.is_none()
        && super::super::simple_select_table(query).is_some_and(|table| {
            !super::super::helpers::access(&table.relation, None, positions).key_identity_unproven
        })
}