drizzle_postgres/builder/
delete.rs

1use crate::ToPostgresSQL;
2use std::fmt::Debug;
3use std::marker::PhantomData;
4
5// Import the ExecutableState trait
6use super::ExecutableState;
7
8//------------------------------------------------------------------------------
9// Type State Markers
10//------------------------------------------------------------------------------
11
12/// Marker for the initial state of DeleteBuilder
13#[derive(Debug, Clone, Copy, Default)]
14pub struct DeleteInitial;
15
16/// Marker for the state after WHERE clause
17#[derive(Debug, Clone, Copy, Default)]
18pub struct DeleteWhereSet;
19
20/// Marker for the state after RETURNING clause
21#[derive(Debug, Clone, Copy, Default)]
22pub struct DeleteReturningSet;
23
24// Mark states that can execute delete queries
25impl ExecutableState for DeleteInitial {}
26impl ExecutableState for DeleteWhereSet {}
27impl ExecutableState for DeleteReturningSet {}
28
29//------------------------------------------------------------------------------
30// DeleteBuilder Definition
31//------------------------------------------------------------------------------
32
33/// Builds a DELETE query specifically for PostgreSQL
34pub type DeleteBuilder<'a, Schema, State, Table> = super::QueryBuilder<'a, Schema, State, Table>;
35
36//------------------------------------------------------------------------------
37// Initial State Implementation
38//------------------------------------------------------------------------------
39
40impl<'a, S, T> DeleteBuilder<'a, S, DeleteInitial, T> {
41    /// Adds a WHERE condition to the query
42    #[inline]
43    pub fn r#where(
44        self,
45        condition: impl ToPostgresSQL<'a>,
46    ) -> DeleteBuilder<'a, S, DeleteWhereSet, T> {
47        let where_sql = crate::helpers::r#where(condition.to_sql());
48        DeleteBuilder {
49            sql: self.sql.append(where_sql),
50            schema: PhantomData,
51            state: PhantomData,
52            table: PhantomData,
53        }
54    }
55
56    /// Adds a RETURNING clause to the query
57    #[inline]
58    pub fn returning(
59        self,
60        columns: impl ToPostgresSQL<'a>,
61    ) -> DeleteBuilder<'a, S, DeleteReturningSet, T> {
62        let returning_sql = crate::helpers::returning(columns);
63        DeleteBuilder {
64            sql: self.sql.append(returning_sql),
65            schema: PhantomData,
66            state: PhantomData,
67            table: PhantomData,
68        }
69    }
70}
71
72//------------------------------------------------------------------------------
73// Post-WHERE Implementation
74//------------------------------------------------------------------------------
75
76impl<'a, S, T> DeleteBuilder<'a, S, DeleteWhereSet, T> {
77    /// Adds a RETURNING clause after WHERE
78    #[inline]
79    pub fn returning(
80        self,
81        columns: impl ToPostgresSQL<'a>,
82    ) -> DeleteBuilder<'a, S, DeleteReturningSet, T> {
83        let returning_sql = crate::helpers::returning(columns);
84        DeleteBuilder {
85            sql: self.sql.append(returning_sql),
86            schema: PhantomData,
87            state: PhantomData,
88            table: PhantomData,
89        }
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use drizzle_core::{SQL, ToSQL};
97
98    #[test]
99    fn test_delete_builder_creation() {
100        let builder = DeleteBuilder::<(), DeleteInitial, ()> {
101            sql: SQL::raw("DELETE FROM test"),
102            schema: PhantomData,
103            state: PhantomData,
104            table: PhantomData,
105        };
106
107        assert_eq!(builder.to_sql().sql(), "DELETE FROM test");
108    }
109}