use super::{Token, TokenWithSpan};
#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
#[path = "execution/tests.rs"]
mod tests;
#[derive(Default)]
pub(in crate::migration) struct Scope {
blocks: Vec<&'static str>,
execution_stopped: bool,
raised_at: Option<usize>,
}
impl Scope {
pub(in crate::migration) fn advance(&mut self, code: &[&TokenWithSpan]) -> bool {
let was_stopped = self.execution_stopped || self.raised_at.is_some();
let mut at = 0;
let mut pending_loop = false;
while let Some(token) = code.get(at) {
if unquoted_word(token, "RETURN") {
let continues = code.get(at + 1).is_some_and(|next| {
unquoted_word(next, "NEXT") || unquoted_word(next, "QUERY")
});
if !continues {
self.execution_stopped = true;
}
break;
}
if unquoted_word(token, "RAISE") {
let raises = code
.get(at + 1)
.is_some_and(|next| unquoted_word(next, "EXCEPTION"));
if raises && self.blocks.iter().all(|kind| *kind == "BEGIN") {
self.raised_at.get_or_insert(self.blocks.len());
}
break;
}
if starts_sql(token) {
break;
}
if unquoted_word(token, "END") {
let kind = code
.get(at + 1)
.and_then(|token| control(token))
.unwrap_or("BEGIN");
if let Some(position) = self.blocks.iter().rposition(|entry| *entry == kind) {
self.blocks.truncate(position);
if self.raised_at.is_some_and(|depth| position < depth) {
self.raised_at = None;
self.execution_stopped = true;
}
}
at += usize::from(kind != "BEGIN");
} else if unquoted_word(token, "FOR") || unquoted_word(token, "WHILE") {
self.blocks.push("LOOP");
pending_loop = true;
} else if unquoted_word(token, "LOOP") && pending_loop {
pending_loop = false;
} else if let Some(kind) = control(token) {
if kind == "EXCEPTION" && self.raised_at == Some(self.blocks.len()) {
self.raised_at = None;
}
self.blocks.push(kind);
}
at += 1;
}
!was_stopped && self.blocks.iter().all(|kind| *kind == "BEGIN")
}
}
fn unquoted_word(token: &TokenWithSpan, expected: &str) -> bool {
matches!(&token.token, Token::Word(word) if word.quote_style.is_none() && word.value.eq_ignore_ascii_case(expected))
}
fn control(token: &TokenWithSpan) -> Option<&'static str> {
["BEGIN", "IF", "CASE", "LOOP", "EXCEPTION"]
.into_iter()
.find(|kind| unquoted_word(token, kind))
}
fn starts_sql(token: &TokenWithSpan) -> bool {
[
"CREATE", "ALTER", "DROP", "EXECUTE", "SELECT", "INSERT", "UPDATE", "DELETE", "RETURN",
]
.into_iter()
.any(|kind| unquoted_word(token, kind))
|| matches!(token.token, super::Token::Assignment | super::Token::Eq)
}