use crate::idents::object_name_identity;
use sqlparser::ast::{
Delete, FromTable, JoinConstraint, JoinOperator, Merge, OnConflictAction, OnInsert, Statement,
TableFactor, TableWithJoins, Update, UpdateTableFromKind,
};
use std::collections::BTreeMap;
pub(in super::super) fn check_upsert(
statement: &Statement,
ctes: &BTreeMap<String, super::super::VisibleCte<'_>>,
out: &mut Vec<super::super::StatementLockCheck>,
) {
let Statement::Insert(insert) = statement else {
return;
};
let Some(OnInsert::OnConflict(conflict)) = &insert.on else {
return;
};
let OnConflictAction::DoUpdate(update) = &conflict.action else {
return;
};
if let Some(selection) = &update.selection {
super::check_guard(selection, ctes, out);
}
}
pub(in super::super) fn check_update_joins(
update: &Update,
ctes: &BTreeMap<String, super::super::VisibleCte<'_>>,
prior: bool,
out: &mut Vec<super::super::StatementLockCheck>,
) {
check_table_joins(&update.table, false, ctes, out);
if prior {
return;
}
if let Some(UpdateTableFromKind::AfterSet(tables) | UpdateTableFromKind::BeforeSet(tables)) =
&update.from
{
for table in tables {
check_table_joins(table, true, ctes, out);
}
}
}
pub(in super::super) fn check_delete_joins(
delete: &Delete,
ctes: &BTreeMap<String, super::super::VisibleCte<'_>>,
linked: bool,
out: &mut Vec<super::super::StatementLockCheck>,
) {
let (FromTable::WithFromKeyword(tables) | FromTable::WithoutKeyword(tables)) = &delete.from;
for table in tables {
check_table_joins(table, false, ctes, out);
}
if !linked {
for table in delete.using.iter().flatten() {
check_table_joins(table, true, ctes, out);
}
}
}
pub(in super::super) fn check_merge(
merge: &Merge,
ctes: &BTreeMap<String, super::super::VisibleCte<'_>>,
out: &mut Vec<super::super::StatementLockCheck>,
) {
super::check_guard(&merge.on, ctes, out);
for predicate in merge
.clauses
.iter()
.filter_map(|clause| clause.predicate.as_ref())
{
super::check_guard(predicate, ctes, out);
}
check_source(&merge.source, ctes, out);
}
fn check_table_joins(
table: &TableWithJoins,
read_base: bool,
ctes: &BTreeMap<String, super::super::VisibleCte<'_>>,
out: &mut Vec<super::super::StatementLockCheck>,
) {
if read_base {
check_source(&table.relation, ctes, out);
}
for join in &table.joins {
check_source(&join.relation, ctes, out);
if let JoinOperator::Join(JoinConstraint::On(expr))
| JoinOperator::Inner(JoinConstraint::On(expr))
| JoinOperator::Left(JoinConstraint::On(expr))
| JoinOperator::LeftOuter(JoinConstraint::On(expr))
| JoinOperator::Right(JoinConstraint::On(expr))
| JoinOperator::RightOuter(JoinConstraint::On(expr))
| JoinOperator::FullOuter(JoinConstraint::On(expr)) = &join.join_operator
{
super::check_guard(expr, ctes, out);
}
}
}
fn check_source(
source: &TableFactor,
ctes: &BTreeMap<String, super::super::VisibleCte<'_>>,
out: &mut Vec<super::super::StatementLockCheck>,
) {
if let TableFactor::NestedJoin {
table_with_joins, ..
} = source
{
check_table_joins(table_with_joins, true, ctes, out);
return;
}
let guarded = match source {
TableFactor::Table {
name,
args: None,
sample,
..
} => {
ctes.get(&object_name_identity(name))
.is_some_and(|cte| cte.has_guard)
|| sample.is_some() && super::contains_guard(sample, ctes)
}
_ => super::contains_guard(source, ctes),
};
if guarded {
out.push(super::super::StatementLockCheck::Guard);
}
}