cassandra_protocol/query/
query_params_builder.rs

1use super::{QueryFlags, QueryParams, QueryValues};
2use crate::consistency::Consistency;
3use crate::types::{CBytes, CInt, CLong};
4
5#[derive(Debug, Default)]
6pub struct QueryParamsBuilder {
7    consistency: Consistency,
8    flags: Option<QueryFlags>,
9    values: Option<QueryValues>,
10    with_names: bool,
11    page_size: Option<CInt>,
12    paging_state: Option<CBytes>,
13    serial_consistency: Option<Consistency>,
14    timestamp: Option<CLong>,
15    keyspace: Option<String>,
16    now_in_seconds: Option<CInt>,
17}
18
19impl QueryParamsBuilder {
20    /// Factory function that returns new `QueryBuilder`.
21    pub fn new() -> QueryParamsBuilder {
22        Default::default()
23    }
24
25    /// Sets new query consistency
26    #[must_use]
27    pub fn with_consistency(mut self, consistency: Consistency) -> Self {
28        self.consistency = consistency;
29        self
30    }
31
32    /// Sets new flags.
33    #[must_use]
34    pub fn with_flags(mut self, flags: QueryFlags) -> Self {
35        self.flags = Some(flags);
36        self
37    }
38
39    /// Sets new query values.
40    #[must_use]
41    pub fn with_values(mut self, values: QueryValues) -> Self {
42        self.with_names = values.has_names();
43        self.values = Some(values);
44        self.flags = self.flags.or_else(|| {
45            let mut flags = QueryFlags::VALUE;
46            if self.with_names {
47                flags.insert(QueryFlags::WITH_NAMES_FOR_VALUES);
48            }
49            Some(flags)
50        });
51
52        self
53    }
54
55    /// Sets the "with names for values" flag
56    #[must_use]
57    pub fn with_names(mut self, with_names: bool) -> Self {
58        self.with_names = with_names;
59        self
60    }
61
62    /// Sets new query consistency.
63    #[must_use]
64    pub fn with_page_size(mut self, size: CInt) -> Self {
65        self.page_size = Some(size);
66        self.flags = self.flags.or(Some(QueryFlags::PAGE_SIZE));
67
68        self
69    }
70
71    /// Sets new query consistency.
72    #[must_use]
73    pub fn with_paging_state(mut self, state: CBytes) -> Self {
74        self.paging_state = Some(state);
75        self.flags = self.flags.or(Some(QueryFlags::WITH_PAGING_STATE));
76
77        self
78    }
79
80    /// Sets new serial consistency.
81    #[must_use]
82    pub fn with_serial_consistency(mut self, serial_consistency: Consistency) -> Self {
83        self.serial_consistency = Some(serial_consistency);
84        self
85    }
86
87    /// Sets new timestamp.
88    #[must_use]
89    pub fn with_timestamp(mut self, timestamp: CLong) -> Self {
90        self.timestamp = Some(timestamp);
91        self
92    }
93
94    /// Overrides used keyspace.
95    #[must_use]
96    pub fn with_keyspace(mut self, keyspace: String) -> Self {
97        self.keyspace = Some(keyspace);
98        self
99    }
100
101    /// Sets "now" in seconds.
102    #[must_use]
103    pub fn with_now_in_seconds(mut self, now_in_seconds: CInt) -> Self {
104        self.now_in_seconds = Some(now_in_seconds);
105        self
106    }
107
108    /// Finalizes query building process and returns query itself
109    #[must_use]
110    pub fn build(self) -> QueryParams {
111        QueryParams {
112            consistency: self.consistency,
113            values: self.values,
114            with_names: self.with_names,
115            page_size: self.page_size,
116            paging_state: self.paging_state,
117            serial_consistency: self.serial_consistency,
118            timestamp: self.timestamp,
119            keyspace: self.keyspace,
120            now_in_seconds: self.now_in_seconds,
121        }
122    }
123}