docs
This commit is contained in:
parent
0be16bad16
commit
3cd834d40c
@ -203,7 +203,7 @@ impl MockDatabaseTrait for MockDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MockRow {
|
impl MockRow {
|
||||||
/// Try to get the values of a [MockRow] and fail gracefully on error
|
/// Get a value from the [MockRow]
|
||||||
pub fn try_get<T>(&self, col: &str) -> Result<T, DbErr>
|
pub fn try_get<T>(&self, col: &str) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: ValueType,
|
T: ValueType,
|
||||||
@ -211,6 +211,7 @@ impl MockRow {
|
|||||||
T::try_from(self.values.get(col).unwrap().clone()).map_err(|e| DbErr::Type(e.to_string()))
|
T::try_from(self.values.get(col).unwrap().clone()).map_err(|e| DbErr::Type(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a value from the [MockRow] based on the order of column name
|
||||||
pub fn try_get_by_index<T>(&self, idx: usize) -> Result<T, DbErr>
|
pub fn try_get_by_index<T>(&self, idx: usize) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: ValueType,
|
T: ValueType,
|
||||||
|
@ -53,7 +53,7 @@ impl From<TryGetError> for DbErr {
|
|||||||
// QueryResult //
|
// QueryResult //
|
||||||
|
|
||||||
impl QueryResult {
|
impl QueryResult {
|
||||||
/// Get a Row from a Column
|
/// Get a value from the query result with prefixed column name
|
||||||
pub fn try_get<T>(&self, pre: &str, col: &str) -> Result<T, DbErr>
|
pub fn try_get<T>(&self, pre: &str, col: &str) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: TryGetable,
|
T: TryGetable,
|
||||||
@ -61,6 +61,7 @@ impl QueryResult {
|
|||||||
Ok(T::try_get(self, pre, col)?)
|
Ok(T::try_get(self, pre, col)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a value from the query result based on the order in the select expressions
|
||||||
pub fn try_get_by_index<T>(&self, idx: usize) -> Result<T, DbErr>
|
pub fn try_get_by_index<T>(&self, idx: usize) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: TryGetable,
|
T: TryGetable,
|
||||||
@ -68,7 +69,7 @@ impl QueryResult {
|
|||||||
Ok(T::try_get_by_index(self, idx)?)
|
Ok(T::try_get_by_index(self, idx)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform query operations on multiple Columns
|
/// Get a tuple value from the query result with prefixed column name
|
||||||
pub fn try_get_many<T>(&self, pre: &str, cols: &[String]) -> Result<T, DbErr>
|
pub fn try_get_many<T>(&self, pre: &str, cols: &[String]) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: TryGetableMany,
|
T: TryGetableMany,
|
||||||
@ -76,6 +77,7 @@ impl QueryResult {
|
|||||||
Ok(T::try_get_many(self, pre, cols)?)
|
Ok(T::try_get_many(self, pre, cols)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a tuple value from the query result based on the order in the select expressions
|
||||||
pub fn try_get_many_by_index<T>(&self) -> Result<T, DbErr>
|
pub fn try_get_many_by_index<T>(&self) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: TryGetableMany,
|
T: TryGetableMany,
|
||||||
@ -1175,13 +1177,13 @@ fn try_get_many_with_slice_len_of(len: usize, cols: &[String]) -> Result<(), Try
|
|||||||
|
|
||||||
// TryGetableFromJson //
|
// TryGetableFromJson //
|
||||||
|
|
||||||
/// Perform a query on multiple columns
|
/// An interface to get a JSON from the query result
|
||||||
#[cfg(feature = "with-json")]
|
#[cfg(feature = "with-json")]
|
||||||
pub trait TryGetableFromJson: Sized
|
pub trait TryGetableFromJson: Sized
|
||||||
where
|
where
|
||||||
for<'de> Self: serde::Deserialize<'de>,
|
for<'de> Self: serde::Deserialize<'de>,
|
||||||
{
|
{
|
||||||
/// Ensure the type implements this method
|
/// Get a JSON from the query result with prefixed column name
|
||||||
#[allow(unused_variables, unreachable_code)]
|
#[allow(unused_variables, unreachable_code)]
|
||||||
fn try_get_from_json(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
|
fn try_get_from_json(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
|
||||||
let column = format!("{}{}", pre, col);
|
let column = format!("{}{}", pre, col);
|
||||||
@ -1223,6 +1225,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a JSON from the query result based on the order in the select expressions
|
||||||
fn try_get_from_json_by_index(res: &QueryResult, idx: usize) -> Result<Self, TryGetError> {
|
fn try_get_from_json_by_index(res: &QueryResult, idx: usize) -> Result<Self, TryGetError> {
|
||||||
match &res.row {
|
match &res.row {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
|
@ -41,7 +41,7 @@ pub trait SelectorTrait {
|
|||||||
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, DbErr>;
|
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, DbErr>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform an operation on an entity that can yield a Value
|
/// Get tuple from query result based on a list of column identifiers
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SelectGetableValue<T, C>
|
pub struct SelectGetableValue<T, C>
|
||||||
where
|
where
|
||||||
@ -52,6 +52,7 @@ where
|
|||||||
model: PhantomData<T>,
|
model: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get tuple from query result based on column index
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SelectGetableTuple<T>
|
pub struct SelectGetableTuple<T>
|
||||||
where
|
where
|
||||||
@ -535,6 +536,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get tuple from query result based on column index
|
||||||
pub fn into_tuple<T>(query: SelectStatement) -> Selector<SelectGetableTuple<T>>
|
pub fn into_tuple<T>(query: SelectStatement) -> Selector<SelectGetableTuple<T>>
|
||||||
where
|
where
|
||||||
T: TryGetableMany,
|
T: TryGetableMany,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user