Skip to main content

drizzle_sqlite/values/
update.rs

1//! Update value types for SQLite
2//!
3//! Mirrors the `SQLiteInsertValue` pattern but simplified for UPDATE operations.
4//! All UPDATE fields are optional (Skip = don't include in SET clause).
5
6use drizzle_core::{Placeholder, SQL, SQLParam};
7
8use super::SQLiteValue;
9use super::insert::ValueWrapper;
10
11/// Represents a value for UPDATE operations that can be skipped, null, or a SQL expression.
12#[derive(Debug, Clone, Default)]
13#[allow(clippy::large_enum_variant)]
14pub enum SQLiteUpdateValue<'a, V: SQLParam, T> {
15    /// Don't include this column in the SET clause
16    #[default]
17    Skip,
18    /// Explicitly set column = NULL
19    Null,
20    /// Set column to a SQL expression (value, placeholder, etc.)
21    Value(ValueWrapper<'a, V, T>),
22}
23
24impl<'a, V: SQLParam, T> SQLiteUpdateValue<'a, V, T> {
25    /// Returns true if this is `Skip`
26    pub fn is_skip(&self) -> bool {
27        matches!(self, Self::Skip)
28    }
29}
30
31// Generic conversion from any type T that can convert to SQLiteValue
32impl<'a, T, U> From<T> for SQLiteUpdateValue<'a, SQLiteValue<'a>, U>
33where
34    T: TryInto<SQLiteValue<'a>>,
35    T: TryInto<U>,
36    U: TryInto<SQLiteValue<'a>>,
37{
38    fn from(value: T) -> Self {
39        let sql = TryInto::<U>::try_into(value)
40            .map(|v| v.try_into().unwrap_or_default())
41            .map(|v: SQLiteValue<'a>| SQL::from(v))
42            .unwrap_or_else(|_| SQL::from(SQLiteValue::Null));
43        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(sql))
44    }
45}
46
47// Placeholder conversion
48impl<'a, T> From<Placeholder> for SQLiteUpdateValue<'a, SQLiteValue<'a>, T> {
49    fn from(placeholder: Placeholder) -> Self {
50        use drizzle_core::{Param, SQLChunk};
51        let chunk = SQLChunk::Param(Param {
52            placeholder,
53            value: None,
54        });
55        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(
56            std::iter::once(chunk).collect(),
57        ))
58    }
59}
60
61// Array conversion for Vec<u8> UpdateValue
62impl<'a, const N: usize> From<[u8; N]> for SQLiteUpdateValue<'a, SQLiteValue<'a>, Vec<u8>> {
63    fn from(value: [u8; N]) -> Self {
64        let sqlite_value = SQLiteValue::Blob(std::borrow::Cow::Owned(value.to_vec()));
65        let sql = SQL::param(sqlite_value);
66        SQLiteUpdateValue::Value(ValueWrapper::<SQLiteValue<'a>, Vec<u8>>::new(sql))
67    }
68}