drizzle_sqlite/builder/
update.rs

1use crate::values::SQLiteValue;
2use drizzle_core::{SQL, SQLTable, ToSQL};
3use std::fmt::Debug;
4use std::marker::PhantomData;
5
6// Import the ExecutableState trait
7use super::ExecutableState;
8
9//------------------------------------------------------------------------------
10// Type State Markers
11//------------------------------------------------------------------------------
12
13/// Marker for the initial state of UpdateBuilder
14#[derive(Debug, Clone, Copy, Default)]
15pub struct UpdateInitial;
16
17/// Marker for the state after SET clause
18#[derive(Debug, Clone, Copy, Default)]
19pub struct UpdateSetClauseSet;
20
21/// Marker for the state after WHERE clause
22#[derive(Debug, Clone, Copy, Default)]
23pub struct UpdateWhereSet;
24
25/// Marker for the state after RETURNING clause
26#[derive(Debug, Clone, Copy, Default)]
27pub struct UpdateReturningSet;
28
29// Mark states that can execute update queries
30impl ExecutableState for UpdateSetClauseSet {}
31impl ExecutableState for UpdateWhereSet {}
32impl ExecutableState for UpdateReturningSet {}
33
34//------------------------------------------------------------------------------
35// UpdateBuilder Definition
36//------------------------------------------------------------------------------
37
38/// Builds an UPDATE query specifically for SQLite
39pub type UpdateBuilder<'a, Schema, State, Table> = super::QueryBuilder<'a, Schema, State, Table>;
40
41//------------------------------------------------------------------------------
42// Initial State Implementation
43//------------------------------------------------------------------------------
44
45impl<'a, Schema, Table> UpdateBuilder<'a, Schema, UpdateInitial, Table>
46where
47    Table: SQLTable<'a, SQLiteValue<'a>>,
48{
49    /// Sets the values to update and transitions to the SetClauseSet state
50    #[inline]
51    pub fn set(
52        self,
53        values: Table::Update,
54    ) -> UpdateBuilder<'a, Schema, UpdateSetClauseSet, Table> {
55        let sql = crate::helpers::set::<'a, Table, SQLiteValue<'a>>(values);
56        UpdateBuilder {
57            sql: self.sql.append(sql),
58            schema: PhantomData,
59            state: PhantomData,
60            table: PhantomData,
61        }
62    }
63}
64
65//------------------------------------------------------------------------------
66// Post-SET Implementation
67//------------------------------------------------------------------------------
68
69impl<'a, S, T> UpdateBuilder<'a, S, UpdateSetClauseSet, T> {
70    /// Adds a WHERE condition and transitions to the WhereSet state
71    #[inline]
72    pub fn r#where(
73        self,
74        condition: SQL<'a, SQLiteValue<'a>>,
75    ) -> UpdateBuilder<'a, S, UpdateWhereSet, T> {
76        let where_sql = crate::helpers::r#where(condition);
77        UpdateBuilder {
78            sql: self.sql.append(where_sql),
79            schema: PhantomData,
80            state: PhantomData,
81            table: PhantomData,
82        }
83    }
84
85    /// Adds a RETURNING clause and transitions to the ReturningSet state
86    #[inline]
87    pub fn returning(
88        self,
89        columns: impl ToSQL<'a, SQLiteValue<'a>>,
90    ) -> UpdateBuilder<'a, S, UpdateReturningSet, T> {
91        let returning_sql = crate::helpers::returning(columns);
92        UpdateBuilder {
93            sql: self.sql.append(returning_sql),
94            schema: PhantomData,
95            state: PhantomData,
96            table: PhantomData,
97        }
98    }
99}
100
101//------------------------------------------------------------------------------
102// Post-WHERE Implementation
103//------------------------------------------------------------------------------
104
105impl<'a, S, T> UpdateBuilder<'a, S, UpdateWhereSet, T> {
106    /// Adds a RETURNING clause after WHERE
107    #[inline]
108    pub fn returning(
109        self,
110        columns: impl ToSQL<'a, SQLiteValue<'a>>,
111    ) -> UpdateBuilder<'a, S, UpdateReturningSet, T> {
112        let returning_sql = crate::helpers::returning(columns);
113        UpdateBuilder {
114            sql: self.sql.append(returning_sql),
115            schema: PhantomData,
116            state: PhantomData,
117            table: PhantomData,
118        }
119    }
120}