From eddd873f4d0a5e20333b81fd81f3cd058a06ace7 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 22 Feb 2021 21:35:51 -0800 Subject: [PATCH] feat(core): add Row::get, Row::get_raw --- sqlx-core/src/row.rs | 13 +++++++++++++ sqlx-mysql/src/row.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/sqlx-core/src/row.rs b/sqlx-core/src/row.rs index 19edd974..a4b6df1b 100644 --- a/sqlx-core/src/row.rs +++ b/sqlx-core/src/row.rs @@ -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; + /// Returns the decoded value at the index. + fn get<'r, T, I>(&'r self, index: I) -> T + where + I: ColumnIndex, + T: Decode<'r, Self::Database>; + /// Returns the decoded value at the index. fn try_get<'r, T, I>(&'r self, index: I) -> crate::Result where I: ColumnIndex, 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>( + &'r self, + index: I, + ) -> >::RawValue; + /// Returns the raw representation of the value at the index. #[allow(clippy::needless_lifetimes)] fn try_get_raw<'r, I: ColumnIndex>( diff --git a/sqlx-mysql/src/row.rs b/sqlx-mysql/src/row.rs index 5be80825..6fb9b9ab 100644 --- a/sqlx-mysql/src/row.rs +++ b/sqlx-mysql/src/row.rs @@ -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, + 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 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.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> @@ -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, + T: Decode<'r, MySql>, + { + self.get(index) + } + fn try_get<'r, T, I>(&'r self, index: I) -> Result where I: ColumnIndex, @@ -154,6 +180,11 @@ impl Row for MySqlRow { self.try_get(index) } + #[allow(clippy::needless_lifetimes)] + fn get_raw<'r, I: ColumnIndex>(&'r self, index: I) -> MySqlRawValue<'r> { + self.get_raw(index) + } + #[allow(clippy::needless_lifetimes)] fn try_get_raw<'r, I: ColumnIndex>(&'r self, index: I) -> Result> { self.try_get_raw(index)