This commit is contained in:
Chris Tsang 2021-05-15 15:07:53 +08:00
parent abc11753a8
commit 4a9fa29e2d
5 changed files with 19 additions and 6 deletions

View File

@ -58,7 +58,10 @@ async fn find_all(db: &Database) -> Result<(), QueryErr> {
async fn find_together(db: &Database) -> Result<(), QueryErr> {
print!("find cakes and fruits: ");
let both = cake::Entity::find().left_join_and_select(fruit::Entity).all(db).await?;
let both = cake::Entity::find()
.left_join_and_select(fruit::Entity)
.all(db)
.await?;
println!();
for bb in both.iter() {

View File

@ -1,5 +1,7 @@
use crate::query::combine;
use crate::{Connection, Database, EntityTrait, FromQueryResult, QueryErr, Select, SelectTwo, Statement};
use crate::{
Connection, Database, EntityTrait, FromQueryResult, QueryErr, Select, SelectTwo, Statement,
};
use sea_query::{QueryBuilder, SelectStatement};
use std::marker::PhantomData;
@ -115,7 +117,10 @@ where
let builder = db.get_query_builder_backend();
self.query.limit(1);
let row = db.get_connection().query_one(self.build(builder)).await?;
Ok((M::from_query_result(&row, combine::SELECT_A)?, N::from_query_result(&row, combine::SELECT_B)?))
Ok((
M::from_query_result(&row, combine::SELECT_A)?,
N::from_query_result(&row, combine::SELECT_B)?,
))
}
pub async fn all(self, db: &Database) -> Result<Vec<(M, N)>, QueryErr> {
@ -123,7 +128,10 @@ where
let rows = db.get_connection().query_all(self.build(builder)).await?;
let mut models = Vec::new();
for row in rows.into_iter() {
models.push((M::from_query_result(&row, combine::SELECT_A)?, N::from_query_result(&row, combine::SELECT_B)?));
models.push((
M::from_query_result(&row, combine::SELECT_A)?,
N::from_query_result(&row, combine::SELECT_B)?,
));
}
Ok(models)
}

View File

@ -71,7 +71,7 @@ where
#[cfg(test)]
mod tests {
use crate::tests_cfg::{cake, fruit};
use crate::{EntityTrait, ColumnTrait, QueryHelper};
use crate::{ColumnTrait, EntityTrait, QueryHelper};
use sea_query::MysqlQueryBuilder;
#[test]

View File

@ -7,6 +7,7 @@ use std::rc::Rc;
pub trait QueryHelper: Sized {
fn query(&mut self) -> &mut SelectStatement;
/// Clear the selection list
fn select_only(mut self) -> Self {
self.query().clear_selects();
self

View File

@ -1,6 +1,6 @@
use crate::{
ColumnTrait, EntityTrait, Identity, Iterable, ModelTrait, PrimaryKeyOfModel, QueryHelper,
Related, RelationDef, Select, SelectTwo
Related, RelationDef, Select, SelectTwo,
};
pub use sea_query::JoinType;
@ -11,6 +11,7 @@ impl<E> Select<E>
where
E: EntityTrait,
{
/// Apply a where condition using the model's primary key
pub fn belongs_to<R>(self, model: &R::Model) -> Self
where
R: EntityTrait + Related<E>,