cdrs_async/query/
query_values.rs

1use std::{collections::HashMap, hash::Hash};
2
3use cassandra_proto::{frame::IntoBytes, types::value::Value, types::CString};
4
5/// Enum that represents named and simple query values.
6#[derive(Debug, Clone)]
7pub enum QueryValues {
8  /// Simple query values. During query exectuion a DB sever will use
9  /// such values one-by-one in the same order as they provided.
10  SimpleValues(Vec<Value>),
11  /// Named query values. During query execution a DB server will use
12  /// such values for entities which have the same name in a DB scheme.
13  NamedValues(HashMap<String, Value>),
14}
15
16impl QueryValues {
17  /// It returns `true` if query values is with names and `false` otherwise.
18  pub fn with_names(&self) -> bool {
19    match *self {
20      QueryValues::SimpleValues(_) => false,
21      _ => true,
22    }
23  }
24
25  /// It return number of values.
26  pub fn len(&self) -> usize {
27    match *self {
28      QueryValues::SimpleValues(ref v) => v.len(),
29      QueryValues::NamedValues(ref m) => m.len(),
30    }
31  }
32
33  fn named_value_into_bytes_fold(mut bytes: Vec<u8>, vals: (&String, &Value)) -> Vec<u8> {
34    let mut name_bytes = CString::new(vals.0.clone()).into_cbytes();
35    let mut vals_bytes = vals.1.into_cbytes();
36    bytes.append(&mut name_bytes);
37    bytes.append(&mut vals_bytes);
38    bytes
39  }
40
41  fn value_into_bytes_fold(mut bytes: Vec<u8>, val: &Value) -> Vec<u8> {
42    let mut val_bytes = val.into_cbytes();
43    bytes.append(&mut val_bytes);
44    bytes
45  }
46}
47
48impl<T: Into<Value> + Clone> From<Vec<T>> for QueryValues {
49  /// It converts values from `Vec` to query values without names `QueryValues::SimpleValues`.
50  fn from(values: Vec<T>) -> QueryValues {
51    let vals = values.iter().map(|v| v.clone().into());
52    QueryValues::SimpleValues(vals.collect())
53  }
54}
55
56impl<'a, T: Into<Value> + Clone> From<&'a [T]> for QueryValues {
57  /// It converts values from `Vec` to query values without names `QueryValues::SimpleValues`.
58  fn from(values: &'a [T]) -> QueryValues {
59    let vals = values.iter().map(|v| v.clone().into());
60    QueryValues::SimpleValues(vals.collect())
61  }
62}
63
64impl<S: ToString + Hash + Eq, V: Into<Value> + Clone> From<HashMap<S, V>> for QueryValues {
65  /// It converts values from `HashMap` to query values with names `QueryValues::NamedValues`.
66  fn from(values: HashMap<S, V>) -> QueryValues {
67    let map: HashMap<String, Value> = HashMap::with_capacity(values.len());
68    let _values = values.iter().fold(map, |mut acc, v| {
69      let name = v.0;
70      let val = v.1;
71      acc.insert(name.to_string(), val.clone().into());
72      acc
73    });
74    QueryValues::NamedValues(_values)
75  }
76}
77
78impl IntoBytes for QueryValues {
79  fn into_cbytes(&self) -> Vec<u8> {
80    let bytes: Vec<u8> = vec![];
81    match *self {
82      QueryValues::SimpleValues(ref v) => v.iter().fold(bytes, QueryValues::value_into_bytes_fold),
83      QueryValues::NamedValues(ref v) => v
84        .iter()
85        .fold(bytes, QueryValues::named_value_into_bytes_fold),
86    }
87  }
88}