drizzle_postgres/values/
update.rs1use super::PostgresValue;
7use super::insert::ValueWrapper;
8use crate::prelude::*;
9use drizzle_core::expr::Excluded;
10use drizzle_core::{
11 SQLColumnInfo, TypedPlaceholder, param::Param, placeholder::Placeholder, sql::SQL,
12 sql::SQLChunk, traits::SQLParam,
13};
14
15#[cfg(feature = "uuid")]
16use uuid::Uuid;
17
18#[derive(Debug, Clone, Default)]
20#[allow(clippy::large_enum_variant)]
21pub enum PostgresUpdateValue<'a, V: SQLParam, T> {
22 #[default]
24 Skip,
25 Null,
27 Value(ValueWrapper<'a, V, T>),
29}
30
31impl<V: SQLParam, T> PostgresUpdateValue<'_, V, T> {
32 pub const fn is_skip(&self) -> bool {
34 matches!(self, Self::Skip)
35 }
36}
37
38impl<'a, T> From<T> for PostgresUpdateValue<'a, PostgresValue<'a>, T>
40where
41 T: TryInto<PostgresValue<'a>>,
42{
43 fn from(value: T) -> Self {
44 let sql = value.try_into().map_or_else(
45 |_| SQL::from(PostgresValue::Null),
46 |v: PostgresValue<'a>| SQL::from(v),
47 );
48 PostgresUpdateValue::Value(ValueWrapper::<PostgresValue<'a>, T>::new(sql))
49 }
50}
51
52impl<'a> From<&str> for PostgresUpdateValue<'a, PostgresValue<'a>, String> {
54 fn from(value: &str) -> Self {
55 let postgres_value = SQL::param(Cow::Owned(PostgresValue::from(value.to_string())));
56 PostgresUpdateValue::Value(ValueWrapper::<PostgresValue<'a>, String>::new(
57 postgres_value,
58 ))
59 }
60}
61
62impl<'a, T> From<Placeholder> for PostgresUpdateValue<'a, PostgresValue<'a>, T> {
64 fn from(placeholder: Placeholder) -> Self {
65 let chunk = SQLChunk::Param(Param {
66 placeholder,
67 value: None,
68 });
69 PostgresUpdateValue::Value(ValueWrapper::<PostgresValue<'a>, T>::new(
70 core::iter::once(chunk).collect(),
71 ))
72 }
73}
74
75impl<'a, M: drizzle_core::types::DataType, N: drizzle_core::expr::Nullability, T>
76 From<TypedPlaceholder<M, N>> for PostgresUpdateValue<'a, PostgresValue<'a>, T>
77{
78 fn from(typed: TypedPlaceholder<M, N>) -> Self {
79 Placeholder::from(typed).into()
80 }
81}
82
83impl<'a, C, T> From<Excluded<C>> for PostgresUpdateValue<'a, PostgresValue<'a>, T>
85where
86 C: SQLColumnInfo,
87{
88 fn from(excluded: Excluded<C>) -> Self {
89 use drizzle_core::ToSQL;
90 let sql = excluded.to_sql();
91 PostgresUpdateValue::Value(ValueWrapper::<PostgresValue<'a>, T>::new(sql))
92 }
93}
94
95#[cfg(feature = "uuid")]
97impl<'a> From<Uuid> for PostgresUpdateValue<'a, PostgresValue<'a>, String> {
98 fn from(value: Uuid) -> Self {
99 let postgres_value = PostgresValue::Uuid(value);
100 let sql = SQL::param(postgres_value);
101 PostgresUpdateValue::Value(ValueWrapper::<PostgresValue<'a>, String>::new(sql))
102 }
103}
104
105#[cfg(feature = "uuid")]
106impl<'a> From<&'a Uuid> for PostgresUpdateValue<'a, PostgresValue<'a>, String> {
107 fn from(value: &'a Uuid) -> Self {
108 let postgres_value = PostgresValue::Uuid(*value);
109 let sql = SQL::param(postgres_value);
110 PostgresUpdateValue::Value(ValueWrapper::<PostgresValue<'a>, String>::new(sql))
111 }
112}