use super::{SqlTextComparison, push_slots};
use crate::comparison::{
At, Backend, ComplexComparison, TextMatch,
sql::{Carrier, not_wrapped, push_column},
};
impl<V> ComplexComparison<sqlx::Sqlite> for SqlTextComparison<V, sqlx::Sqlite>
where
V: Carrier<sqlx::Sqlite>,
{
#[inline]
fn conjuncts(&self) -> usize {
Self::conjuncts(self)
}
fn apply<'q>(
&'q self,
at: At<'_>,
mut builder: <sqlx::Sqlite as Backend>::Builder<'q>,
) -> Result<<sqlx::Sqlite as Backend>::Builder<'q>, <sqlx::Sqlite as Backend>::Error> {
push_slots(self, at, &mut builder, push_text_match);
Ok(builder)
}
}
fn push_text_match<V>(
qb: &mut sqlx::QueryBuilder<sqlx::Sqlite>,
at: At<'_>,
kind: TextMatch,
operand: &V,
negated: bool,
nocase: bool,
) where
V: Carrier<sqlx::Sqlite>,
{
not_wrapped(qb, negated, |qb| match kind {
TextMatch::Contains => {
qb.push("instr(");
push_folded_column(qb, at, nocase);
qb.push(", ");
push_folded_operand(qb, operand, nocase);
qb.push(") > 0");
}
TextMatch::Prefix => {
qb.push("substr(");
push_folded_column(qb, at, nocase);
qb.push(", 1, length(");
push_folded_operand(qb, operand, nocase);
qb.push(")) = ");
push_folded_operand(qb, operand, nocase);
}
TextMatch::Suffix => {
qb.push("substr(");
push_folded_column(qb, at, nocase);
qb.push(", -length(");
push_folded_operand(qb, operand, nocase);
qb.push(")) = ");
push_folded_operand(qb, operand, nocase);
}
});
}
fn push_folded_operand<V>(qb: &mut sqlx::QueryBuilder<sqlx::Sqlite>, operand: &V, nocase: bool)
where
V: Carrier<sqlx::Sqlite>,
{
if nocase {
qb.push("lower(");
}
operand.push_bind(qb);
if nocase {
qb.push(")");
}
}
fn push_folded_column(qb: &mut sqlx::QueryBuilder<sqlx::Sqlite>, at: At<'_>, nocase: bool) {
if nocase {
qb.push("lower(");
}
push_column(qb, at);
if nocase {
qb.push(")");
}
}