toql_core/sql_builder/select_stream.rs
1//! Deserializing selected columns into struct fields.
2
3/// Hold select state about a struct field.
4#[derive(Debug, PartialEq)]
5pub enum Select {
6 /// Field is selected in the query
7 Query,
8 /// Field is always selected (preselection)
9 Preselect,
10 /// Field is not selected
11 None,
12}
13
14impl Select {
15 /// Return true, if field is selected
16 pub fn is_selected(&self) -> bool {
17 self != &Select::None
18 }
19}
20
21/// SelectStream memorizes which fields and joined structs are selected.
22///
23/// It is needed for the deserialization trait [FromRow](crate::from_row::FromRow).
24/// The selections can either come from the query or the preselections in the mapping.
25/// The number of selections does not correspond with the number of selected columns or expressions, because
26/// each join gets an additional selection. For the number of columns take (BuildResult::column_counter)[crate::sql_builder::build_result::BuildResult::column_counter].
27#[derive(Debug, PartialEq)]
28pub struct SelectStream {
29 stream: Vec<Select>,
30}
31
32impl SelectStream {
33 /// Create new stream.
34 pub fn new() -> Self {
35 Self { stream: Vec::new() }
36 }
37 /// Add select state at the end of the stream.
38 pub fn push(&mut self, selection: Select) {
39 self.stream.push(selection);
40 }
41 /// Return iterator to the stream.
42 pub fn iter(&self) -> std::slice::Iter<'_, Select> {
43 self.stream.iter()
44 }
45}
46
47impl Default for SelectStream {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53#[cfg(test)]
54mod test {
55 use super::SelectStream;
56
57 #[test]
58 fn default() {
59 assert_eq!(SelectStream::new(), SelectStream::default())
60 }
61}