1use std::collections::HashMap;
2use std::time::Duration;
3
4use bigdecimal::BigDecimal;
5use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
6use num_bigint::BigInt;
7use qubit_common::lang::DataType;
8use url::Url;
9
10use crate::Value;
11use crate::error::{ValueError, ValueResult};
12
13use super::multi_values::MultiValues;
14
15#[doc(hidden)]
28pub trait MultiValuesGetter<T> {
29 fn get_values(&self) -> ValueResult<Vec<T>>;
30}
31
32#[doc(hidden)]
37pub trait MultiValuesFirstGetter<T> {
38 fn get_first_value(&self) -> ValueResult<T>;
39}
40
41#[doc(hidden)]
46pub trait MultiValuesSetter<T> {
47 fn set_values(&mut self, values: Vec<T>) -> ValueResult<()>;
48}
49
50#[doc(hidden)]
52pub trait MultiValuesSetArg<'a> {
53 type Item: 'a + Clone;
55
56 fn apply(self, target: &mut MultiValues) -> ValueResult<()>;
57}
58
59#[doc(hidden)]
64pub trait MultiValuesSetterSlice<T> {
65 fn set_values_slice(&mut self, values: &[T]) -> ValueResult<()>;
66}
67
68#[doc(hidden)]
73pub trait MultiValuesSingleSetter<T> {
74 fn set_single_value(&mut self, value: T) -> ValueResult<()>;
75}
76
77#[doc(hidden)]
82pub trait MultiValuesAdder<T> {
83 fn add_value(&mut self, value: T) -> ValueResult<()>;
84}
85
86#[doc(hidden)]
91pub trait MultiValuesMultiAdder<T> {
92 fn add_values(&mut self, values: Vec<T>) -> ValueResult<()>;
93}
94
95#[doc(hidden)]
97pub trait MultiValuesAddArg<'a> {
98 type Item: 'a + Clone;
100
101 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()>;
102}
103
104#[doc(hidden)]
107pub(crate) trait MultiValuesMultiAdderSlice<T> {
108 fn add_values_slice(&mut self, values: &[T]) -> ValueResult<()>;
109}
110
111#[doc(hidden)]
116pub trait MultiValuesConstructor<T> {
117 fn from_vec(values: Vec<T>) -> Self;
118}
119
120macro_rules! impl_multi_value_traits {
125 ($type:ty, $variant:ident, $data_type:expr) => {
126 impl MultiValuesGetter<$type> for MultiValues {
127 #[inline]
128 fn get_values(&self) -> ValueResult<Vec<$type>> {
129 match self {
130 MultiValues::$variant(v) => Ok(v.clone()),
131 MultiValues::Empty(dt) if *dt == $data_type => Ok(Vec::new()),
132 _ => Err(ValueError::TypeMismatch {
133 expected: $data_type,
134 actual: self.data_type(),
135 }),
136 }
137 }
138 }
139
140 impl MultiValuesFirstGetter<$type> for MultiValues {
141 #[inline]
142 fn get_first_value(&self) -> ValueResult<$type> {
143 match self {
144 MultiValues::$variant(v) if !v.is_empty() => Ok(v[0].clone()),
145 MultiValues::$variant(_) => Err(ValueError::NoValue),
146 MultiValues::Empty(dt) if *dt == $data_type => Err(ValueError::NoValue),
147 _ => Err(ValueError::TypeMismatch {
148 expected: $data_type,
149 actual: self.data_type(),
150 }),
151 }
152 }
153 }
154
155 impl MultiValuesSetter<$type> for MultiValues {
156 #[inline]
157 fn set_values(&mut self, values: Vec<$type>) -> ValueResult<()> {
158 *self = MultiValues::$variant(values);
159 Ok(())
160 }
161 }
162
163 impl MultiValuesSetterSlice<$type> for MultiValues {
167 #[inline]
168 fn set_values_slice(&mut self, values: &[$type]) -> ValueResult<()> {
169 *self = MultiValues::$variant(values.to_vec());
171 Ok(())
172 }
173 }
174
175 impl MultiValuesSingleSetter<$type> for MultiValues {
176 #[inline]
177 fn set_single_value(&mut self, value: $type) -> ValueResult<()> {
178 *self = MultiValues::$variant(vec![value]);
179 Ok(())
180 }
181 }
182
183 impl MultiValuesAdder<$type> for MultiValues {
184 #[inline]
185 fn add_value(&mut self, value: $type) -> ValueResult<()> {
186 match self {
187 MultiValues::$variant(v) => {
188 v.push(value);
189 Ok(())
190 }
191 MultiValues::Empty(dt) if *dt == $data_type => {
192 *self = MultiValues::$variant(vec![value]);
193 Ok(())
194 }
195 _ => Err(ValueError::TypeMismatch {
196 expected: $data_type,
197 actual: self.data_type(),
198 }),
199 }
200 }
201 }
202
203 impl<'a> MultiValuesSetArg<'a> for Vec<$type> {
205 type Item = $type;
206
207 #[inline]
208 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
209 <MultiValues as MultiValuesSetter<$type>>::set_values(target, self)
210 }
211 }
212
213 impl<'a> MultiValuesSetArg<'a> for &'a [$type]
214 where
215 $type: Clone,
216 {
217 type Item = $type;
218
219 #[inline]
220 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
221 <MultiValues as MultiValuesSetterSlice<$type>>::set_values_slice(target, self)
222 }
223 }
224
225 impl<'a> MultiValuesSetArg<'a> for $type {
226 type Item = $type;
227
228 #[inline]
229 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
230 <MultiValues as MultiValuesSingleSetter<$type>>::set_single_value(target, self)
231 }
232 }
233
234 impl MultiValuesMultiAdder<$type> for MultiValues {
235 #[inline]
236 fn add_values(&mut self, values: Vec<$type>) -> ValueResult<()> {
237 match self {
238 MultiValues::$variant(v) => {
239 v.extend(values);
240 Ok(())
241 }
242 MultiValues::Empty(dt) if *dt == $data_type => {
243 *self = MultiValues::$variant(values);
244 Ok(())
245 }
246 _ => Err(ValueError::TypeMismatch {
247 expected: $data_type,
248 actual: self.data_type(),
249 }),
250 }
251 }
252 }
253
254 impl MultiValuesMultiAdderSlice<$type> for MultiValues {
255 #[inline]
256 fn add_values_slice(&mut self, values: &[$type]) -> ValueResult<()> {
257 match self {
258 MultiValues::$variant(v) => {
259 v.extend_from_slice(values);
260 Ok(())
261 }
262 MultiValues::Empty(dt) if *dt == $data_type => {
263 *self = MultiValues::$variant(values.to_vec());
264 Ok(())
265 }
266 _ => Err(ValueError::TypeMismatch {
267 expected: $data_type,
268 actual: self.data_type(),
269 }),
270 }
271 }
272 }
273
274 impl<'a> MultiValuesAddArg<'a> for $type {
276 type Item = $type;
277
278 #[inline]
279 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
280 <MultiValues as MultiValuesAdder<$type>>::add_value(target, self)
281 }
282 }
283
284 impl<'a> MultiValuesAddArg<'a> for Vec<$type> {
285 type Item = $type;
286
287 #[inline]
288 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
289 <MultiValues as MultiValuesMultiAdder<$type>>::add_values(target, self)
290 }
291 }
292
293 impl<'a> MultiValuesAddArg<'a> for &'a [$type]
294 where
295 $type: Clone,
296 {
297 type Item = $type;
298
299 #[inline]
300 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
301 <MultiValues as MultiValuesMultiAdderSlice<$type>>::add_values_slice(target, self)
302 }
303 }
304
305 impl MultiValuesConstructor<$type> for MultiValues {
306 #[inline]
307 fn from_vec(values: Vec<$type>) -> Self {
308 MultiValues::$variant(values)
309 }
310 }
311 };
312}
313
314impl_multi_value_traits!(bool, Bool, DataType::Bool);
316impl_multi_value_traits!(char, Char, DataType::Char);
317impl_multi_value_traits!(i8, Int8, DataType::Int8);
318impl_multi_value_traits!(i16, Int16, DataType::Int16);
319impl_multi_value_traits!(i32, Int32, DataType::Int32);
320impl_multi_value_traits!(i64, Int64, DataType::Int64);
321impl_multi_value_traits!(i128, Int128, DataType::Int128);
322impl_multi_value_traits!(u8, UInt8, DataType::UInt8);
323impl_multi_value_traits!(u16, UInt16, DataType::UInt16);
324impl_multi_value_traits!(u32, UInt32, DataType::UInt32);
325impl_multi_value_traits!(u64, UInt64, DataType::UInt64);
326impl_multi_value_traits!(u128, UInt128, DataType::UInt128);
327impl_multi_value_traits!(f32, Float32, DataType::Float32);
328impl_multi_value_traits!(f64, Float64, DataType::Float64);
329impl_multi_value_traits!(String, String, DataType::String);
330impl_multi_value_traits!(NaiveDate, Date, DataType::Date);
331impl_multi_value_traits!(NaiveTime, Time, DataType::Time);
332impl_multi_value_traits!(NaiveDateTime, DateTime, DataType::DateTime);
333impl_multi_value_traits!(DateTime<Utc>, Instant, DataType::Instant);
334impl_multi_value_traits!(BigInt, BigInteger, DataType::BigInteger);
335impl_multi_value_traits!(BigDecimal, BigDecimal, DataType::BigDecimal);
336impl_multi_value_traits!(isize, IntSize, DataType::IntSize);
337impl_multi_value_traits!(usize, UIntSize, DataType::UIntSize);
338impl_multi_value_traits!(Duration, Duration, DataType::Duration);
339impl_multi_value_traits!(Url, Url, DataType::Url);
340impl_multi_value_traits!(HashMap<String, String>, StringMap, DataType::StringMap);
341impl_multi_value_traits!(serde_json::Value, Json, DataType::Json);
342
343impl MultiValuesSetArg<'_> for &str {
345 type Item = String;
346
347 #[inline]
348 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
349 <MultiValues as MultiValuesSingleSetter<String>>::set_single_value(target, self.to_string())
350 }
351}
352
353impl MultiValuesSetArg<'_> for Vec<&str> {
354 type Item = String;
355
356 #[inline]
357 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
358 let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
359 <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
360 }
361}
362
363impl<'b> MultiValuesSetArg<'_> for &'b [&'b str] {
364 type Item = String;
365
366 #[inline]
367 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
368 let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
369 <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
370 }
371}
372
373impl MultiValuesAddArg<'_> for &str {
374 type Item = String;
375
376 #[inline]
377 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
378 <MultiValues as MultiValuesAdder<String>>::add_value(target, self.to_string())
379 }
380}
381
382impl MultiValuesAddArg<'_> for Vec<&str> {
383 type Item = String;
384
385 #[inline]
386 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
387 let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
388 <MultiValues as MultiValuesMultiAdder<String>>::add_values(target, owned)
389 }
390}
391
392impl<'b> MultiValuesAddArg<'_> for &'b [&'b str] {
393 type Item = String;
394
395 #[inline]
396 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
397 let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
398 <MultiValues as MultiValuesMultiAdder<String>>::add_values(target, owned)
399 }
400}
401
402impl MultiValues {
407 pub fn to_value(&self) -> Value {
416 match self {
417 MultiValues::Empty(dt) => Value::Empty(*dt),
418 MultiValues::Bool(v) => v
419 .first()
420 .copied()
421 .map(Value::Bool)
422 .unwrap_or(Value::Empty(DataType::Bool)),
423 MultiValues::Char(v) => v
424 .first()
425 .copied()
426 .map(Value::Char)
427 .unwrap_or(Value::Empty(DataType::Char)),
428 MultiValues::Int8(v) => v
429 .first()
430 .copied()
431 .map(Value::Int8)
432 .unwrap_or(Value::Empty(DataType::Int8)),
433 MultiValues::Int16(v) => v
434 .first()
435 .copied()
436 .map(Value::Int16)
437 .unwrap_or(Value::Empty(DataType::Int16)),
438 MultiValues::Int32(v) => v
439 .first()
440 .copied()
441 .map(Value::Int32)
442 .unwrap_or(Value::Empty(DataType::Int32)),
443 MultiValues::Int64(v) => v
444 .first()
445 .copied()
446 .map(Value::Int64)
447 .unwrap_or(Value::Empty(DataType::Int64)),
448 MultiValues::Int128(v) => v
449 .first()
450 .copied()
451 .map(Value::Int128)
452 .unwrap_or(Value::Empty(DataType::Int128)),
453 MultiValues::UInt8(v) => v
454 .first()
455 .copied()
456 .map(Value::UInt8)
457 .unwrap_or(Value::Empty(DataType::UInt8)),
458 MultiValues::UInt16(v) => v
459 .first()
460 .copied()
461 .map(Value::UInt16)
462 .unwrap_or(Value::Empty(DataType::UInt16)),
463 MultiValues::UInt32(v) => v
464 .first()
465 .copied()
466 .map(Value::UInt32)
467 .unwrap_or(Value::Empty(DataType::UInt32)),
468 MultiValues::UInt64(v) => v
469 .first()
470 .copied()
471 .map(Value::UInt64)
472 .unwrap_or(Value::Empty(DataType::UInt64)),
473 MultiValues::UInt128(v) => v
474 .first()
475 .copied()
476 .map(Value::UInt128)
477 .unwrap_or(Value::Empty(DataType::UInt128)),
478 MultiValues::IntSize(v) => v
479 .first()
480 .copied()
481 .map(Value::IntSize)
482 .unwrap_or(Value::Empty(DataType::IntSize)),
483 MultiValues::UIntSize(v) => v
484 .first()
485 .copied()
486 .map(Value::UIntSize)
487 .unwrap_or(Value::Empty(DataType::UIntSize)),
488 MultiValues::Float32(v) => v
489 .first()
490 .copied()
491 .map(Value::Float32)
492 .unwrap_or(Value::Empty(DataType::Float32)),
493 MultiValues::Float64(v) => v
494 .first()
495 .copied()
496 .map(Value::Float64)
497 .unwrap_or(Value::Empty(DataType::Float64)),
498 MultiValues::BigInteger(v) => v
499 .first()
500 .cloned()
501 .map(Value::BigInteger)
502 .unwrap_or(Value::Empty(DataType::BigInteger)),
503 MultiValues::BigDecimal(v) => v
504 .first()
505 .cloned()
506 .map(Value::BigDecimal)
507 .unwrap_or(Value::Empty(DataType::BigDecimal)),
508 MultiValues::String(v) => v
509 .first()
510 .cloned()
511 .map(Value::String)
512 .unwrap_or(Value::Empty(DataType::String)),
513 MultiValues::Date(v) => v
514 .first()
515 .copied()
516 .map(Value::Date)
517 .unwrap_or(Value::Empty(DataType::Date)),
518 MultiValues::Time(v) => v
519 .first()
520 .copied()
521 .map(Value::Time)
522 .unwrap_or(Value::Empty(DataType::Time)),
523 MultiValues::DateTime(v) => v
524 .first()
525 .copied()
526 .map(Value::DateTime)
527 .unwrap_or(Value::Empty(DataType::DateTime)),
528 MultiValues::Instant(v) => v
529 .first()
530 .copied()
531 .map(Value::Instant)
532 .unwrap_or(Value::Empty(DataType::Instant)),
533 MultiValues::Duration(v) => v
534 .first()
535 .copied()
536 .map(Value::Duration)
537 .unwrap_or(Value::Empty(DataType::Duration)),
538 MultiValues::Url(v) => v
539 .first()
540 .cloned()
541 .map(Value::Url)
542 .unwrap_or(Value::Empty(DataType::Url)),
543 MultiValues::StringMap(v) => v
544 .first()
545 .cloned()
546 .map(Value::StringMap)
547 .unwrap_or(Value::Empty(DataType::StringMap)),
548 MultiValues::Json(v) => v
549 .first()
550 .cloned()
551 .map(Value::Json)
552 .unwrap_or(Value::Empty(DataType::Json)),
553 }
554 }
555
556 pub fn merge(&mut self, other: &MultiValues) -> ValueResult<()> {
579 if self.data_type() != other.data_type() {
580 return Err(ValueError::TypeMismatch {
581 expected: self.data_type(),
582 actual: other.data_type(),
583 });
584 }
585 if other.count() == 0 {
586 return Ok(());
587 }
588
589 match (self, other) {
590 (MultiValues::Bool(v), MultiValues::Bool(o)) => v.extend_from_slice(o),
591 (MultiValues::Char(v), MultiValues::Char(o)) => v.extend_from_slice(o),
592 (MultiValues::Int8(v), MultiValues::Int8(o)) => v.extend_from_slice(o),
593 (MultiValues::Int16(v), MultiValues::Int16(o)) => v.extend_from_slice(o),
594 (MultiValues::Int32(v), MultiValues::Int32(o)) => v.extend_from_slice(o),
595 (MultiValues::Int64(v), MultiValues::Int64(o)) => v.extend_from_slice(o),
596 (MultiValues::Int128(v), MultiValues::Int128(o)) => v.extend_from_slice(o),
597 (MultiValues::UInt8(v), MultiValues::UInt8(o)) => v.extend_from_slice(o),
598 (MultiValues::UInt16(v), MultiValues::UInt16(o)) => v.extend_from_slice(o),
599 (MultiValues::UInt32(v), MultiValues::UInt32(o)) => v.extend_from_slice(o),
600 (MultiValues::UInt64(v), MultiValues::UInt64(o)) => v.extend_from_slice(o),
601 (MultiValues::UInt128(v), MultiValues::UInt128(o)) => v.extend_from_slice(o),
602 (MultiValues::Float32(v), MultiValues::Float32(o)) => v.extend_from_slice(o),
603 (MultiValues::Float64(v), MultiValues::Float64(o)) => v.extend_from_slice(o),
604 (MultiValues::String(v), MultiValues::String(o)) => v.extend_from_slice(o),
605 (MultiValues::Date(v), MultiValues::Date(o)) => v.extend_from_slice(o),
606 (MultiValues::Time(v), MultiValues::Time(o)) => v.extend_from_slice(o),
607 (MultiValues::DateTime(v), MultiValues::DateTime(o)) => v.extend_from_slice(o),
608 (MultiValues::Instant(v), MultiValues::Instant(o)) => v.extend_from_slice(o),
609 (MultiValues::BigInteger(v), MultiValues::BigInteger(o)) => v.extend_from_slice(o),
610 (MultiValues::BigDecimal(v), MultiValues::BigDecimal(o)) => v.extend_from_slice(o),
611 (MultiValues::IntSize(v), MultiValues::IntSize(o)) => v.extend_from_slice(o),
612 (MultiValues::UIntSize(v), MultiValues::UIntSize(o)) => v.extend_from_slice(o),
613 (MultiValues::Duration(v), MultiValues::Duration(o)) => v.extend_from_slice(o),
614 (MultiValues::Url(v), MultiValues::Url(o)) => v.extend_from_slice(o),
615 (MultiValues::StringMap(v), MultiValues::StringMap(o)) => v.extend(o.iter().cloned()),
616 (MultiValues::Json(v), MultiValues::Json(o)) => v.extend(o.iter().cloned()),
617 (slot @ MultiValues::Empty(_), other_values) => *slot = other_values.clone(),
618 _ => unreachable!(),
619 }
620
621 Ok(())
622 }
623}
624
625impl Default for MultiValues {
626 #[inline]
627 fn default() -> Self {
628 MultiValues::Empty(DataType::String)
629 }
630}
631
632impl From<Value> for MultiValues {
633 fn from(value: Value) -> Self {
634 match value {
635 Value::Empty(dt) => MultiValues::Empty(dt),
636 Value::Bool(v) => MultiValues::Bool(vec![v]),
637 Value::Char(v) => MultiValues::Char(vec![v]),
638 Value::Int8(v) => MultiValues::Int8(vec![v]),
639 Value::Int16(v) => MultiValues::Int16(vec![v]),
640 Value::Int32(v) => MultiValues::Int32(vec![v]),
641 Value::Int64(v) => MultiValues::Int64(vec![v]),
642 Value::Int128(v) => MultiValues::Int128(vec![v]),
643 Value::UInt8(v) => MultiValues::UInt8(vec![v]),
644 Value::UInt16(v) => MultiValues::UInt16(vec![v]),
645 Value::UInt32(v) => MultiValues::UInt32(vec![v]),
646 Value::UInt64(v) => MultiValues::UInt64(vec![v]),
647 Value::UInt128(v) => MultiValues::UInt128(vec![v]),
648 Value::Float32(v) => MultiValues::Float32(vec![v]),
649 Value::Float64(v) => MultiValues::Float64(vec![v]),
650 Value::String(v) => MultiValues::String(vec![v]),
651 Value::Date(v) => MultiValues::Date(vec![v]),
652 Value::Time(v) => MultiValues::Time(vec![v]),
653 Value::DateTime(v) => MultiValues::DateTime(vec![v]),
654 Value::Instant(v) => MultiValues::Instant(vec![v]),
655 Value::BigInteger(v) => MultiValues::BigInteger(vec![v]),
656 Value::BigDecimal(v) => MultiValues::BigDecimal(vec![v]),
657 Value::IntSize(v) => MultiValues::IntSize(vec![v]),
658 Value::UIntSize(v) => MultiValues::UIntSize(vec![v]),
659 Value::Duration(v) => MultiValues::Duration(vec![v]),
660 Value::Url(v) => MultiValues::Url(vec![v]),
661 Value::StringMap(v) => MultiValues::StringMap(vec![v]),
662 Value::Json(v) => MultiValues::Json(vec![v]),
663 }
664 }
665}