Skip to main content

cratestack_sql/values/
into_sql.rs

1use cratestack_core::Value;
2
3use super::sql_value::SqlValue;
4
5pub trait IntoSqlValue {
6    fn into_sql_value(self) -> SqlValue;
7}
8
9impl IntoSqlValue for bool {
10    fn into_sql_value(self) -> SqlValue {
11        SqlValue::Bool(self)
12    }
13}
14
15impl IntoSqlValue for i64 {
16    fn into_sql_value(self) -> SqlValue {
17        SqlValue::Int(self)
18    }
19}
20
21impl IntoSqlValue for f64 {
22    fn into_sql_value(self) -> SqlValue {
23        SqlValue::Float(self)
24    }
25}
26
27impl IntoSqlValue for String {
28    fn into_sql_value(self) -> SqlValue {
29        SqlValue::String(self)
30    }
31}
32
33impl IntoSqlValue for &str {
34    fn into_sql_value(self) -> SqlValue {
35        SqlValue::String(self.to_owned())
36    }
37}
38
39impl IntoSqlValue for uuid::Uuid {
40    fn into_sql_value(self) -> SqlValue {
41        SqlValue::Uuid(self)
42    }
43}
44
45impl IntoSqlValue for chrono::DateTime<chrono::Utc> {
46    fn into_sql_value(self) -> SqlValue {
47        SqlValue::DateTime(self)
48    }
49}
50
51impl IntoSqlValue for Value {
52    fn into_sql_value(self) -> SqlValue {
53        SqlValue::Json(self)
54    }
55}
56
57// One `impl` per concrete backend (cratestack#505 Direction 2) rather than
58// one `impl for cratestack_core::Decimal` — both may be active in the same
59// build now (see `cratestack-core/src/decimal.rs`'s module doc), and each
60// backend's concrete type boxes into the same `SqlValue::Decimal` variant.
61// Not a blanket `impl<D: DecimalValue> IntoSqlValue for D` because that
62// would overlap with the concrete impls above (e.g. `i64` already
63// satisfies `DecimalValue`'s bounds).
64#[cfg(feature = "decimal-rust-decimal")]
65impl IntoSqlValue for rust_decimal::Decimal {
66    fn into_sql_value(self) -> SqlValue {
67        SqlValue::Decimal(Box::new(self))
68    }
69}
70
71#[cfg(feature = "decimal-bigdecimal")]
72impl IntoSqlValue for bigdecimal::BigDecimal {
73    fn into_sql_value(self) -> SqlValue {
74        SqlValue::Decimal(Box::new(self))
75    }
76}
77
78impl IntoSqlValue for Vec<f32> {
79    fn into_sql_value(self) -> SqlValue {
80        SqlValue::Vector(self)
81    }
82}