1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::{
    base::{RsCore, OAM},
    conn::AmConnCore,
    protocol::parts::{HdbValue, ResultSetMetadata},
    HdbError, HdbResult,
};
use serde_db::de::DeserializableRow;
use std::sync::Arc;

/// A single line of a `ResultSet`, consisting of the contained `HdbValue`s and
/// a reference to the metadata.
///
/// `Row` has several methods that support an efficient data transfer into your own data structures.
///
/// You also can access individual values with `row[idx]`, or iterate over the values (with
/// `row.iter()` or `for value in row {...}`).
#[derive(Debug)]
pub struct Row {
    metadata: Arc<ResultSetMetadata>,
    value_iter: <Vec<HdbValue<'static>> as IntoIterator>::IntoIter,
}

impl Row {
    /// Factory for row.
    pub(crate) fn new(metadata: Arc<ResultSetMetadata>, values: Vec<HdbValue<'static>>) -> Self {
        Self {
            metadata,
            value_iter: values.into_iter(),
        }
    }

    /// Converts the entire Row into a rust value.
    ///
    /// # Errors
    ///
    /// `HdbError::Deserialization` if deserialization into the target type is not possible.
    pub fn try_into<'de, T>(self) -> HdbResult<T>
    where
        T: serde::de::Deserialize<'de>,
    {
        trace!("Row::into_typed()");
        Ok(DeserializableRow::try_into(self)?)
    }

    /// Removes and returns the next value.
    pub fn next_value(&mut self) -> Option<HdbValue<'static>> {
        self.value_iter.next()
    }

    /// Conveniently combines `next_value()` and the value's `try_into()`.
    ///
    /// # Errors
    ///
    /// `HdbError::Usage` if there is no more element.
    ///
    /// `HdbError::Deserialization` if deserialization into the target type is not possible.
    pub fn next_try_into<'de, T>(&mut self) -> HdbResult<T>
    where
        T: serde::de::Deserialize<'de>,
    {
        self.next_value()
            .ok_or_else(|| HdbError::Usage("no more value"))?
            .try_into()
    }

    /// Returns the length of the row.
    pub fn len(&self) -> usize {
        trace!("Row::len()");
        self.value_iter.len()
    }

    /// Returns true if the row contains no value.
    pub fn is_empty(&self) -> bool {
        self.value_iter.as_slice().is_empty()
    }

    /// Converts itself in the single contained value.
    ///
    /// # Errors
    ///
    /// `HdbError::Usage` if the row is empty or has more than one value.
    pub fn into_single_value(mut self) -> HdbResult<HdbValue<'static>> {
        if self.len() > 1 {
            Err(HdbError::Usage("Row has more than one field"))
        } else {
            Ok(self
                .next_value()
                .ok_or_else(|| HdbError::Usage("Row is empty"))?)
        }
    }

    /// Returns the metadata.
    pub fn metadata(&self) -> &ResultSetMetadata {
        trace!("Row::metadata()");
        &(self.metadata)
    }

    #[cfg(feature = "sync")]
    pub(crate) fn parse_sync(
        md: Arc<ResultSetMetadata>,
        o_am_rscore: &OAM<RsCore>,
        am_conn_core: &AmConnCore,
        rdr: &mut std::io::Cursor<Vec<u8>>,
    ) -> HdbResult<Self> {
        let mut values = Vec::<HdbValue>::new();

        let md0 = Arc::as_ref(&md);

        // for col_idx in 0..md.len() {
        for col_md in &**md0 {
            let value = HdbValue::parse_sync(
                col_md.type_id(),
                col_md.is_array_type(),
                col_md.scale(),
                col_md.is_nullable(),
                am_conn_core,
                o_am_rscore,
                rdr,
            )?;
            values.push(value);
        }
        let row = Self::new(md, values);
        Ok(row)
    }

    #[cfg(feature = "async")]
    pub(crate) async fn parse_async(
        md: Arc<ResultSetMetadata>,
        o_am_rscore: &OAM<RsCore>,
        am_conn_core: &AmConnCore,
        rdr: &mut std::io::Cursor<Vec<u8>>,
    ) -> HdbResult<Self> {
        let mut values = Vec::<HdbValue>::new();

        let md0 = Arc::as_ref(&md);

        // for col_idx in 0..md.len() {
        for col_md in &**md0 {
            let value = HdbValue::parse_async(
                col_md.type_id(),
                col_md.is_array_type(),
                col_md.scale(),
                col_md.is_nullable(),
                am_conn_core,
                o_am_rscore,
                rdr,
            )
            .await?;
            values.push(value);
        }
        let row = Self::new(md, values);
        Ok(row)
    }
}

/// Support indexing.
impl std::ops::Index<usize> for Row {
    type Output = HdbValue<'static>;
    fn index(&self, idx: usize) -> &HdbValue<'static> {
        &self.value_iter.as_slice()[idx]
    }
}

impl std::ops::IndexMut<usize> for Row {
    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
        &mut self.value_iter.as_mut_slice()[idx]
    }
}

/// Row is an iterator with item `HdbValue`.
impl Iterator for Row {
    type Item = HdbValue<'static>;
    fn next(&mut self) -> Option<HdbValue<'static>> {
        self.next_value()
    }
}

impl std::fmt::Display for Row {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        for v in self.value_iter.as_slice() {
            write!(fmt, "{v}, ")?;
        }
        Ok(())
    }
}