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    NullBool,
14    NullInt,
15    NullFloat,
16    NullString,
17    NullBytes,
18    NullUuid,
19    NullDateTime,
20    NullJson,
21}
22
23#[derive(Debug, Clone, PartialEq)]
24pub(crate) enum FilterValue {
25    None,
26    Single(SqlValue),
27    Many(Vec<SqlValue>),
28}
29
30#[derive(Debug, Clone, PartialEq)]
31pub struct SqlColumnValue {
32    pub column: &'static str,
33    pub value: SqlValue,
34}
35
36pub trait CreateModelInput<M> {
37    fn sql_values(&self) -> Vec<SqlColumnValue>;
38}
39
40pub trait UpdateModelInput<M> {
41    fn sql_values(&self) -> Vec<SqlColumnValue>;
42}
43
44pub trait IntoSqlValue {
45    fn into_sql_value(self) -> SqlValue;
46}
47
48impl IntoSqlValue for bool {
49    fn into_sql_value(self) -> SqlValue {
50        SqlValue::Bool(self)
51    }
52}
53
54impl IntoSqlValue for i64 {
55    fn into_sql_value(self) -> SqlValue {
56        SqlValue::Int(self)
57    }
58}
59
60impl IntoSqlValue for f64 {
61    fn into_sql_value(self) -> SqlValue {
62        SqlValue::Float(self)
63    }
64}
65
66impl IntoSqlValue for String {
67    fn into_sql_value(self) -> SqlValue {
68        SqlValue::String(self)
69    }
70}
71
72impl IntoSqlValue for &str {
73    fn into_sql_value(self) -> SqlValue {
74        SqlValue::String(self.to_owned())
75    }
76}
77
78impl IntoSqlValue for uuid::Uuid {
79    fn into_sql_value(self) -> SqlValue {
80        SqlValue::Uuid(self)
81    }
82}
83
84impl IntoSqlValue for chrono::DateTime<chrono::Utc> {
85    fn into_sql_value(self) -> SqlValue {
86        SqlValue::DateTime(self)
87    }
88}
89
90impl IntoSqlValue for Value {
91    fn into_sql_value(self) -> SqlValue {
92        SqlValue::Json(self)
93    }
94}