use super::{record, Event};
use sqlparser::{
ast::Statement,
tokenizer::{Location, Span},
};
use std::iter::Peekable;
pub(in crate::migration) struct Barriers {
pending: Peekable<std::vec::IntoIter<Span>>,
active_end: Option<Location>,
}
impl Barriers {
pub(in crate::migration) fn new(spans: Vec<Span>) -> Self {
Self {
pending: spans.into_iter().peekable(),
active_end: None,
}
}
pub(in crate::migration) fn record(
&mut self,
statement: &Statement,
position: Location,
out: &mut Vec<Event>,
) {
let mut crossed = false;
while let Some(span) = self.pending.next_if(|span| span.start <= position) {
self.active_end = Some(span.end);
crossed = true;
}
if crossed {
out.push(Event::Reset);
}
if self.active_end.is_none_or(|end| position >= end) {
record(statement, position.line.max(1) as usize, out);
}
}
pub(in crate::migration) fn finish(mut self, out: &mut Vec<Event>) {
if self.pending.next().is_some() {
out.push(Event::Reset);
}
}
}