1use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
4use queryweave::Value;
5use sqlx::query_builder::Separated;
6use sqlx::{Database, Encode, QueryBuilder, Type};
7
8pub trait BindValues: Database {
19 fn push_bind_value(query_builder: &mut QueryBuilder<'_, Self>, value: Value);
22
23 fn push_bind_value_unseparated<Sep: std::fmt::Display>(
26 separated: &mut Separated<'_, '_, Self, Sep>,
27 value: Value,
28 );
29}
30
31macro_rules! bind_value {
34 ($target:expr, $bind:ident, $value:expr) => {
35 match $value {
36 Value::Bool(value) => $target.$bind(value),
37 Value::I16(value) => $target.$bind(value),
38 Value::I32(value) => $target.$bind(value),
39 Value::I64(value) => $target.$bind(value),
40 Value::F32(value) => $target.$bind(value),
41 Value::F64(value) => $target.$bind(value),
42 Value::Text(value) => $target.$bind(value),
43 Value::DateTimeUtc(value) => $target.$bind(value),
44 Value::NaiveDateTime(value) => $target.$bind(value),
45 Value::NaiveDate(value) => $target.$bind(value),
46 Value::NaiveTime(value) => $target.$bind(value),
47 }
48 };
49}
50
51impl<DB> BindValues for DB
54where
55 DB: Database,
56 for<'q> Option<bool>: Encode<'q, DB> + Type<DB>,
57 for<'q> Option<i16>: Encode<'q, DB> + Type<DB>,
58 for<'q> Option<i32>: Encode<'q, DB> + Type<DB>,
59 for<'q> Option<i64>: Encode<'q, DB> + Type<DB>,
60 for<'q> Option<f32>: Encode<'q, DB> + Type<DB>,
61 for<'q> Option<f64>: Encode<'q, DB> + Type<DB>,
62 for<'q> Option<String>: Encode<'q, DB> + Type<DB>,
63 for<'q> Option<DateTime<Utc>>: Encode<'q, DB> + Type<DB>,
64 for<'q> Option<NaiveDateTime>: Encode<'q, DB> + Type<DB>,
65 for<'q> Option<NaiveDate>: Encode<'q, DB> + Type<DB>,
66 for<'q> Option<NaiveTime>: Encode<'q, DB> + Type<DB>,
67{
68 fn push_bind_value(query_builder: &mut QueryBuilder<'_, Self>, value: Value) {
69 bind_value!(query_builder, push_bind, value);
70 }
71
72 fn push_bind_value_unseparated<Sep: std::fmt::Display>(
73 separated: &mut Separated<'_, '_, Self, Sep>,
74 value: Value,
75 ) {
76 bind_value!(separated, push_bind_unseparated, value);
77 }
78}