Skip to main content

a3s_orm/query/
delete.rs

1use std::marker::PhantomData;
2
3use crate::ast::{DeleteNode, QueryNode, TableNode};
4use crate::expression::{Expression, Selection};
5use crate::schema::Table;
6
7use super::Query;
8
9#[derive(Clone, Debug)]
10pub struct DeleteQuery<T: Table, O = ()> {
11    node: DeleteNode,
12    marker: PhantomData<fn() -> (T, O)>,
13}
14
15pub fn delete_from<T: Table>() -> DeleteQuery<T> {
16    DeleteQuery {
17        node: DeleteNode {
18            table: TableNode {
19                name: T::NAME,
20                alias: None,
21            },
22            filter: None,
23            returning: Vec::new(),
24        },
25        marker: PhantomData,
26    }
27}
28
29impl<T: Table, O> DeleteQuery<T, O> {
30    pub fn filter(mut self, expression: Expression) -> Self {
31        self.node.filter = Some(match self.node.filter.take() {
32            Some(existing) => existing.and(expression),
33            None => expression,
34        });
35        self
36    }
37
38    pub fn returning<S: Selection>(mut self, selection: S) -> DeleteQuery<T, S::Output> {
39        self.node.returning.extend(selection.expressions());
40        DeleteQuery {
41            node: self.node,
42            marker: PhantomData,
43        }
44    }
45}
46
47impl<T: Table, O> Query for DeleteQuery<T, O> {
48    type Output = O;
49
50    fn compile(self, dialect: &impl crate::Dialect) -> crate::Result<crate::CompiledQuery> {
51        crate::compiler::compile(QueryNode::Delete(self.node), dialect)
52    }
53}