cassandra_protocol/types/
rows.rs

1use std::net::IpAddr;
2use std::num::{NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8};
3use std::sync::Arc;
4
5use chrono::prelude::*;
6use time::PrimitiveDateTime;
7use uuid::Uuid;
8
9use crate::error::{column_is_empty_err, Error, Result};
10use crate::frame::message_result::{
11    BodyResResultRows, ColSpec, ColType, ColTypeOption, ColTypeOptionValue, RowsMetadata,
12};
13use crate::frame::Version;
14use crate::types::blob::Blob;
15use crate::types::data_serialization_types::*;
16use crate::types::decimal::Decimal;
17use crate::types::list::List;
18use crate::types::map::Map;
19use crate::types::tuple::Tuple;
20use crate::types::udt::Udt;
21use crate::types::{ByIndex, ByName, CBytes, IntoRustByIndex, IntoRustByName};
22use num_bigint::BigInt;
23
24#[derive(Clone, Debug)]
25pub struct Row {
26    metadata: Arc<RowsMetadata>,
27    row_content: Vec<CBytes>,
28    protocol_version: Version,
29}
30
31impl Row {
32    pub fn from_body(body: BodyResResultRows) -> Vec<Row> {
33        let metadata = Arc::new(body.metadata);
34        let protocol_version = body.protocol_version;
35        body.rows_content
36            .into_iter()
37            .map(|row| Row {
38                metadata: metadata.clone(),
39                row_content: row,
40                protocol_version,
41            })
42            .collect()
43    }
44
45    /// Checks if a column is present in the row.
46    pub fn contains_column(&self, name: &str) -> bool {
47        self.metadata
48            .col_specs
49            .iter()
50            .any(|spec| spec.name.as_str() == name)
51    }
52
53    /// Checks for NULL for a given column. Returns false if given column does not exist.
54    pub fn is_empty(&self, index: usize) -> bool {
55        self.row_content
56            .get(index)
57            .map(|data| data.is_null_or_empty())
58            .unwrap_or(false)
59    }
60
61    /// Checks for NULL for a given column. Returns false if given column does not exist.
62    pub fn is_empty_by_name(&self, name: &str) -> bool {
63        self.metadata
64            .col_specs
65            .iter()
66            .position(|spec| spec.name.as_str() == name)
67            .map(|index| self.is_empty(index))
68            .unwrap_or(false)
69    }
70
71    fn col_spec_by_name(&self, name: &str) -> Option<(&ColSpec, &CBytes)> {
72        self.metadata
73            .col_specs
74            .iter()
75            .position(|spec| spec.name.as_str() == name)
76            .map(|i| {
77                let col_spec = &self.metadata.col_specs[i];
78                let data = &self.row_content[i];
79                (col_spec, data)
80            })
81    }
82
83    fn col_spec_by_index(&self, index: usize) -> Option<(&ColSpec, &CBytes)> {
84        let specs = self.metadata.col_specs.iter();
85        let values = self.row_content.iter();
86        specs.zip(values).nth(index)
87    }
88}
89
90impl ByName for Row {}
91
92into_rust_by_name!(Row, Blob);
93into_rust_by_name!(Row, String);
94into_rust_by_name!(Row, bool);
95into_rust_by_name!(Row, i64);
96into_rust_by_name!(Row, i32);
97into_rust_by_name!(Row, i16);
98into_rust_by_name!(Row, i8);
99into_rust_by_name!(Row, f64);
100into_rust_by_name!(Row, f32);
101into_rust_by_name!(Row, IpAddr);
102into_rust_by_name!(Row, Uuid);
103into_rust_by_name!(Row, List);
104into_rust_by_name!(Row, Map);
105into_rust_by_name!(Row, Udt);
106into_rust_by_name!(Row, Tuple);
107into_rust_by_name!(Row, PrimitiveDateTime);
108into_rust_by_name!(Row, Decimal);
109into_rust_by_name!(Row, NonZeroI8);
110into_rust_by_name!(Row, NonZeroI16);
111into_rust_by_name!(Row, NonZeroI32);
112into_rust_by_name!(Row, NonZeroI64);
113into_rust_by_name!(Row, NaiveDateTime);
114into_rust_by_name!(Row, DateTime<Utc>);
115into_rust_by_name!(Row, BigInt);
116
117impl ByIndex for Row {}
118
119into_rust_by_index!(Row, Blob);
120into_rust_by_index!(Row, String);
121into_rust_by_index!(Row, bool);
122into_rust_by_index!(Row, i64);
123into_rust_by_index!(Row, i32);
124into_rust_by_index!(Row, i16);
125into_rust_by_index!(Row, i8);
126into_rust_by_index!(Row, f64);
127into_rust_by_index!(Row, f32);
128into_rust_by_index!(Row, IpAddr);
129into_rust_by_index!(Row, Uuid);
130into_rust_by_index!(Row, List);
131into_rust_by_index!(Row, Map);
132into_rust_by_index!(Row, Udt);
133into_rust_by_index!(Row, Tuple);
134into_rust_by_index!(Row, PrimitiveDateTime);
135into_rust_by_index!(Row, Decimal);
136into_rust_by_index!(Row, NonZeroI8);
137into_rust_by_index!(Row, NonZeroI16);
138into_rust_by_index!(Row, NonZeroI32);
139into_rust_by_index!(Row, NonZeroI64);
140into_rust_by_index!(Row, NaiveDateTime);
141into_rust_by_index!(Row, DateTime<Utc>);
142into_rust_by_index!(Row, BigInt);