Skip to main content

drizzle_sqlite/values/
update.rs

1//! Update value types for `SQLite`.
2//!
3//! Each field in an UPDATE operation can be skipped (left unchanged),
4//! set to NULL, or set to a value or expression.
5
6use crate::prelude::*;
7use drizzle_core::expr::Excluded;
8use drizzle_core::{Placeholder, SQL, SQLColumnInfo, SQLParam, TypedPlaceholder};
9
10use super::SQLiteValue;
11use super::insert::ValueWrapper;
12
13/// Represents a value for UPDATE operations that can be skipped, null, or a SQL expression.
14#[derive(Debug, Clone, Default)]
15#[allow(clippy::large_enum_variant)]
16pub enum SQLiteUpdateValue<'a, V: SQLParam, T> {
17    /// Don't include this column in the SET clause
18    #[default]
19    Skip,
20    /// Explicitly set column = NULL
21    Null,
22    /// Set column to a SQL expression (value, placeholder, etc.)
23    Value(ValueWrapper<'a, V, T>),
24}
25
26impl<V: SQLParam, T> SQLiteUpdateValue<'_, V, T> {
27    /// Returns true if this is `Skip`
28    pub const fn is_skip(&self) -> bool {
29        matches!(self, Self::Skip)
30    }
31}
32
33// Generic conversion from any type T that can convert to SQLiteValue
34impl<'a, T, U> From<T> for SQLiteUpdateValue<'a, SQLiteValue<'a>, U>
35where
36    T: TryInto<SQLiteValue<'a>> + TryInto<U>,
37    U: TryInto<SQLiteValue<'a>>,
38{
39    fn from(value: T) -> Self {
40        let sql = TryInto::<U>::try_into(value)
41            .map(|v| v.try_into().unwrap_or_default())
42            .map_or_else(
43                |_| SQL::from(SQLiteValue::Null),
44                |v: SQLiteValue<'a>| SQL::from(v),
45            );
46        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(sql))
47    }
48}
49
50// Placeholder conversion
51impl<'a, T> From<Placeholder> for SQLiteUpdateValue<'a, SQLiteValue<'a>, T> {
52    fn from(placeholder: Placeholder) -> Self {
53        use drizzle_core::{Param, SQLChunk};
54        let chunk = SQLChunk::Param(Param {
55            placeholder,
56            value: None,
57        });
58        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(
59            core::iter::once(chunk).collect(),
60        ))
61    }
62}
63
64impl<'a, M: drizzle_core::types::DataType, N: drizzle_core::expr::Nullability, T>
65    From<TypedPlaceholder<M, N>> for SQLiteUpdateValue<'a, SQLiteValue<'a>, T>
66{
67    fn from(typed: TypedPlaceholder<M, N>) -> Self {
68        Placeholder::from(typed).into()
69    }
70}
71
72// Excluded column reference conversion (for ON CONFLICT DO UPDATE SET)
73impl<'a, C, T> From<Excluded<C>> for SQLiteUpdateValue<'a, SQLiteValue<'a>, T>
74where
75    C: SQLColumnInfo,
76{
77    fn from(excluded: Excluded<C>) -> Self {
78        use drizzle_core::ToSQL;
79        let sql = excluded.to_sql();
80        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(sql))
81    }
82}
83
84// Array conversion for Vec<u8> UpdateValue
85impl<'a, const N: usize> From<[u8; N]> for SQLiteUpdateValue<'a, SQLiteValue<'a>, Vec<u8>> {
86    fn from(value: [u8; N]) -> Self {
87        let sqlite_value = SQLiteValue::Blob(crate::prelude::Cow::Owned(value.to_vec()));
88        let sql = SQL::param(sqlite_value);
89        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, Vec<u8>>::new(sql))
90    }
91}