cassandra_proto/query/
query_params_builder.rs

1use super::{QueryFlags, QueryParams, QueryValues};
2use crate::consistency::Consistency;
3use crate::types::CBytes;
4
5#[derive(Debug, Default)]
6pub struct QueryParamsBuilder {
7  consistency: Consistency,
8  flags: Option<Vec<QueryFlags>>,
9  values: Option<QueryValues>,
10  with_names: Option<bool>,
11  page_size: Option<i32>,
12  paging_state: Option<CBytes>,
13  serial_consistency: Option<Consistency>,
14  timestamp: Option<i64>,
15}
16
17impl QueryParamsBuilder {
18  /// Factory function that returns new `QueryBuilder`.
19  /// Default consistency level is `One`
20  pub fn new() -> QueryParamsBuilder {
21    Default::default()
22  }
23
24  /// Sets new query consistency
25  pub fn consistency(mut self, consistency: Consistency) -> Self {
26    self.consistency = consistency;
27
28    self
29  }
30
31  /// Sets new flags.
32  builder_opt_field!(flags, Vec<QueryFlags>);
33
34  /// Sets new values.
35  /// Sets new query consistency
36  pub fn values(mut self, values: QueryValues) -> Self {
37    let with_names = values.with_names();
38    self.with_names = Some(with_names);
39    self.values = Some(values);
40    self.flags = self.flags.or(Some(vec![])).map(|mut flags| {
41      flags.push(QueryFlags::Value);
42      if with_names {
43        flags.push(QueryFlags::WithNamesForValues);
44      }
45      flags
46    });
47
48    self
49  }
50
51  /// Sets new with_names parameter value.
52  builder_opt_field!(with_names, bool);
53
54  /// Sets new values.
55  /// Sets new query consistency
56  pub fn page_size(mut self, size: i32) -> Self {
57    self.page_size = Some(size);
58    self.flags = self.flags.or(Some(vec![])).map(|mut flags| {
59      flags.push(QueryFlags::PageSize);
60      flags
61    });
62
63    self
64  }
65
66  /// Sets new values.
67  /// Sets new query consistency
68  pub fn paging_state(mut self, state: CBytes) -> Self {
69    self.paging_state = Some(state);
70    self.flags = self.flags.or(Some(vec![])).map(|mut flags| {
71      flags.push(QueryFlags::WithPagingState);
72      flags
73    });
74
75    self
76  }
77
78  /// Sets new serial_consistency value.
79  builder_opt_field!(serial_consistency, Consistency);
80
81  /// Sets new timestamp value.
82  builder_opt_field!(timestamp, i64);
83
84  /// Finalizes query building process and returns query itself
85  pub fn finalize(self) -> QueryParams {
86    QueryParams {
87      consistency: self.consistency,
88      flags: self.flags.unwrap_or(vec![]),
89      values: self.values,
90      with_names: self.with_names,
91      page_size: self.page_size,
92      paging_state: self.paging_state,
93      serial_consistency: self.serial_consistency,
94      timestamp: self.timestamp,
95    }
96  }
97}