I'm trying to write something similar to the Query Builder separated
function, but for where statements, it works fine, except that I thought the builder push_bind
function would convert Options to NULL, but that doesn't seem to be the case, I tried to to work around it somehow but it didn't work either. Is there a way to achieve this ?
struct SqlxPushWhere<'qb, 'args: 'qb> {
start: bool,
query: &'qb mut QueryBuilder<'args, Sqlite>,
}
impl<'qb, 'args: 'qb> SqlxPushWhere<'qb, 'args> {
fn push<T>(&mut self, field: &str, value: T) -> &mut Self
where
T: 'args + Type<Sqlite> + Encode<'args, Sqlite> + Send,
{
if self.start {
self.query.push("WHERE ");
self.start = false;
} else {
self.query.push("AND ");
}
// Didn't work because is_null returns false
if value.produces().is_some_and(|t| t.is_null()) {
self.query.push(format!("{} IS NULL", field));
} else {
self.query.push(format!("{} = ", field)).push_bind(value);
}
self
}
}
fn example() {
// ...
let mut where = query.query_where();
where.push("field_foo", None);
}