use crate::db::schema::PersistedIndexExpressionOp;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct SemanticIndexExpression {
op: PersistedIndexExpressionOp,
field: String,
}
impl SemanticIndexExpression {
#[must_use]
pub(in crate::db) const fn new(op: PersistedIndexExpressionOp, field: String) -> Self {
Self { op, field }
}
#[must_use]
pub(in crate::db) const fn field(&self) -> &str {
self.field.as_str()
}
#[must_use]
pub(in crate::db) const fn op(&self) -> PersistedIndexExpressionOp {
self.op
}
#[must_use]
#[cfg(any(test, feature = "sql"))]
pub(in crate::db) const fn supports_text_casefold_lookup(&self) -> bool {
matches!(
self.op,
PersistedIndexExpressionOp::Lower | PersistedIndexExpressionOp::Upper
)
}
#[must_use]
pub(in crate::db) fn canonical_order_text(&self) -> String {
let field = self.field();
match self.op {
PersistedIndexExpressionOp::Lower => format!("LOWER({field})"),
PersistedIndexExpressionOp::Upper => format!("UPPER({field})"),
PersistedIndexExpressionOp::Trim => format!("TRIM({field})"),
PersistedIndexExpressionOp::LowerTrim => format!("LOWER(TRIM({field}))"),
PersistedIndexExpressionOp::Date => format!("DATE({field})"),
PersistedIndexExpressionOp::Year => format!("YEAR({field})"),
PersistedIndexExpressionOp::Month => format!("MONTH({field})"),
PersistedIndexExpressionOp::Day => format!("DAY({field})"),
}
}
}