Unify API
This commit is contained in:
parent
711202d4d5
commit
a77d341818
@ -108,7 +108,7 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> {
|
|||||||
.left_join(cake::Relation::Fruit)
|
.left_join(cake::Relation::Fruit)
|
||||||
.select_only()
|
.select_only()
|
||||||
.column(cake::Column::Name)
|
.column(cake::Column::Name)
|
||||||
.expr_as(fruit::Column::Id.count(), "num_of_fruits")
|
.column_as(fruit::Column::Id.count(), "num_of_fruits")
|
||||||
.group_by(cake::Column::Name);
|
.group_by(cake::Column::Name);
|
||||||
|
|
||||||
let results = select.into_model::<SelectResult>().all(db).await?;
|
let results = select.into_model::<SelectResult>().all(db).await?;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ColumnTrait, ModelTrait, PrimaryKeyTrait, RelationBuilder, RelationTrait, RelationType, Select, PrimaryKeyOfModel
|
ColumnTrait, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, RelationBuilder, RelationTrait,
|
||||||
|
RelationType, Select,
|
||||||
};
|
};
|
||||||
use sea_query::{Iden, IntoIden, Value};
|
use sea_query::{Iden, IntoIden, Value};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -65,7 +66,8 @@ pub trait EntityTrait: EntityName {
|
|||||||
/// ```
|
/// ```
|
||||||
fn find_by<V>(v: V) -> Select<Self>
|
fn find_by<V>(v: V) -> Select<Self>
|
||||||
where
|
where
|
||||||
V: Into<Value>, Self::PrimaryKey: PrimaryKeyOfModel<Self::Model>
|
V: Into<Value>,
|
||||||
|
Self::PrimaryKey: PrimaryKeyOfModel<Self::Model>,
|
||||||
{
|
{
|
||||||
let mut select = Self::find();
|
let mut select = Self::find();
|
||||||
if let Some(key) = Self::PrimaryKey::iter().next() {
|
if let Some(key) = Self::PrimaryKey::iter().next() {
|
||||||
|
@ -6,7 +6,8 @@ use core::fmt::Debug;
|
|||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
pub use sea_query::JoinType;
|
pub use sea_query::JoinType;
|
||||||
use sea_query::{
|
use sea_query::{
|
||||||
Alias, Expr, Iden, IntoIden, Order, QueryBuilder, SelectExpr, SelectStatement, SimpleExpr,
|
Alias, Expr, Iden, IntoColumnRef, IntoIden, Order, QueryBuilder, SelectExpr, SelectStatement,
|
||||||
|
SimpleExpr,
|
||||||
};
|
};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -19,6 +20,25 @@ where
|
|||||||
pub(crate) entity: PhantomData<E>,
|
pub(crate) entity: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait IntoSimpleExpr {
|
||||||
|
fn into_simple_expr(self) -> SimpleExpr;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C> IntoSimpleExpr for C
|
||||||
|
where
|
||||||
|
C: ColumnTrait,
|
||||||
|
{
|
||||||
|
fn into_simple_expr(self) -> SimpleExpr {
|
||||||
|
SimpleExpr::Column(self.as_column_ref().into_column_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoSimpleExpr for SimpleExpr {
|
||||||
|
fn into_simple_expr(self) -> SimpleExpr {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<E: 'static> Select<E>
|
impl<E: 'static> Select<E>
|
||||||
where
|
where
|
||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
@ -96,51 +116,34 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn column<C>(mut self, col: C) -> Self
|
pub fn column<C>(mut self, col: C) -> Self
|
||||||
where
|
where
|
||||||
C: ColumnTrait,
|
C: IntoSimpleExpr,
|
||||||
{
|
{
|
||||||
self.query.column(col.as_column_ref());
|
self.query.expr(SelectExpr {
|
||||||
|
expr: col.into_simple_expr(),
|
||||||
|
alias: None,
|
||||||
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a group by column
|
/// Add a select column with alias
|
||||||
///
|
|
||||||
/// ```
|
/// ```
|
||||||
/// use sea_orm::{ColumnTrait, EntityTrait, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
/// use sea_orm::{ColumnTrait, EntityTrait, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
||||||
///
|
///
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// cake::Entity::find()
|
/// cake::Entity::find()
|
||||||
/// .select_only()
|
/// .select_only()
|
||||||
/// .column(cake::Column::Name)
|
/// .column_as(cake::Column::Id.count(), "count")
|
||||||
/// .group_by(cake::Column::Name)
|
|
||||||
/// .build(PostgresQueryBuilder)
|
|
||||||
/// .to_string(),
|
|
||||||
/// r#"SELECT "cake"."name" FROM "cake" GROUP BY "cake"."name""#
|
|
||||||
/// );
|
|
||||||
/// ```
|
|
||||||
pub fn group_by<C>(mut self, col: C) -> Self
|
|
||||||
where
|
|
||||||
C: ColumnTrait,
|
|
||||||
{
|
|
||||||
self.query.group_by_col(col.as_column_ref());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add a select expression
|
|
||||||
/// ```
|
|
||||||
/// use sea_orm::{ColumnTrait, EntityTrait, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
|
||||||
///
|
|
||||||
/// assert_eq!(
|
|
||||||
/// cake::Entity::find()
|
|
||||||
/// .select_only()
|
|
||||||
/// .expr_as(cake::Column::Id.count(), "count")
|
|
||||||
/// .build(PostgresQueryBuilder)
|
/// .build(PostgresQueryBuilder)
|
||||||
/// .to_string(),
|
/// .to_string(),
|
||||||
/// r#"SELECT COUNT("cake"."id") AS "count" FROM "cake""#
|
/// r#"SELECT COUNT("cake"."id") AS "count" FROM "cake""#
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn expr_as(mut self, expr: SimpleExpr, alias: &str) -> Self {
|
pub fn column_as<C>(mut self, col: C, alias: &str) -> Self
|
||||||
|
where
|
||||||
|
C: IntoSimpleExpr,
|
||||||
|
{
|
||||||
self.query.expr(SelectExpr {
|
self.query.expr(SelectExpr {
|
||||||
expr,
|
expr: col.into_simple_expr(),
|
||||||
alias: Some(Rc::new(Alias::new(alias))),
|
alias: Some(Rc::new(Alias::new(alias))),
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
@ -177,6 +180,29 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a group by column
|
||||||
|
/// ```
|
||||||
|
/// use sea_orm::{ColumnTrait, EntityTrait, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// cake::Entity::find()
|
||||||
|
/// .select_only()
|
||||||
|
/// .column(cake::Column::Name)
|
||||||
|
/// .group_by(cake::Column::Name)
|
||||||
|
/// .build(PostgresQueryBuilder)
|
||||||
|
/// .to_string(),
|
||||||
|
/// r#"SELECT "cake"."name" FROM "cake" GROUP BY "cake"."name""#
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
pub fn group_by<C>(mut self, col: C) -> Self
|
||||||
|
where
|
||||||
|
C: IntoSimpleExpr,
|
||||||
|
{
|
||||||
|
self.query.add_group_by(vec![col.into_simple_expr()]);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add an order_by expression (ascending)
|
||||||
/// ```
|
/// ```
|
||||||
/// use sea_orm::{EntityTrait, tests_cfg::cake, sea_query::MysqlQueryBuilder};
|
/// use sea_orm::{EntityTrait, tests_cfg::cake, sea_query::MysqlQueryBuilder};
|
||||||
///
|
///
|
||||||
@ -188,11 +214,15 @@ where
|
|||||||
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC"
|
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC"
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn order_by(mut self, col: E::Column) -> Self {
|
pub fn order_by<C>(mut self, col: C) -> Self
|
||||||
self.query.order_by((E::default(), col), Order::Asc);
|
where
|
||||||
|
C: IntoSimpleExpr,
|
||||||
|
{
|
||||||
|
self.query.order_by_expr(col.into_simple_expr(), Order::Asc);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add an order_by expression (descending)
|
||||||
/// ```
|
/// ```
|
||||||
/// use sea_orm::{EntityTrait, tests_cfg::cake, sea_query::MysqlQueryBuilder};
|
/// use sea_orm::{EntityTrait, tests_cfg::cake, sea_query::MysqlQueryBuilder};
|
||||||
///
|
///
|
||||||
@ -204,8 +234,12 @@ where
|
|||||||
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` DESC"
|
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` DESC"
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn order_by_desc(mut self, col: E::Column) -> Self {
|
pub fn order_by_desc<C>(mut self, col: C) -> Self
|
||||||
self.query.order_by((E::default(), col), Order::Desc);
|
where
|
||||||
|
C: IntoSimpleExpr,
|
||||||
|
{
|
||||||
|
self.query
|
||||||
|
.order_by_expr(col.into_simple_expr(), Order::Desc);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user