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
use crate::{
ast::Value,
error::{Error, ErrorKind},
};
use std::sync::Arc;
/// An owned version of a `Row` in a `ResultSet`. See
/// [ResultRowRef](struct.ResultRowRef.html) for documentation on data access.
#[derive(Debug, PartialEq)]
pub struct ResultRow {
pub(crate) columns: Arc<Vec<String>>,
pub(crate) values: Vec<Value<'static>>,
}
impl IntoIterator for ResultRow {
type Item = Value<'static>;
type IntoIter = std::vec::IntoIter<Value<'static>>;
fn into_iter(self) -> Self::IntoIter {
self.values.into_iter()
}
}
/// A reference to a `Row` in a `ResultSet`. The columns can be accessed either
/// through their position or using the column name.
///
/// ```
/// # use quaint::connector::*;
/// let names = vec!["id".to_string(), "name".to_string()];
/// let rows = vec![vec!["1234".into(), "Musti".into()]];
///
/// let result_set = ResultSet::new(names, rows);
/// let row = result_set.first().unwrap();
///
/// assert_eq!(row[0], row["id"]);
/// assert_eq!(row[1], row["name"]);
/// ```
#[derive(Debug, PartialEq)]
pub struct ResultRowRef<'a> {
pub(crate) columns: Arc<Vec<String>>,
pub(crate) values: &'a Vec<Value<'static>>,
}
impl ResultRow {
/// Take a value from a certain position in the row, if having a value in
/// that position. Usage documentation in
/// [ResultRowRef](struct.ResultRowRef.html).
pub fn at(&self, i: usize) -> Option<&Value<'static>> {
if self.values.len() <= i {
None
} else {
Some(&self.values[i])
}
}
/// Take a value with the given column name from the row. Usage
/// documentation in [ResultRowRef](struct.ResultRowRef.html).
pub fn get(&self, name: &str) -> Option<&Value<'static>> {
self.columns.iter().position(|c| c == name).map(|idx| &self.values[idx])
}
/// Make a referring [ResultRowRef](struct.ResultRowRef.html).
pub fn as_ref(&self) -> ResultRowRef {
ResultRowRef {
columns: Arc::clone(&self.columns),
values: &self.values,
}
}
pub fn into_single(self) -> crate::Result<Value<'static>> {
match self.into_iter().next() {
Some(val) => Ok(val),
None => Err(Error::builder(ErrorKind::NotFound).build()),
}
}
}
impl<'a> ResultRowRef<'a> {
/// Take a value from a certain position in the row, if having a value in
/// that position.
///
/// ```
/// # use quaint::connector::*;
/// # let names = vec!["id".to_string(), "name".to_string()];
/// # let rows = vec![vec!["1234".into(), "Musti".into()]];
/// # let result_set = ResultSet::new(names, rows);
/// # let row = result_set.first().unwrap();
/// assert_eq!(Some(&row[0]), row.at(0));
/// ```
pub fn at(&self, i: usize) -> Option<&'a Value<'static>> {
if self.values.len() <= i {
None
} else {
Some(&self.values[i])
}
}
/// Take a value with the given column name from the row.
///
/// ```
/// # use quaint::connector::*;
/// # let names = vec!["id".to_string(), "name".to_string()];
/// # let rows = vec![vec!["1234".into(), "Musti".into()]];
/// # let result_set = ResultSet::new(names, rows);
/// # let row = result_set.first().unwrap();
/// assert_eq!(Some(&row["id"]), row.get("id"));
/// ```
pub fn get(&self, name: &str) -> Option<&'a Value<'static>> {
self.columns.iter().position(|c| c == name).map(|idx| &self.values[idx])
}
}