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
mod index;
mod result_row;

pub use index::*;
pub use result_row::*;

use crate::ast::ParameterizedValue;
use std::sync::Arc;

#[cfg(feature = "json-1")]
use serde_json::{Map, Value};

/// Encapsulates a set of results and their respective column names.
#[derive(Debug)]
pub struct ResultSet {
    pub(crate) columns: Arc<Vec<String>>,
    pub(crate) rows: Vec<Vec<ParameterizedValue<'static>>>,
}

impl ResultSet {
    /// Creates a new instance, bound to the given column names and result rows.
    pub fn new(
        names: Vec<String>,
        rows: Vec<Vec<ParameterizedValue<'static>>>,
    ) -> Self
    {
        Self {
            columns: Arc::new(names),
            rows,
        }
    }

    /// An iterator of column names.
    pub fn columns(&self) -> &Vec<String> {
        &self.columns
    }

    /// Returns the number of rows in the `ResultSet`.
    pub fn len(&self) -> usize {
        self.rows.len()
    }

    /// Returns true if the `ResultSet` contains no rows.
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Returns the first row of the `ResultSet`, or None if the set is empty.
    pub fn first(&self) -> Option<ResultRowRef> {
        self.get(0)
    }

    /// Returns a reference to a row in a given position.
    pub fn get(&self, index: usize) -> Option<ResultRowRef> {
        self.rows.get(index).map(|row| ResultRowRef {
            columns: Arc::clone(&self.columns),
            values: row,
        })
    }
}

impl IntoIterator for ResultSet {
    type Item = ResultRow;
    type IntoIter = ResultSetIterator;

    fn into_iter(self) -> Self::IntoIter {
        ResultSetIterator {
            columns: self.columns,
            internal_iterator: self.rows.into_iter(),
        }
    }
}

/// Thin iterator for ResultSet rows.
/// Might become lazy one day.
pub struct ResultSetIterator {
    pub(crate) columns: Arc<Vec<String>>,
    pub(crate) internal_iterator: std::vec::IntoIter<Vec<ParameterizedValue<'static>>>,
}

impl Iterator for ResultSetIterator {
    type Item = ResultRow;

    fn next(&mut self) -> Option<Self::Item> {
        match self.internal_iterator.next() {
            Some(row) => Some(ResultRow {
                columns: Arc::clone(&self.columns),
                values: row,
            }),
            None => None,
        }
    }
}

#[cfg(feature = "json-1")]
impl From<ResultSet> for Value {
    fn from(result_set: ResultSet) -> Self {
        let columns: Vec<String> = result_set.columns().iter().map(ToString::to_string).collect();
        let mut result = Vec::new();

        for row in result_set.into_iter() {
            let mut object = Map::new();

            for (idx, p_value) in row.into_iter().enumerate() {
                let column_name: String = columns[idx].clone();
                object.insert(column_name, Value::from(p_value));
            }

            result.push(Value::Object(object));
        }

        Value::Array(result)
    }
}