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
// This file is part of https://github.com/SpringQL/SpringQL which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.

use crate::{
    api::{
        error::{Result, SpringError},
        spring_source_row::SpringSourceRow,
    },
    stream_engine::{autonomous_executor::SchemalessRow, SpringValue, SqlValue},
};

/// Row object from an in memory sink queue.
#[derive(Debug)]
pub struct SpringSinkRow(SchemalessRow);

impl SpringSinkRow {
    pub(crate) fn new(row: SchemalessRow) -> Self {
        SpringSinkRow(row)
    }

    /// Get a i-th column value from the row.
    ///
    /// # Failure
    ///
    /// - [SpringError::Sql](crate::api::error::SpringError::Sql) when:
    ///   - Column index out of range
    /// - [SpringError::Null](crate::api::error::SpringError::Null) when:
    ///   - Column value is NULL
    pub fn get_not_null_by_index<T>(&self, i_col: usize) -> Result<T>
    where
        T: SpringValue,
    {
        let sql_value = self.0.get_by_index(i_col)?;

        match sql_value {
            SqlValue::Null => Err(SpringError::Null { i_col }),
            SqlValue::NotNull(nn_sql_value) => nn_sql_value.unpack(),
        }
    }
}

impl From<SpringSinkRow> for SpringSourceRow {
    fn from(sink_row: SpringSinkRow) -> Self {
        SpringSourceRow::new(sink_row.0)
    }
}