no-mistakes-postgres-sql 0.109.130

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::super::predicates::Projection;
use super::super::SqlSelectFact;
use super::projection::Policy;
use crate::star_projection::WholeRowUses;
use sqlparser::ast::{Expr, JoinConstraint, Select, TableFactor, TableWithJoins};

pub(super) fn table_names(from: &[TableWithJoins], ctes: &[String]) -> Vec<String> {
    let mut names = Vec::new();
    for table in from {
        push_table(&table.relation, ctes, &mut names);
        for join in &table.joins {
            push_table(&join.relation, ctes, &mut names);
        }
    }
    names
}

pub(super) fn collect_derived_queries(
    sql: &str,
    from: &[TableWithJoins],
    ctes: &[String],
    in_insert_select: bool,
    policy: Policy<'_, impl Projection>,
    whole_rows: Option<&WholeRowUses>,
    out: &mut Vec<SqlSelectFact>,
) {
    for factor in from.iter().flat_map(|table| {
        std::iter::once(&table.relation).chain(table.joins.iter().map(|join| &join.relation))
    }) {
        collect_derived_factor(sql, factor, ctes, in_insert_select, policy, whole_rows, out);
    }
}

fn collect_derived_factor(
    sql: &str,
    table: &TableFactor,
    ctes: &[String],
    in_insert_select: bool,
    policy: Policy<'_, impl Projection>,
    whole_rows: Option<&WholeRowUses>,
    out: &mut Vec<SqlSelectFact>,
) {
    match table {
        TableFactor::Derived { subquery, .. } => {
            let narrowed = whole_rows.is_some_and(|uses| uses.narrows_derived(table));
            super::collector::collect_query_projected(
                sql,
                subquery,
                ctes,
                in_insert_select,
                false,
                policy.with_narrowed(narrowed),
                out,
            );
        }
        TableFactor::NestedJoin {
            table_with_joins,
            alias,
        } => collect_derived_queries(
            sql,
            std::slice::from_ref(table_with_joins),
            ctes,
            in_insert_select,
            policy,
            whole_rows.filter(|_| alias.is_none()),
            out,
        ),
        _ => {}
    }
}

fn push_table(table: &TableFactor, ctes: &[String], names: &mut Vec<String>) {
    match table {
        TableFactor::Table { name, .. } => {
            if let Some(table) = super::super::predicates::base_table(name, ctes) {
                names.push(table);
            }
        }
        TableFactor::NestedJoin {
            table_with_joins, ..
        } => names.extend(table_names(std::slice::from_ref(table_with_joins), ctes)),
        _ => {}
    }
}

pub(in crate::statements) fn predicate_text(select: &Select) -> String {
    let mut parts = Vec::new();
    if let Some(selection) = &select.selection {
        parts.push(selection.to_string());
    }
    for table in &select.from {
        for join in &table.joins {
            if let Some(expr) = join_expr(&join.join_operator) {
                parts.push(expr.to_string());
            }
        }
    }
    parts.join(" ")
}

pub(super) fn join_expr(operator: &sqlparser::ast::JoinOperator) -> Option<&Expr> {
    match operator {
        sqlparser::ast::JoinOperator::Join(JoinConstraint::On(expr))
        | sqlparser::ast::JoinOperator::Inner(JoinConstraint::On(expr))
        | sqlparser::ast::JoinOperator::Left(JoinConstraint::On(expr))
        | sqlparser::ast::JoinOperator::LeftOuter(JoinConstraint::On(expr))
        | sqlparser::ast::JoinOperator::Right(JoinConstraint::On(expr))
        | sqlparser::ast::JoinOperator::RightOuter(JoinConstraint::On(expr))
        | sqlparser::ast::JoinOperator::FullOuter(JoinConstraint::On(expr)) => Some(expr),
        _ => None,
    }
}