no-mistakes-postgres-sql 0.109.131

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::*;

pub(in super::super) fn for_header(code: &[&Token]) -> bool {
    let Some(into) = code.iter().position(|token| word(token, "IN")) else {
        return false;
    };
    let Some(targets) = assignment::comma_parts(&code[1..into]) else {
        return false;
    };
    if !targets.iter().all(|target| assignment::target(target)) {
        return false;
    }
    let mut source = &code[into + 1..];
    if bound_cursor_args(source).is_some()
        || cursor_reference_len(source.iter().copied()) == Some(source.len())
    {
        return targets.len() == 1;
    }
    if row_query(source)
        || source.first().is_some_and(|token| word(token, "EXECUTE"))
            && (static_execute(source) || super::super::sql::matches(source))
    {
        return true;
    }
    if source.first().is_some_and(|token| word(token, "REVERSE")) {
        source = &source[1..];
    }
    let step = source.iter().position(|token| word(token, "BY"));
    if let Some(at) = step {
        if !assignment::expression(&source[at + 1..]) {
            return false;
        }
        source = &source[..at];
    }
    let Some(range) = source
        .iter()
        .position(|token| super::super::ranges::marker(token))
    else {
        return false;
    };
    assignment::expression(&source[..range]) && assignment::expression(&source[range + 1..])
}

pub(in super::super) fn foreach_header(code: &[&Token]) -> bool {
    foreach_array_start(code).is_some_and(|at| assignment::expression(&code[at..]))
}

pub(in super::super) fn foreach_array_start(code: &[&Token]) -> Option<usize> {
    let into = super::super::boundary(code, "IN")?;
    let targets = match &code[1..into] {
        [targets @ .., slice, Token::Number(count, false)]
            if word(slice, "SLICE")
                && crate::numeric_literal::integer(count)
                    .is_ok_and(|value| i32::try_from(value).is_ok()) =>
        {
            targets
        }
        targets => targets,
    };
    let targets = assignment::comma_parts(targets)?;
    (targets.into_iter().all(assignment::target)
        && code.get(into + 1).is_some_and(|token| word(token, "ARRAY")))
    .then_some(into + 2)
}