taitan_orm_trait/logic/
location_not.rs

1use crate::traits::{Location, LocationKind, Parameter};
2use sqlx::Database;
3use std::borrow::Cow;
4use std::fmt::{Debug, Display, Formatter};
5use std::marker::PhantomData;
6
7pub struct Not<DB, T>
8where
9    DB: Database,
10    T: Location<DB> + Debug,
11{
12    pub expr: T,
13    _marker: PhantomData<DB>,
14}
15
16impl<DB, T> Not<DB, T>
17where
18    DB: Database,
19    T: Location<DB> + Debug,
20{
21    pub fn new(expr: T) -> Self {
22        Self {
23            expr,
24            _marker: PhantomData,
25        }
26    }
27}
28
29impl<DB, T> Debug for Not<DB, T>
30where
31    DB: Database,
32    T: Location<DB> + Debug,
33{
34    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35        write!(f, "(NOT {:?})", self.expr)
36    }
37}
38
39impl<DB, T> Parameter<DB> for Not<DB, T>
40where
41    DB: Database,
42    T: Location<DB> + Debug,
43{
44    fn add_to_args<'a, 'b>(
45        &'a self,
46        args: &'b mut <DB as Database>::Arguments<'a>,
47    ) -> crate::result::Result<()> {
48        if !self.expr.all_none() {
49            self.expr.add_to_args(args)?;
50        }
51        Ok(())
52    }
53}
54
55impl<DB, T> Location<DB> for Not<DB, T>
56where
57    DB: Database,
58    T: Location<DB> + Debug,
59{
60    fn table_name(&self) -> Cow<'static, str> {
61        self.expr.table_name()
62    }
63    fn kind(&self) -> LocationKind {
64        LocationKind::Not
65    }
66    fn gen_where_sql<'a>(&self) -> Cow<'a, str> {
67        if self.expr.all_none() {
68            self.expr.gen_where_sql()
69        } else {
70            format!("(NOT {})", self.expr.gen_where_sql(),).into()
71        }
72    }
73
74    // fn add_to_args<'a, 'b>(
75    //     &'a self,
76    //     args: &'b mut DB::Arguments<'a>,
77    // ) -> crate::brave_new::result::Result<()> {
78    //     if !self.expr.all_none() {
79    //         self.expr.add_args(args)?;
80    //     }
81    //     Ok(())
82    // }
83
84    fn all_none(&self) -> bool {
85        self.expr.all_none()
86    }
87}