Implement AsRef<Statement> for Row(s) (#887)

This commit is contained in:
gwenn 2021-05-26 14:51:28 +02:00 committed by GitHub
parent 48e7561af9
commit df02910660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 68 deletions

View File

@ -1,6 +1,6 @@
use std::str; use std::str;
use crate::{Error, Result, Row, Rows, Statement}; use crate::{Error, Result, Statement};
/// Information about a column of a SQLite query. /// Information about a column of a SQLite query.
#[derive(Debug)] #[derive(Debug)]
@ -147,72 +147,6 @@ impl Statement<'_> {
} }
} }
impl<'stmt> Rows<'stmt> {
/// Get all the column names.
#[inline]
pub fn column_names(&self) -> Option<Vec<&str>> {
self.stmt.map(Statement::column_names)
}
/// Return the number of columns.
#[inline]
pub fn column_count(&self) -> Option<usize> {
self.stmt.map(Statement::column_count)
}
/// Return the name of the column.
#[inline]
pub fn column_name(&self, col: usize) -> Option<Result<&str>> {
self.stmt.map(|stmt| stmt.column_name(col))
}
/// Return the index of the column.
#[inline]
pub fn column_index(&self, name: &str) -> Option<Result<usize>> {
self.stmt.map(|stmt| stmt.column_index(name))
}
/// Returns a slice describing the columns of the Rows.
#[inline]
#[cfg(feature = "column_decltype")]
pub fn columns(&self) -> Option<Vec<Column>> {
self.stmt.map(Statement::columns)
}
}
impl<'stmt> Row<'stmt> {
/// Get all the column names of the Row.
#[inline]
pub fn column_names(&self) -> Vec<&str> {
self.stmt.column_names()
}
/// Return the number of columns in the current row.
#[inline]
pub fn column_count(&self) -> usize {
self.stmt.column_count()
}
/// Return the name of the column.
#[inline]
pub fn column_name(&self, col: usize) -> Result<&str> {
self.stmt.column_name(col)
}
/// Return the index of the column.
#[inline]
pub fn column_index(&self, name: &str) -> Result<usize> {
self.stmt.column_index(name)
}
/// Returns a slice describing the columns of the Row.
#[inline]
#[cfg(feature = "column_decltype")]
pub fn columns(&self) -> Vec<Column> {
self.stmt.columns()
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{Connection, Result}; use crate::{Connection, Result};

View File

@ -1861,7 +1861,7 @@ mod test {
db.execute_batch(sql)?; db.execute_batch(sql)?;
db.query_row("SELECT * FROM foo", [], |r| { db.query_row("SELECT * FROM foo", [], |r| {
assert_eq!(2, r.column_count()); assert_eq!(2, r.as_ref().column_count());
Ok(()) Ok(())
}) })
} }

View File

@ -78,6 +78,11 @@ impl<'stmt> Rows<'stmt> {
{ {
AndThenRows { rows: self, map: f } AndThenRows { rows: self, map: f }
} }
/// Give access to the underlying statement
pub fn as_ref(&self) -> Option<&Statement<'stmt>> {
self.stmt
}
} }
impl<'stmt> Rows<'stmt> { impl<'stmt> Rows<'stmt> {
@ -358,6 +363,12 @@ impl<'stmt> Row<'stmt> {
} }
} }
impl<'stmt> AsRef<Statement<'stmt>> for Row<'stmt> {
fn as_ref(&self) -> &Statement<'stmt> {
self.stmt
}
}
mod sealed { mod sealed {
/// This trait exists just to ensure that the only impls of `trait Params` /// This trait exists just to ensure that the only impls of `trait Params`
/// that are allowed are ones in this crate. /// that are allowed are ones in this crate.