use super::Scope;
use crate::idents::{ident_key, object_name_identity, relation_part_key};
use sqlparser::ast::{Ident, ObjectName, TableFactor, TableWithJoins};
use std::collections::{BTreeMap, BTreeSet};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(in crate::usage) struct Bindings {
pub(super) names: BTreeMap<String, BTreeSet<String>>,
pub(in crate::usage) tables: BTreeSet<String>,
}
impl Bindings {
pub(in crate::usage) fn table(
&mut self,
name: &ObjectName,
alias: Option<&Ident>,
cte: bool,
) -> BTreeSet<String> {
let relation = object_name_identity(name);
let targets = if cte {
BTreeSet::new()
} else {
BTreeSet::from([relation.clone()])
};
self.tables.extend(targets.iter().cloned());
if let Some(alias) = alias {
self.bind(alias, &targets);
} else if let Some(part) = name.0.last().and_then(|part| part.as_ident()) {
self.bind(part, &targets);
self.names
.entry(relation)
.or_default()
.extend(targets.iter().cloned());
}
targets
}
pub(in crate::usage) fn joined(
&mut self,
source: &TableWithJoins,
scope: &Scope,
wildcard: &mut BTreeSet<String>,
) -> BTreeSet<String> {
let mut tables = self.factor(&source.relation, scope, wildcard);
for join in &source.joins {
let right = self.factor(&join.relation, scope, wildcard);
match crate::usage::details::join_constraint(&join.join_operator) {
Some(sqlparser::ast::JoinConstraint::Natural) => {
wildcard.extend(tables.iter().chain(&right).cloned());
}
Some(sqlparser::ast::JoinConstraint::UsingAlias(_, alias)) => {
self.bind(alias, &BTreeSet::new());
}
_ => {}
}
tables.extend(right);
}
tables
}
pub(in crate::usage) fn factor(
&mut self,
source: &TableFactor,
scope: &Scope,
wildcard: &mut BTreeSet<String>,
) -> BTreeSet<String> {
match source {
TableFactor::Table {
name,
alias,
args: None,
..
} => {
let targets = self.table(
name,
alias.as_ref().map(|alias| &alias.name),
scope.is_cte(name),
);
if alias
.as_ref()
.is_some_and(|alias| !alias.columns.is_empty())
{
wildcard.extend(targets.iter().cloned());
}
targets
}
TableFactor::Table {
name,
alias: None,
args: Some(_),
..
}
| TableFactor::Function {
name, alias: None, ..
} => {
if let Some(part) = name.0.last().and_then(|part| part.as_ident()) {
self.bind(part, &BTreeSet::new());
}
BTreeSet::new()
}
TableFactor::NestedJoin {
table_with_joins,
alias,
} => {
if let Some(alias) = alias {
let mut inner = Self::default();
let tables = inner.joined(table_with_joins, scope, wildcard);
self.bind(&alias.name, &tables);
for (name, targets) in inner.names {
self.names.entry(name).or_default().extend(targets);
}
if !alias.columns.is_empty() {
wildcard.extend(tables.iter().cloned());
}
self.tables.extend(tables.iter().cloned());
tables
} else {
self.joined(table_with_joins, scope, wildcard)
}
}
TableFactor::Table {
alias: Some(alias), ..
}
| TableFactor::Derived {
alias: Some(alias), ..
}
| TableFactor::Function {
alias: Some(alias), ..
}
| TableFactor::TableFunction {
alias: Some(alias), ..
}
| TableFactor::JsonTable {
alias: Some(alias), ..
}
| TableFactor::OpenJsonTable {
alias: Some(alias), ..
}
| TableFactor::XmlTable {
alias: Some(alias), ..
}
| TableFactor::UNNEST {
alias: Some(alias), ..
} => {
self.bind(&alias.name, &BTreeSet::new());
BTreeSet::new()
}
TableFactor::UNNEST { alias: None, .. } => {
self.bind(&Ident::new("unnest"), &BTreeSet::new());
BTreeSet::new()
}
TableFactor::XmlTable { alias: None, .. } => {
self.bind(&Ident::new("xmltable"), &BTreeSet::new());
BTreeSet::new()
}
TableFactor::JsonTable { alias: None, .. } => {
self.bind(&Ident::new("json_table"), &BTreeSet::new());
BTreeSet::new()
}
_ => BTreeSet::new(),
}
}
fn bind(&mut self, alias: &Ident, tables: &BTreeSet<String>) {
self.names
.entry(relation_part_key(&ident_key(alias)))
.or_default()
.extend(tables.iter().cloned());
}
}