use super::{Cursor, SqlCursorBound};
use crate::idents::unwrap_expr;
use sqlparser::ast::{Expr, UnaryOperator};
pub(super) fn of(expr: &Expr, resolve: impl FnOnce(&Expr) -> Option<Cursor>) -> Option<Cursor> {
let mut odd = true;
let mut inner = unwrap_expr(expr);
while let Expr::UnaryOp {
op: UnaryOperator::Not,
expr,
} = inner
{
odd = !odd;
inner = unwrap_expr(expr);
}
let mut cursor = resolve(inner)?;
if !odd {
return Some(cursor);
}
if cursor.optional {
return None;
}
cursor.bound = match cursor.bound {
SqlCursorBound::Lower => SqlCursorBound::Upper,
SqlCursorBound::Upper => SqlCursorBound::Lower,
};
Some(cursor)
}