Skip to main content

cratestack_sqlx/
values.rs

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(crate) 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    /// Run schema-derived validators (`@length`, `@email`, `@regex`, ...) on
41    /// the input. Default impl is a no-op for inputs without validators.
42    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 IntoSqlValue {
55    fn into_sql_value(self) -> SqlValue;
56}
57
58impl IntoSqlValue for bool {
59    fn into_sql_value(self) -> SqlValue {
60        SqlValue::Bool(self)
61    }
62}
63
64impl IntoSqlValue for i64 {
65    fn into_sql_value(self) -> SqlValue {
66        SqlValue::Int(self)
67    }
68}
69
70impl IntoSqlValue for f64 {
71    fn into_sql_value(self) -> SqlValue {
72        SqlValue::Float(self)
73    }
74}
75
76impl IntoSqlValue for String {
77    fn into_sql_value(self) -> SqlValue {
78        SqlValue::String(self)
79    }
80}
81
82impl IntoSqlValue for &str {
83    fn into_sql_value(self) -> SqlValue {
84        SqlValue::String(self.to_owned())
85    }
86}
87
88impl IntoSqlValue for uuid::Uuid {
89    fn into_sql_value(self) -> SqlValue {
90        SqlValue::Uuid(self)
91    }
92}
93
94impl IntoSqlValue for chrono::DateTime<chrono::Utc> {
95    fn into_sql_value(self) -> SqlValue {
96        SqlValue::DateTime(self)
97    }
98}
99
100impl IntoSqlValue for Value {
101    fn into_sql_value(self) -> SqlValue {
102        SqlValue::Json(self)
103    }
104}
105
106impl IntoSqlValue for cratestack_core::Decimal {
107    fn into_sql_value(self) -> SqlValue {
108        SqlValue::Decimal(self)
109    }
110}