1use rbs::to_value;
2use rbs::Value as RbValue;
3use sea_query::*;
4pub trait SqlxBinder {
5 fn build_rbs<T: QueryBuilder>(&self, query_builder: T) -> (String, Vec<rbs::Value>);
6 fn build_any_rbs(&self, query_builder: &dyn QueryBuilder) -> (String, Vec<rbs::Value>);
7}
8
9macro_rules! impl_rbs_binder {
10 ($l:ident) => {
11 impl SqlxBinder for $l {
12 fn build_rbs<T: QueryBuilder>(&self, query_builder: T) -> (String, Vec<rbs::Value>) {
13 let (query, values) = self.build(query_builder);
14 (query, to_rb_values(values))
15 }
16
17 fn build_any_rbs(&self, query_builder: &dyn QueryBuilder) -> (String, Vec<rbs::Value>) {
18 let (query, values) = self.build_any(query_builder);
19 (query, to_rb_values(values))
20 }
21 }
22 };
23}
24
25impl_rbs_binder!(SelectStatement);
26impl_rbs_binder!(UpdateStatement);
27impl_rbs_binder!(InsertStatement);
28impl_rbs_binder!(DeleteStatement);
29impl_rbs_binder!(WithQuery);
30trait ToRbV {
31 fn to(self) -> RbValue;
32}
33
34impl<T> ToRbV for Option<T>
35where
36 T: Into<RbValue>,
37{
38 fn to(self) -> RbValue {
39 match self {
40 Some(v) => v.into(),
41 None => RbValue::Null,
42 }
43 }
44}
45
46fn to_rb_values(values: Values) -> Vec<rbs::Value> {
47 let mut args: Vec<rbs::Value> = vec![];
48 for arg in values {
49 match arg {
50 Value::Bool(v) => args.push(v.to()),
51 Value::TinyInt(v) => args.push(v.to()),
52 Value::SmallInt(v) => args.push(v.to()),
53 Value::Int(v) => args.push(v.to()),
54 Value::BigInt(v) => args.push(v.to()),
55 Value::TinyUnsigned(v) => args.push(v.to()),
56 Value::SmallUnsigned(v) => args.push(v.to()),
57 Value::Unsigned(v) => args.push(v.to()),
58 Value::BigUnsigned(v) => args.push(v.to()),
59 Value::Float(v) => args.push(v.to()),
60 Value::Double(v) => args.push(v.to()),
61 Value::String(v) => {
62 let d = match v {
63 Some(v) => v.to_string().into(),
64 None => RbValue::Null,
65 };
66 args.push(d)
67 }
68 Value::Char(v) => args.push(to_value!(v)),
69 Value::Bytes(v) => args.push(to_value!(v)),
70 #[cfg(feature = "with-chrono")]
71 Value::ChronoDate(v) => args.push(to_value!(
72 Value::ChronoDate(v).chrono_as_naive_utc_in_string()
73 )),
74 #[cfg(feature = "with-chrono")]
75 Value::ChronoTime(v) => {
76 args.push(to_value!(
77 Value::ChronoTime(v).chrono_as_naive_utc_in_string()
78 ));
79 }
80 #[cfg(feature = "with-chrono")]
81 Value::ChronoDateTime(t) => {
82 args.push(Value::ChronoDateTime(t).chrono_as_naive_utc_in_string().to());
83 }
84 #[cfg(feature = "with-chrono")]
85 Value::ChronoDateTimeUtc(t) => {
86 args.push(Value::ChronoDateTimeUtc(t).chrono_as_naive_utc_in_string().to());
87 }
88 #[cfg(feature = "with-chrono")]
89 Value::ChronoDateTimeLocal(t) => {
90 args.push(Value::ChronoDateTimeLocal(t).chrono_as_naive_utc_in_string().to());
91 }
92 #[cfg(feature = "with-chrono")]
93 Value::ChronoDateTimeWithTimeZone(t) => {
94 args.push(Value::ChronoDateTimeWithTimeZone(t).chrono_as_naive_utc_in_string().to());
95 }
96 #[cfg(feature = "with-time")]
97 Value::TimeDate(t) => {
98 args.push(Value::TimeDate(t).time_as_naive_utc_in_string().to());
99 }
100 #[cfg(feature = "with-time")]
101 Value::TimeTime(t) => {
102 args.push(Value::TimeTime(t).time_as_naive_utc_in_string().to());
103 }
104 #[cfg(feature = "with-time")]
105 Value::TimeDateTime(t) => {
106 args.push(Value::TimeDateTime(t).time_as_naive_utc_in_string().to());
107 }
108 #[cfg(feature = "with-time")]
109 Value::TimeDateTimeWithTimeZone(t) => {
110 args.push(Value::TimeDateTimeWithTimeZone(t).time_as_naive_utc_in_string().to());
111 }
112 #[cfg(feature = "with-uuid")]
113 Value::Uuid(uuid) => {
114 args.push(Value::Uuid(uuid).to_string().into());
115 }
116 #[cfg(feature = "with-rust_decimal")]
117 Value::Decimal(_) => {
118 panic!("rbs doesn't support Decimal arguments");
119 }
120 #[cfg(feature = "with-bigdecimal")]
121 Value::BigDecimal(d) => {
122 args.push(to_value!(Value::BigDecimal(d).to_string()));
123 }
124 #[cfg(feature = "with-json")]
125 Value::Json(j) => {
126 args.push(to_value!(j));
127 }
128 #[cfg(feature = "postgres-array")]
129 Value::Array(_, _) => {
130 panic!("Mysql doesn't support array arguments");
131 }
132 #[cfg(feature = "with-ipnetwork")]
133 Value::IpNetwork(_) => {
134 panic!("Mysql doesn't support IpNetwork arguments");
135 }
136 #[cfg(feature = "with-mac_address")]
137 Value::MacAddress(_) => {
138 panic!("Mysql doesn't support MacAddress arguments");
139 }
140 }
141 }
142 args
143}