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

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

use crate::{ast::Value, error::*};
use serde_json::Map;
use std::sync::Arc;

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

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

    pub fn set_last_insert_id(&mut self, id: u64) {
        self.last_insert_id = Some(id);
    }

    /// The last id inserted, if available. Only works on certain databases and
    /// if using an auto-increment ids.
    pub fn last_insert_id(&self) -> Option<u64> {
        self.last_insert_id
    }

    /// 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,
        })
    }

    /// Takes the first row if existing, otherwise returns error.
    pub fn into_single(self) -> crate::Result<ResultRow> {
        match self.into_iter().next() {
            Some(row) => Ok(row),
            None => Err(Error::builder(ErrorKind::NotFound).build()),
        }
    }
}

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<Value<'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,
        }
    }
}

impl From<ResultSet> for serde_json::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, serde_json::Value::from(p_value));
            }

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

        serde_json::Value::Array(result)
    }
}