drizzle_sqlite/values/
insert.rs

1//------------------------------------------------------------------------------
2// InsertValue Definition - SQL-based value for inserts
3//------------------------------------------------------------------------------
4
5use std::marker::PhantomData;
6
7use drizzle_core::{Placeholder, SQL, SQLParam};
8
9use crate::{OwnedSQLiteValue, SQLiteValue};
10
11/// Wrapper for SQL with type information
12#[derive(Debug, Clone)]
13pub struct ValueWrapper<'a, V: SQLParam, T> {
14    pub value: SQL<'a, V>,
15    pub _phantom: PhantomData<T>,
16}
17
18impl<'a, V: SQLParam, T> ValueWrapper<'a, V, T> {
19    pub const fn new<U>(value: SQL<'a, V>) -> ValueWrapper<'a, V, U> {
20        ValueWrapper {
21            value,
22            _phantom: PhantomData,
23        }
24    }
25}
26
27/// Represents a value for INSERT operations that can be omitted, null, or a SQL expression
28#[derive(Debug, Clone, Default)]
29pub enum SQLiteInsertValue<'a, V: SQLParam, T> {
30    /// Omit this column from the INSERT (use database default)
31    #[default]
32    Omit,
33    /// Explicitly insert NULL
34    Null,
35    /// Insert a SQL expression (value, placeholder, etc.)
36    Value(ValueWrapper<'a, V, T>),
37}
38
39impl<'a, T> SQLiteInsertValue<'a, SQLiteValue<'a>, T> {
40    /// Converts this InsertValue to an owned version with 'static lifetime
41    pub fn into_owned(self) -> SQLiteInsertValue<'static, SQLiteValue<'static>, T> {
42        match self {
43            SQLiteInsertValue::Omit => SQLiteInsertValue::Omit,
44            SQLiteInsertValue::Null => SQLiteInsertValue::Null,
45            SQLiteInsertValue::Value(wrapper) => {
46                // Extract the parameter value, convert to owned, then back to static SQLiteValue
47                if let Some(drizzle_core::SQLChunk::Param(param)) = wrapper.value.chunks.first() {
48                    if let Some(ref val) = param.value {
49                        let owned_val = OwnedSQLiteValue::from(val.as_ref().clone());
50                        let static_val: SQLiteValue<'static> = owned_val.into();
51                        let static_sql = drizzle_core::SQL::param(static_val);
52                        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'static>, T>::new(
53                            static_sql,
54                        ))
55                    } else {
56                        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'static>, T>::new(
57                            drizzle_core::SQL::param(SQLiteValue::Null),
58                        ))
59                    }
60                } else {
61                    SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'static>, T>::new(
62                        drizzle_core::SQL::param(SQLiteValue::Null),
63                    ))
64                }
65            }
66        }
67    }
68}
69
70impl<'a, T, U> From<T> for SQLiteInsertValue<'a, SQLiteValue<'a>, U>
71where
72    T: TryInto<SQLiteValue<'a>>,
73    T: TryInto<U>,
74    U: TryInto<SQLiteValue<'a>>,
75{
76    fn from(value: T) -> Self {
77        let sql = TryInto::<U>::try_into(value)
78            .map(|v| v.try_into().unwrap_or_default())
79            .map(|v: SQLiteValue<'a>| SQL::from(v))
80            .unwrap_or_else(|_| SQL::from(SQLiteValue::Null));
81        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(sql))
82    }
83}
84
85impl<'a, T> From<Placeholder> for SQLiteInsertValue<'a, SQLiteValue<'a>, T> {
86    fn from(placeholder: Placeholder) -> Self {
87        use drizzle_core::{Param, SQLChunk};
88        let chunk = SQLChunk::Param(Param {
89            placeholder,
90            value: None,
91        });
92        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(
93            std::iter::once(chunk).collect(),
94        ))
95    }
96}
97
98// Array conversion for Vec<u8> InsertValue - support flexible input types
99impl<'a, const N: usize> From<[u8; N]> for SQLiteInsertValue<'a, SQLiteValue<'a>, Vec<u8>> {
100    fn from(value: [u8; N]) -> Self {
101        let sqlite_value = SQLiteValue::Blob(std::borrow::Cow::Owned(value.to_vec()));
102        let sql = SQL::param(sqlite_value);
103        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'a>, Vec<u8>>::new(sql))
104    }
105}