1use cratestack_core::Value;
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum SqlValue {
5 Bool(bool),
6 Int(i64),
7 Float(f64),
8 String(String),
9 Bytes(Vec<u8>),
10 Uuid(uuid::Uuid),
11 DateTime(chrono::DateTime<chrono::Utc>),
12 Json(Value),
13 Decimal(cratestack_core::Decimal),
14 NullBool,
15 NullInt,
16 NullFloat,
17 NullString,
18 NullBytes,
19 NullUuid,
20 NullDateTime,
21 NullJson,
22 NullDecimal,
23}
24
25#[derive(Debug, Clone, PartialEq)]
26pub enum FilterValue {
27 None,
28 Single(SqlValue),
29 Many(Vec<SqlValue>),
30}
31
32#[derive(Debug, Clone, PartialEq)]
33pub struct SqlColumnValue {
34 pub column: &'static str,
35 pub value: SqlValue,
36}
37
38pub trait CreateModelInput<M> {
39 fn sql_values(&self) -> Vec<SqlColumnValue>;
40 fn validate(&self) -> Result<(), cratestack_core::CoolError> {
43 Ok(())
44 }
45}
46
47pub trait UpdateModelInput<M> {
48 fn sql_values(&self) -> Vec<SqlColumnValue>;
49 fn validate(&self) -> Result<(), cratestack_core::CoolError> {
50 Ok(())
51 }
52}
53
54pub trait UpsertModelInput<M>: Send {
66 fn sql_values(&self) -> Vec<SqlColumnValue>;
68
69 fn primary_key_value(&self) -> SqlValue;
73
74 fn validate(&self) -> Result<(), cratestack_core::CoolError> {
75 Ok(())
76 }
77}
78
79pub trait IntoSqlValue {
80 fn into_sql_value(self) -> SqlValue;
81}
82
83impl IntoSqlValue for bool {
84 fn into_sql_value(self) -> SqlValue {
85 SqlValue::Bool(self)
86 }
87}
88
89impl IntoSqlValue for i64 {
90 fn into_sql_value(self) -> SqlValue {
91 SqlValue::Int(self)
92 }
93}
94
95impl IntoSqlValue for f64 {
96 fn into_sql_value(self) -> SqlValue {
97 SqlValue::Float(self)
98 }
99}
100
101impl IntoSqlValue for String {
102 fn into_sql_value(self) -> SqlValue {
103 SqlValue::String(self)
104 }
105}
106
107impl IntoSqlValue for &str {
108 fn into_sql_value(self) -> SqlValue {
109 SqlValue::String(self.to_owned())
110 }
111}
112
113impl IntoSqlValue for uuid::Uuid {
114 fn into_sql_value(self) -> SqlValue {
115 SqlValue::Uuid(self)
116 }
117}
118
119impl IntoSqlValue for chrono::DateTime<chrono::Utc> {
120 fn into_sql_value(self) -> SqlValue {
121 SqlValue::DateTime(self)
122 }
123}
124
125impl IntoSqlValue for Value {
126 fn into_sql_value(self) -> SqlValue {
127 SqlValue::Json(self)
128 }
129}
130
131impl IntoSqlValue for cratestack_core::Decimal {
132 fn into_sql_value(self) -> SqlValue {
133 SqlValue::Decimal(self)
134 }
135}