Skip to content

Commit

Permalink
add into_model() for Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhannadAlrusayni committed Sep 16, 2021
1 parent 38a4d3a commit 9107c90
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
71 changes: 70 additions & 1 deletion src/database/statement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::DbBackend;
use crate::{DbBackend, FromQueryResult, SelectModel, SelectorRaw};
use sea_query::{inject_parameters, MysqlQueryBuilder, PostgresQueryBuilder, SqliteQueryBuilder};
pub use sea_query::{Value, Values};
use std::fmt;
Expand Down Expand Up @@ -43,6 +43,75 @@ impl Statement {
db_backend,
}
}

/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
/// # maplit::btreemap! {
/// # "name" => Into::<Value>::into("Chocolate Forest"),
/// # "num_of_cakes" => Into::<Value>::into(1),
/// # },
/// # maplit::btreemap! {
/// # "name" => Into::<Value>::into("New York Cheese"),
/// # "num_of_cakes" => Into::<Value>::into(1),
/// # },
/// # ]])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, FromQueryResult};
///
/// #[derive(Debug, PartialEq, FromQueryResult)]
/// struct SelectResult {
/// name: String,
/// num_of_cakes: i32,
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<SelectResult> = Statement::from_sql_and_values(
/// DbBackend::Postgres,
/// r#"SELECT "cake"."name", count("cake"."id") AS "num_of_cakes" FROM "cake""#,
/// vec![],
/// )
/// .into_model::<SelectResult>()
/// .all(&db)
/// .await?;
///
/// assert_eq!(
/// res,
/// vec![
/// SelectResult {
/// name: "Chocolate Forest".to_owned(),
/// num_of_cakes: 1,
/// },
/// SelectResult {
/// name: "New York Cheese".to_owned(),
/// num_of_cakes: 1,
/// },
/// ]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
/// vec![Transaction::from_sql_and_values(
/// DbBackend::Postgres,
/// r#"SELECT "cake"."name", count("cake"."id") AS "num_of_cakes" FROM "cake""#,
/// vec![]
/// ),]
/// );
/// ```
pub fn into_model<M>(self) -> SelectorRaw<SelectModel<M>>
where
M: FromQueryResult,
{
SelectorRaw::<SelectModel<M>>::from_statement(self)
}
}

impl fmt::Display for Statement {
Expand Down
14 changes: 14 additions & 0 deletions src/executor/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ impl<S> SelectorRaw<S>
where
S: SelectorTrait,
{
// Create `SelectorRaw` from Statment. Executing this `SelectorRaw` will
// return a type `M` which implement `FromQueryResult`.
//
// Helper function used by `Statment.into_model()`
pub(crate) fn from_statement<M>(stmt: Statement) -> SelectorRaw<SelectModel<M>>
where
M: FromQueryResult,
{
SelectorRaw {
stmt,
selector: SelectModel { model: PhantomData },
}
}

/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
Expand Down

0 comments on commit 9107c90

Please sign in to comment.