mod bind_guard;
mod conditions;
mod lexicographic;
mod negated;
pub(super) mod placeholders;
use crate::idents::{ident_key, unwrap_expr};
use crate::statements::{SqlConjunctFact, SqlCursorBound};
use conditions::{flatten, is_constant_true};
use placeholders::is_recovered_placeholder;
use sqlparser::ast::{BinaryOperator, Expr, UnaryOperator, Value};
pub(super) fn of(
selection: Option<&Expr>,
names: &[String],
order_columns: &[String],
order_ascending: &[Option<bool>],
int4_bindings: &super::Int4Bindings,
recovered_placeholder_positions: &[(u32, u32)],
) -> Vec<SqlConjunctFact> {
let mut leaves = Vec::new();
if let Some(selection) = selection {
flatten(selection, &mut leaves);
}
leaves
.into_iter()
.map(|leaf| {
let cursor = cursor(
leaf,
names,
order_columns,
order_ascending,
int4_bindings,
recovered_placeholder_positions,
);
SqlConjunctFact {
text: crate::predicate_normalization::normalize(&leaf.to_string()),
cursor_columns: cursor
.as_ref()
.map_or_else(Vec::new, |cursor| cursor.columns.clone()),
cursor_bound: cursor.as_ref().map(|cursor| cursor.bound),
cursor_optional: cursor.as_ref().is_some_and(|cursor| cursor.optional),
constant_true: is_constant_true(leaf),
bind_guard: bind_guard::is_bind_guard(leaf, recovered_placeholder_positions),
}
})
.collect()
}
pub(super) struct Cursor {
columns: Vec<String>,
bound: SqlCursorBound,
optional: bool,
}
pub(super) fn column(
expr: &Expr,
names: &[String],
recovered_placeholder_positions: &[(u32, u32)],
) -> Option<String> {
match unwrap_expr(expr) {
Expr::Identifier(ident)
if is_recovered_placeholder(ident, recovered_placeholder_positions) =>
{
None
}
Expr::Identifier(ident) => Some(ident_key(ident)),
Expr::CompoundIdentifier(parts) => {
let (column, qualifier) = (parts.last()?, parts.get(parts.len().checked_sub(2)?)?);
names
.contains(&ident_key(qualifier))
.then(|| ident_key(column))
}
_ => None,
}
}
fn cursor(
expr: &Expr,
names: &[String],
order_columns: &[String],
order_ascending: &[Option<bool>],
int4_bindings: &super::Int4Bindings,
recovered_placeholder_positions: &[(u32, u32)],
) -> Option<Cursor> {
match unwrap_expr(expr) {
Expr::UnaryOp {
op: UnaryOperator::Not,
expr,
} => negated::of(expr, |inner| {
cursor(
inner,
names,
order_columns,
order_ascending,
int4_bindings,
recovered_placeholder_positions,
)
}),
Expr::BinaryOp {
left,
op:
op @ (BinaryOperator::Gt
| BinaryOperator::GtEq
| BinaryOperator::Lt
| BinaryOperator::LtEq),
right,
} => {
let greater = matches!(op, BinaryOperator::Gt | BinaryOperator::GtEq);
let (columns, lower) =
match compare(left, right, names, recovered_placeholder_positions) {
Some(columns) => (columns, greater),
None => (
compare(right, left, names, recovered_placeholder_positions)?,
!greater,
),
};
Some(Cursor {
columns,
bound: bound(lower),
optional: false,
})
}
Expr::BinaryOp {
left,
op: BinaryOperator::Or,
right,
} => match (unwrap_expr(left), unwrap_expr(right)) {
(Expr::IsNull(probe), other) | (other, Expr::IsNull(probe))
if is_bind(probe, recovered_placeholder_positions) =>
{
cursor(
other,
names,
order_columns,
order_ascending,
int4_bindings,
recovered_placeholder_positions,
)
.map(|cursor| Cursor {
optional: true,
..cursor
})
}
_ => lexicographic::cursor(
expr,
names,
order_columns,
order_ascending,
int4_bindings,
recovered_placeholder_positions,
),
},
_ => None,
}
}
fn bound(lower: bool) -> SqlCursorBound {
if lower {
SqlCursorBound::Lower
} else {
SqlCursorBound::Upper
}
}
fn compare(
column_side: &Expr,
value_side: &Expr,
names: &[String],
recovered_placeholder_positions: &[(u32, u32)],
) -> Option<Vec<String>> {
if !is_bind(value_side, recovered_placeholder_positions) {
return None;
}
match unwrap_expr(column_side) {
Expr::Tuple(items) => items
.iter()
.map(|item| column(item, names, recovered_placeholder_positions))
.collect(),
other => column(other, names, recovered_placeholder_positions).map(|column| vec![column]),
}
}
fn is_bind(expr: &Expr, recovered_placeholder_positions: &[(u32, u32)]) -> bool {
match unwrap_expr(expr) {
Expr::Value(value) => matches!(value.value, Value::Placeholder(_)),
Expr::Identifier(ident) => is_recovered_placeholder(ident, recovered_placeholder_positions),
Expr::Cast { expr, .. } => is_bind(expr, recovered_placeholder_positions),
Expr::Tuple(items) => {
!items.is_empty()
&& items
.iter()
.all(|item| is_bind(item, recovered_placeholder_positions))
}
_ => false,
}
}