feat(core): add Row::get, Row::get_raw

This commit is contained in:
Ryan Leckey 2021-02-22 21:35:51 -08:00
parent 21f3a83c5f
commit eddd873f4d
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
2 changed files with 44 additions and 0 deletions

View File

@ -40,12 +40,25 @@ pub trait Row: 'static + Send + Sync {
/// Returns the column index, given the name of the column.
fn try_index_of(&self, name: &str) -> crate::Result<usize>;
/// Returns the decoded value at the index.
fn get<'r, T, I>(&'r self, index: I) -> T
where
I: ColumnIndex<Self>,
T: Decode<'r, Self::Database>;
/// Returns the decoded value at the index.
fn try_get<'r, T, I>(&'r self, index: I) -> crate::Result<T>
where
I: ColumnIndex<Self>,
T: Decode<'r, Self::Database>;
/// Returns the raw representation of the value at the index.
#[allow(clippy::needless_lifetimes)]
fn get_raw<'r, I: ColumnIndex<Self>>(
&'r self,
index: I,
) -> <Self::Database as HasRawValue<'r>>::RawValue;
/// Returns the raw representation of the value at the index.
#[allow(clippy::needless_lifetimes)]
fn try_get_raw<'r, I: ColumnIndex<Self>>(

View File

@ -79,6 +79,15 @@ impl MySqlRow {
.ok_or_else(|| Error::ColumnNotFound { name: name.to_owned().into_boxed_str() })
}
/// Returns the decoded value at the index.
pub fn get<'r, T, I>(&'r self, index: I) -> T
where
I: ColumnIndex<Self>,
T: Decode<'r, MySql>,
{
self.try_get(index).unwrap()
}
/// Returns the decoded value at the index.
pub fn try_get<'r, T, I>(&'r self, index: I) -> Result<T>
where
@ -88,6 +97,15 @@ impl MySqlRow {
Ok(self.try_get_raw(index)?.decode()?)
}
/// Returns the raw representation of the value at the index.
#[allow(clippy::needless_lifetimes)]
pub fn get_raw<'r, I>(&'r self, index: I) -> MySqlRawValue<'r>
where
I: ColumnIndex<Self>,
{
self.try_get_raw(index).unwrap()
}
/// Returns the raw representation of the value at the index.
#[allow(clippy::needless_lifetimes)]
pub fn try_get_raw<'r, I>(&'r self, index: I) -> Result<MySqlRawValue<'r>>
@ -146,6 +164,14 @@ impl Row for MySqlRow {
self.try_index_of(name)
}
fn get<'r, T, I>(&'r self, index: I) -> T
where
I: ColumnIndex<Self>,
T: Decode<'r, MySql>,
{
self.get(index)
}
fn try_get<'r, T, I>(&'r self, index: I) -> Result<T>
where
I: ColumnIndex<Self>,
@ -154,6 +180,11 @@ impl Row for MySqlRow {
self.try_get(index)
}
#[allow(clippy::needless_lifetimes)]
fn get_raw<'r, I: ColumnIndex<Self>>(&'r self, index: I) -> MySqlRawValue<'r> {
self.get_raw(index)
}
#[allow(clippy::needless_lifetimes)]
fn try_get_raw<'r, I: ColumnIndex<Self>>(&'r self, index: I) -> Result<MySqlRawValue<'r>> {
self.try_get_raw(index)