|
| 1 | +use diesel::{ |
| 2 | + helper_types::{IntoBoxed, LeftJoin, LeftJoinQuerySource}, |
| 3 | + prelude::*, |
| 4 | + sql_types::{Bool, Nullable}, |
| 5 | + sqlite::Sqlite, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::*; |
| 9 | + |
| 10 | +table! { |
| 11 | + bike (id) { |
| 12 | + id -> Text, |
| 13 | + name -> Text, |
| 14 | + owner_id -> Text, |
| 15 | + color_id -> Text |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +table! { |
| 20 | + color (id) { |
| 21 | + id -> Text, |
| 22 | + name -> Text |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +joinable!(bike -> color (color_id)); |
| 27 | +allow_tables_to_appear_in_same_query!(bike, color); |
| 28 | + |
| 29 | +#[allow(non_camel_case_types)] |
| 30 | +pub(super) enum Condition { |
| 31 | + name(StringFilter), |
| 32 | + color(StringFilter), |
| 33 | + And(Vec<Condition>), |
| 34 | + Or(Vec<Condition>), |
| 35 | +} |
| 36 | + |
| 37 | +type ConditionSource = LeftJoinQuerySource<bike::dsl::bike, color::dsl::color>; |
| 38 | +// Need this type for common condition expressions |
| 39 | +type BoxedCondition = Box<dyn BoxableExpression<ConditionSource, Sqlite, SqlType = Nullable<Bool>>>; |
| 40 | +type QuerySource = LeftJoin<bike::dsl::bike, color::dsl::color>; |
| 41 | +type BoxedQuery = IntoBoxed<'static, QuerySource, Sqlite>; |
| 42 | + |
| 43 | +impl Condition { |
| 44 | + fn to_boxed_condition(self) -> Option<BoxedCondition> { |
| 45 | + Some(match self { |
| 46 | + Condition::name(f) => string_filter!(f, bike::dsl::name), |
| 47 | + Condition::color(f) => string_filter!(f, color::dsl::name), |
| 48 | + Condition::And(conditions) => match create_filter(conditions, AndOr::And) { |
| 49 | + Some(boxed_condition) => boxed_condition, |
| 50 | + None => return None, |
| 51 | + }, |
| 52 | + Condition::Or(conditions) => match create_filter(conditions, AndOr::Or) { |
| 53 | + Some(boxed_condition) => boxed_condition, |
| 54 | + None => return None, |
| 55 | + }, |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// This method can also be made into a macro, but it should be fine to just duplicate |
| 61 | +fn create_filter(conditions: Vec<Condition>, and_or: AndOr) -> Option<BoxedCondition> { |
| 62 | + conditions |
| 63 | + .into_iter() |
| 64 | + // Map into array of boxed conditions |
| 65 | + .filter_map::<BoxedCondition, _>(Condition::to_boxed_condition) |
| 66 | + // Reduce to a boxed_condition1.and(boxed_condition2).and(boxed_condition3)... |
| 67 | + .fold(None, |boxed_conditions, boxed_condition| { |
| 68 | + Some(match boxed_conditions { |
| 69 | + Some(bc) => match and_or { |
| 70 | + AndOr::And => Box::new(bc.and(boxed_condition)), |
| 71 | + AndOr::Or => Box::new(bc.or(boxed_condition)), |
| 72 | + }, |
| 73 | + None => boxed_condition, |
| 74 | + }) |
| 75 | + }) |
| 76 | +} |
| 77 | +pub(super) fn create_filtered_query(conditions: Vec<Condition>) -> BoxedQuery { |
| 78 | + let boxed_query = bike::dsl::bike.left_join(color::dsl::color).into_boxed(); |
| 79 | + |
| 80 | + match create_filter(conditions, AndOr::And) { |
| 81 | + Some(boxed_conditions) => boxed_query.filter(boxed_conditions), |
| 82 | + None => boxed_query, |
| 83 | + } |
| 84 | +} |
0 commit comments