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
use std::{marker, sync::Arc};

use crate::{
    column::{Column, ColumnType},
    types::{FromSql, SqlType},
};

use crate::{
    block::{Block, ColumnIdx},
    error::Result,
};

/// A row from Clickhouse
pub struct Row<'a, K: ColumnType> {
    pub(crate) row: usize,
    pub(crate) block_ref: BlockRef<'a, K>,
    pub(crate) kind: marker::PhantomData<K>,
}

impl<'a, K: ColumnType> Row<'a, K> {
    /// Get the value of a particular cell of the row.
    pub fn get<T, I>(&'a self, col: I) -> Result<T>
    where
        T: FromSql<'a>,
        I: ColumnIdx + Copy,
    {
        self.block_ref.get(self.row, col)
    }

    /// Return the number of cells in the current row.
    pub fn len(&self) -> usize {
        self.block_ref.column_count()
    }

    /// Returns `true` if the row contains no cells.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the name of a particular cell of the row.
    pub fn name<I: ColumnIdx + Copy>(&self, col: I) -> Result<&str> {
        Ok(self.block_ref.get_column(col)?.name())
    }

    /// Get the type of a particular cell of the row.
    pub fn sql_type<I: ColumnIdx + Copy>(&self, col: I) -> Result<SqlType> {
        Ok(self.block_ref.get_column(col)?.sql_type())
    }
}

pub(crate) enum BlockRef<'a, K: ColumnType> {
    Borrowed(&'a Block<K>),
    Owned(Arc<Block<K>>),
}

impl<'a, K: ColumnType> Clone for BlockRef<'a, K> {
    fn clone(&self) -> Self {
        match self {
            BlockRef::Borrowed(block_ref) => BlockRef::Borrowed(*block_ref),
            BlockRef::Owned(block_ref) => BlockRef::Owned(block_ref.clone()),
        }
    }
}

impl<'a, K: ColumnType> BlockRef<'a, K> {
    fn row_count(&self) -> usize {
        match self {
            BlockRef::Borrowed(block) => block.row_count(),
            BlockRef::Owned(block) => block.row_count(),
        }
    }

    fn column_count(&self) -> usize {
        match self {
            BlockRef::Borrowed(block) => block.column_count(),
            BlockRef::Owned(block) => block.column_count(),
        }
    }

    fn get<'s, T, I>(&'s self, row: usize, col: I) -> Result<T>
    where
        T: FromSql<'s>,
        I: ColumnIdx + Copy,
    {
        match self {
            BlockRef::Borrowed(block) => block.get(row, col),
            BlockRef::Owned(block) => block.get(row, col),
        }
    }

    fn get_column<I: ColumnIdx + Copy>(&self, col: I) -> Result<&Column<K>> {
        match self {
            BlockRef::Borrowed(block) => {
                let column_index = col.get_index(block.columns())?;
                Ok(&block.columns[column_index])
            }
            BlockRef::Owned(block) => {
                let column_index = col.get_index(block.columns())?;
                Ok(&block.columns[column_index])
            }
        }
    }
}

/// Immutable rows iterator
pub struct Rows<'a, K: ColumnType> {
    pub(crate) row: usize,
    pub(crate) block_ref: BlockRef<'a, K>,
    pub(crate) kind: marker::PhantomData<K>,
}

impl<'a, K: ColumnType> Iterator for Rows<'a, K> {
    type Item = Row<'a, K>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.row >= self.block_ref.row_count() {
            return None;
        }
        let result = Some(Row {
            row: self.row,
            block_ref: self.block_ref.clone(),
            kind: marker::PhantomData,
        });
        self.row += 1;
        result
    }
}