Skip to main content

sea_query_sqlx/
sqlx_postgres.rs

1#[cfg(all(feature = "postgres-array", feature = "with-bigdecimal"))]
2use sea_query::prelude::BigDecimal;
3#[cfg(all(feature = "postgres-array", feature = "with-rust_decimal"))]
4use sea_query::prelude::Decimal;
5#[cfg(all(feature = "postgres-array", feature = "with-ipnetwork"))]
6use sea_query::prelude::IpNetwork;
7#[cfg(all(feature = "postgres-array", feature = "with-json"))]
8use sea_query::prelude::Json;
9#[cfg(all(feature = "postgres-array", feature = "with-mac_address"))]
10use sea_query::prelude::MacAddress;
11#[cfg(all(feature = "postgres-array", feature = "with-uuid"))]
12use sea_query::prelude::Uuid;
13#[cfg(all(feature = "postgres-array", feature = "with-time"))]
14use sea_query::prelude::time;
15#[cfg(all(feature = "postgres-array", feature = "with-chrono"))]
16use sea_query::prelude::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc};
17
18#[cfg(feature = "postgres-array")]
19use sea_query::ArrayType;
20use sea_query::{OptionEnum, Value};
21
22use crate::SqlxValues;
23
24impl sqlx::IntoArguments<sqlx::postgres::Postgres> for SqlxValues {
25    fn into_arguments(self) -> sqlx::postgres::PgArguments {
26        let mut args = sqlx::postgres::PgArguments::default();
27        for arg in self.0.into_iter() {
28            use sqlx::Arguments;
29            match arg {
30                Value::Bool(b) => {
31                    let _ = args.add(b);
32                }
33                Value::TinyInt(i) => {
34                    let _ = args.add(i);
35                }
36                Value::SmallInt(i) => {
37                    let _ = args.add(i);
38                }
39                Value::Int(i) => {
40                    let _ = args.add(i);
41                }
42                Value::BigInt(i) => {
43                    let _ = args.add(i);
44                }
45                Value::TinyUnsigned(i) => {
46                    let _ = args.add(i.map(|i| i as i16));
47                }
48                Value::SmallUnsigned(i) => {
49                    let _ = args.add(i.map(|i| i as i32));
50                }
51                Value::Unsigned(i) => {
52                    let _ = args.add(i.map(|i| i as i64));
53                }
54                Value::BigUnsigned(i) => {
55                    let _ = args.add(i.map(|i| <i64 as TryFrom<u64>>::try_from(i).unwrap()));
56                }
57                Value::Float(f) => {
58                    let _ = args.add(f);
59                }
60                Value::Double(d) => {
61                    let _ = args.add(d);
62                }
63                Value::String(s) => {
64                    let _ = args.add(s.as_deref());
65                }
66                Value::Enum(e) => {
67                    let value = match e {
68                        OptionEnum::Some(v) => Some(v.value.into_owned()),
69                        OptionEnum::None(_) => None,
70                    };
71                    let _ = args.add(value);
72                }
73                Value::Char(c) => {
74                    let _ = args.add(c.map(|c| c.to_string()));
75                }
76                Value::Bytes(b) => {
77                    let _ = args.add(b.as_deref());
78                }
79                #[cfg(feature = "with-chrono")]
80                Value::ChronoDate(d) => {
81                    let _ = args.add(d);
82                }
83                #[cfg(feature = "with-chrono")]
84                Value::ChronoTime(t) => {
85                    let _ = args.add(t);
86                }
87                #[cfg(feature = "with-chrono")]
88                Value::ChronoDateTime(t) => {
89                    let _ = args.add(t);
90                }
91                #[cfg(feature = "with-chrono")]
92                Value::ChronoDateTimeUtc(t) => {
93                    let _ = args.add(t);
94                }
95                #[cfg(feature = "with-chrono")]
96                Value::ChronoDateTimeLocal(t) => {
97                    let _ = args.add(t);
98                }
99                #[cfg(feature = "with-chrono")]
100                Value::ChronoDateTimeWithTimeZone(t) => {
101                    let _ = args.add(t);
102                }
103                #[cfg(feature = "with-time")]
104                Value::TimeDate(t) => {
105                    let _ = args.add(t);
106                }
107                #[cfg(feature = "with-time")]
108                Value::TimeTime(t) => {
109                    let _ = args.add(t);
110                }
111                #[cfg(feature = "with-time")]
112                Value::TimeDateTime(t) => {
113                    let _ = args.add(t);
114                }
115                #[cfg(feature = "with-time")]
116                Value::TimeDateTimeWithTimeZone(t) => {
117                    let _ = args.add(t);
118                }
119                #[cfg(feature = "with-jiff")]
120                Value::JiffDate(_) => {
121                    panic!("SQLx 0.9 does not support Jiff arguments for Postgres yet");
122                }
123                #[cfg(feature = "with-jiff")]
124                Value::JiffTime(_) => {
125                    panic!("SQLx 0.9 does not support Jiff arguments for Postgres yet");
126                }
127                #[cfg(feature = "with-jiff")]
128                Value::JiffDateTime(_) => {
129                    panic!("SQLx 0.9 does not support Jiff arguments for Postgres yet");
130                }
131                #[cfg(feature = "with-jiff")]
132                Value::JiffTimestamp(_) => {
133                    panic!("SQLx 0.9 does not support Jiff arguments for Postgres yet");
134                }
135                #[cfg(feature = "with-uuid")]
136                Value::Uuid(uuid) => {
137                    let _ = args.add(uuid);
138                }
139                #[cfg(feature = "with-rust_decimal")]
140                Value::Decimal(d) => {
141                    let _ = args.add(d);
142                }
143                #[cfg(feature = "with-bigdecimal")]
144                Value::BigDecimal(d) => {
145                    let _ = args.add(d.as_deref());
146                }
147                #[cfg(feature = "with-json")]
148                Value::Json(j) => {
149                    let _ = args.add(j.as_deref());
150                }
151                #[cfg(feature = "with-ipnetwork")]
152                Value::IpNetwork(ip) => {
153                    let _ = args.add(ip);
154                }
155                #[cfg(feature = "with-mac_address")]
156                Value::MacAddress(mac) => {
157                    let _ = args.add(mac);
158                }
159                #[cfg(feature = "postgres-array")]
160                Value::Array(ty, v) => match ty {
161                    ArrayType::Bool => {
162                        let value: Option<Vec<bool>> = Value::Array(ty, v)
163                            .expect("This Value::Array should consist of Value::Bool");
164                        let _ = args.add(value);
165                    }
166                    ArrayType::TinyInt => {
167                        let value: Option<Vec<i8>> = Value::Array(ty, v)
168                            .expect("This Value::Array should consist of Value::TinyInt");
169                        let _ = args.add(value);
170                    }
171                    ArrayType::SmallInt => {
172                        let value: Option<Vec<i16>> = Value::Array(ty, v)
173                            .expect("This Value::Array should consist of Value::SmallInt");
174                        let _ = args.add(value);
175                    }
176                    ArrayType::Int => {
177                        let value: Option<Vec<i32>> = Value::Array(ty, v)
178                            .expect("This Value::Array should consist of Value::Int");
179                        let _ = args.add(value);
180                    }
181                    ArrayType::BigInt => {
182                        let value: Option<Vec<i64>> = Value::Array(ty, v)
183                            .expect("This Value::Array should consist of Value::BigInt");
184                        let _ = args.add(value);
185                    }
186                    ArrayType::TinyUnsigned => {
187                        let value: Option<Vec<u8>> = Value::Array(ty, v)
188                            .expect("This Value::Array should consist of Value::TinyUnsigned");
189                        let value: Option<Vec<i16>> =
190                            value.map(|vec| vec.into_iter().map(|i| i as i16).collect());
191                        let _ = args.add(value);
192                    }
193                    ArrayType::SmallUnsigned => {
194                        let value: Option<Vec<u16>> = Value::Array(ty, v)
195                            .expect("This Value::Array should consist of Value::SmallUnsigned");
196                        let value: Option<Vec<i32>> =
197                            value.map(|vec| vec.into_iter().map(|i| i as i32).collect());
198                        let _ = args.add(value);
199                    }
200                    ArrayType::Unsigned => {
201                        let value: Option<Vec<u32>> = Value::Array(ty, v)
202                            .expect("This Value::Array should consist of Value::Unsigned");
203                        let value: Option<Vec<i64>> =
204                            value.map(|vec| vec.into_iter().map(|i| i as i64).collect());
205                        let _ = args.add(value);
206                    }
207                    ArrayType::BigUnsigned => {
208                        let value: Option<Vec<u64>> = Value::Array(ty, v)
209                            .expect("This Value::Array should consist of Value::BigUnsigned");
210                        let value: Option<Vec<i64>> = value.map(|vec| {
211                            vec.into_iter()
212                                .map(|i| <i64 as TryFrom<u64>>::try_from(i).unwrap())
213                                .collect()
214                        });
215                        let _ = args.add(value);
216                    }
217                    ArrayType::Float => {
218                        let value: Option<Vec<f32>> = Value::Array(ty, v)
219                            .expect("This Value::Array should consist of Value::Float");
220                        let _ = args.add(value);
221                    }
222                    ArrayType::Double => {
223                        let value: Option<Vec<f64>> = Value::Array(ty, v)
224                            .expect("This Value::Array should consist of Value::Double");
225                        let _ = args.add(value);
226                    }
227                    ArrayType::String => {
228                        let value: Option<Vec<String>> = Value::Array(ty, v)
229                            .expect("This Value::Array should consist of Value::String");
230                        let _ = args.add(value);
231                    }
232                    ArrayType::Enum(_) => {
233                        let value: Option<Vec<String>> = v.map(|values| {
234                            values
235                                .into_iter()
236                                .map(|value| match value {
237                                    Value::Enum(OptionEnum::Some(value))  => value.value.into_owned(),
238                                    _ => {
239                                        panic!(
240                                            "Value::Array(ArrayType::Enum) should contain Value::Enum"
241                                        );
242                                    }
243                                })
244                                .collect()
245                        });
246                        let _ = args.add(value);
247                    }
248                    ArrayType::Char => {
249                        let value: Option<Vec<char>> = Value::Array(ty, v)
250                            .expect("This Value::Array should consist of Value::Char");
251                        let value: Option<Vec<String>> =
252                            value.map(|vec| vec.into_iter().map(|c| c.to_string()).collect());
253                        let _ = args.add(value);
254                    }
255                    ArrayType::Bytes => {
256                        let value: Option<Vec<Vec<u8>>> = Value::Array(ty, v)
257                            .expect("This Value::Array should consist of Value::Bytes");
258                        let _ = args.add(value);
259                    }
260                    #[cfg(feature = "with-chrono")]
261                    ArrayType::ChronoDate => {
262                        let value: Option<Vec<NaiveDate>> = Value::Array(ty, v)
263                            .expect("This Value::Array should consist of Value::ChronoDate");
264                        let _ = args.add(value);
265                    }
266                    #[cfg(feature = "with-chrono")]
267                    ArrayType::ChronoTime => {
268                        let value: Option<Vec<NaiveTime>> = Value::Array(ty, v)
269                            .expect("This Value::Array should consist of Value::ChronoTime");
270                        let _ = args.add(value);
271                    }
272                    #[cfg(feature = "with-chrono")]
273                    ArrayType::ChronoDateTime => {
274                        let value: Option<Vec<NaiveDateTime>> = Value::Array(ty, v)
275                            .expect("This Value::Array should consist of Value::ChronoDateTime");
276                        let _ = args.add(value);
277                    }
278                    #[cfg(feature = "with-chrono")]
279                    ArrayType::ChronoDateTimeUtc => {
280                        let value: Option<Vec<DateTime<Utc>>> = Value::Array(ty, v)
281                            .expect("This Value::Array should consist of Value::ChronoDateTimeUtc");
282                        let _ = args.add(value);
283                    }
284                    #[cfg(feature = "with-chrono")]
285                    ArrayType::ChronoDateTimeLocal => {
286                        let value: Option<Vec<DateTime<Local>>> = Value::Array(ty, v).expect(
287                            "This Value::Array should consist of Value::ChronoDateTimeLocal",
288                        );
289                        let _ = args.add(value);
290                    }
291                    #[cfg(feature = "with-chrono")]
292                    ArrayType::ChronoDateTimeWithTimeZone => {
293                        let value: Option<Vec<DateTime<FixedOffset>>> = Value::Array(ty, v).expect(
294                            "This Value::Array should consist of Value::ChronoDateTimeWithTimeZone",
295                        );
296                        let _ = args.add(value);
297                    }
298                    #[cfg(feature = "with-time")]
299                    ArrayType::TimeDate => {
300                        let value: Option<Vec<time::Date>> = Value::Array(ty, v)
301                            .expect("This Value::Array should consist of Value::TimeDate");
302                        let _ = args.add(value);
303                    }
304                    #[cfg(feature = "with-time")]
305                    ArrayType::TimeTime => {
306                        let value: Option<Vec<time::Time>> = Value::Array(ty, v)
307                            .expect("This Value::Array should consist of Value::TimeTime");
308                        let _ = args.add(value);
309                    }
310                    #[cfg(feature = "with-time")]
311                    ArrayType::TimeDateTime => {
312                        let value: Option<Vec<time::PrimitiveDateTime>> = Value::Array(ty, v)
313                            .expect("This Value::Array should consist of Value::TimeDateTime");
314                        let _ = args.add(value);
315                    }
316                    #[cfg(feature = "with-time")]
317                    ArrayType::TimeDateTimeWithTimeZone => {
318                        let value: Option<Vec<time::OffsetDateTime>> = Value::Array(ty, v).expect(
319                            "This Value::Array should consist of Value::TimeDateTimeWithTimeZone",
320                        );
321                        let _ = args.add(value);
322                    }
323                    #[cfg(feature = "with-jiff")]
324                    ArrayType::JiffDate => {
325                        panic!("SQLx 0.9 does not support Jiff array arguments for Postgres yet");
326                    }
327                    #[cfg(feature = "with-jiff")]
328                    ArrayType::JiffTime => {
329                        panic!("SQLx 0.9 does not support Jiff array arguments for Postgres yet");
330                    }
331                    #[cfg(feature = "with-jiff")]
332                    ArrayType::JiffDateTime => {
333                        panic!("SQLx 0.9 does not support Jiff array arguments for Postgres yet");
334                    }
335                    #[cfg(feature = "with-jiff")]
336                    ArrayType::JiffTimestamp => {
337                        panic!("SQLx 0.9 does not support Jiff array arguments for Postgres yet");
338                    }
339                    #[cfg(feature = "with-uuid")]
340                    ArrayType::Uuid => {
341                        let value: Option<Vec<Uuid>> = Value::Array(ty, v)
342                            .expect("This Value::Array should consist of Value::Uuid");
343                        let _ = args.add(value);
344                    }
345                    #[cfg(feature = "with-rust_decimal")]
346                    ArrayType::Decimal => {
347                        let value: Option<Vec<Decimal>> = Value::Array(ty, v)
348                            .expect("This Value::Array should consist of Value::Decimal");
349                        let _ = args.add(value);
350                    }
351                    #[cfg(feature = "with-bigdecimal")]
352                    ArrayType::BigDecimal => {
353                        let value: Option<Vec<BigDecimal>> = Value::Array(ty, v)
354                            .expect("This Value::Array should consist of Value::BigDecimal");
355                        let _ = args.add(value);
356                    }
357                    #[cfg(feature = "with-json")]
358                    ArrayType::Json => {
359                        let value: Option<Vec<Json>> = Value::Array(ty, v)
360                            .expect("This Value::Array should consist of Value::Json");
361                        let _ = args.add(value);
362                    }
363                    #[cfg(feature = "with-ipnetwork")]
364                    ArrayType::IpNetwork => {
365                        let value: Option<Vec<IpNetwork>> = Value::Array(ty, v)
366                            .expect("This Value::Array should consist of Value::IpNetwork");
367                        let _ = args.add(value);
368                    }
369                    #[cfg(feature = "with-mac_address")]
370                    ArrayType::MacAddress => {
371                        let value: Option<Vec<MacAddress>> = Value::Array(ty, v)
372                            .expect("This Value::Array should consist of Value::MacAddress");
373                        let _ = args.add(value);
374                    }
375                },
376                #[cfg(feature = "postgres-vector")]
377                Value::Vector(v) => {
378                    let _ = args.add(v);
379                } /* #[cfg(feature = "postgres-range")]
380                  Value::Range(v) => {
381                  let _ = args.add(v);
382                  } */
383            }
384        }
385        args
386    }
387}