use super::*;
use crate::idents::{ident_key, object_name_ident};
mod wide;
use wide::{bare_name, wide_column};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RowAccess {
pub relation: Option<String>,
pub key: Option<(String, String)>,
pub recovered_key: Option<(String, (u32, u32))>,
pub key_identity_unproven: bool,
pub has_predicate: bool,
pub only: bool,
}
#[derive(Default)]
struct Keys {
key: Option<(String, String)>,
recovered_key: Option<(String, (u32, u32))>,
value_cast: bool,
}
type BoundValue = (Option<String>, Option<(u32, u32)>);
pub(super) fn access(
table: &TableFactor,
predicate: Option<&Expr>,
positions: PlaceholderPositions<'_>,
) -> RowAccess {
access_with(table, predicate, positions, false)
}
pub(super) fn lookup_access(
table: &TableFactor,
predicate: Option<&Expr>,
positions: PlaceholderPositions<'_>,
) -> RowAccess {
access_with(table, predicate, positions, true)
}
fn access_with(
table: &TableFactor,
predicate: Option<&Expr>,
positions: PlaceholderPositions<'_>,
wide: bool,
) -> RowAccess {
match table {
TableFactor::Table {
name,
alias,
args: None,
inheritance,
..
} => {
let keys = predicate
.and_then(|expr| {
let qual = if wide && alias.is_none() {
Some(object_name_identity(name))
} else {
qualifier(table)
};
let bare = (wide && alias.is_none()).then(|| bare_name(name)).flatten();
equality(expr, qual.as_deref(), bare.as_deref(), positions, wide)
})
.unwrap_or_default();
RowAccess {
relation: (!generated_name(name, positions)
&& !alias
.as_ref()
.is_some_and(|alias| generated(&alias.name, positions)))
.then(|| object_name_identity(name)),
has_predicate: predicate.is_some(),
key: keys.key,
recovered_key: keys.recovered_key,
key_identity_unproven: keys.value_cast
|| alias
.as_ref()
.is_some_and(|alias| !alias.columns.is_empty()),
only: matches!(
inheritance,
Some(sqlparser::ast::TableInheritance::Only { .. })
),
}
}
_ => RowAccess {
relation: None,
key: None,
recovered_key: None,
key_identity_unproven: false,
has_predicate: predicate.is_some(),
only: false,
},
}
}
pub(super) fn qualifier(table: &TableFactor) -> Option<String> {
match table {
TableFactor::Table { name, alias, .. } => alias
.as_ref()
.map(|alias| &alias.name)
.or_else(|| object_name_ident(name))
.map(ident_key),
_ => None,
}
}
fn equality(
expr: &Expr,
qualifier: Option<&str>,
bare: Option<&str>,
positions: PlaceholderPositions<'_>,
wide: bool,
) -> Option<Keys> {
let Expr::BinaryOp {
left,
op: BinaryOperator::Eq,
right,
} = unwrap_expr(expr)
else {
return None;
};
let binding = |column_expr, value_expr| {
let pick = if wide { wide_column } else { column };
let column = pick(column_expr, qualifier, positions)
.or_else(|| bare.and_then(|bare| pick(column_expr, Some(bare), positions)))?;
let (value, origin) = bound(value_expr, positions)?;
let recovered_key = origin.map(|at| (column.clone(), at));
let key = value.map(|value| (column, value));
Some(Keys {
key,
recovered_key,
value_cast: matches!(unwrap_expr(value_expr), Expr::Cast { .. }),
})
};
binding(left, right).or_else(|| binding(right, left))
}
pub(super) fn column(
expr: &Expr,
qualifier: Option<&str>,
positions: PlaceholderPositions<'_>,
) -> Option<String> {
match unwrap_expr(expr) {
Expr::Identifier(id) if !generated(id, positions) => Some(ident_key(id)),
Expr::CompoundIdentifier(ids)
if ids.len() == 2
&& !ids.iter().any(|id| generated(id, positions))
&& Some(ident_key(&ids[0]).as_str()) == qualifier =>
{
Some(ident_key(&ids[1]))
}
_ => None,
}
}
pub(super) fn bound(expr: &Expr, positions: PlaceholderPositions<'_>) -> Option<BoundValue> {
match unwrap_expr(expr) {
Expr::Value(value) => {
let at = (value.span.start.line as u32, value.span.start.column as u32);
let origin = matches!(value.value, sqlparser::ast::Value::Placeholder(_))
&& positions.is_some_and(|positions| positions.contains(&at));
Some((Some(value.to_string()), origin.then_some(at)))
}
Expr::Identifier(ident) if generated(ident, positions) => Some((
None,
Some((ident.span.start.line as u32, ident.span.start.column as u32)),
)),
Expr::Cast { expr, .. } => bound(expr, positions),
_ => None,
}
}
pub(super) fn constant(expr: &Expr) -> bool {
match unwrap_expr(expr) {
Expr::Value(value) => !matches!(
value.value,
sqlparser::ast::Value::Placeholder(_) | sqlparser::ast::Value::Null
),
Expr::Cast { expr, .. } | Expr::UnaryOp { expr, .. } => constant(expr),
Expr::BinaryOp { left, right, .. } => constant(left) && constant(right),
_ => false,
}
}
pub(super) fn generated(
ident: &sqlparser::ast::Ident,
positions: PlaceholderPositions<'_>,
) -> bool {
super::super::value::is_placeholder_ident_at(ident, Some(positions.unwrap_or_default()))
}
pub(super) fn generated_name(
name: &sqlparser::ast::ObjectName,
positions: PlaceholderPositions<'_>,
) -> bool {
name.0
.iter()
.filter_map(|part| part.as_ident())
.any(|ident| generated(ident, positions))
}