diff --git a/build-tools/docker-create.sh b/build-tools/docker-create.sh index cf5aac4ea..8d22ee1e7 100644 --- a/build-tools/docker-create.sh +++ b/build-tools/docker-create.sh @@ -3,7 +3,7 @@ # Delete all containers # $ docker rm -f $(docker ps -a -q) # -# Delete all volumns +# Delete all volumes # $ docker volume rm $(docker volume ls -q) # # Delete all images diff --git a/src/query/helper.rs b/src/query/helper.rs index ef754dcf9..43bc9da9f 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -4,7 +4,7 @@ use crate::{ }; use sea_query::{ Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockType, SeaRc, SelectExpr, - SelectStatement, TableRef, + SelectStatement, SimpleExpr, TableRef, }; pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement}; @@ -446,6 +446,79 @@ pub trait QuerySelect: Sized { self.query().lock_exclusive(); self } + + /// Add an expression to the select expression list. + /// ``` + /// use sea_orm::sea_query::Expr; + /// use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait}; + /// + /// assert_eq!( + /// cake::Entity::find() + /// .select_only() + /// .expr(Expr::col((cake::Entity, cake::Column::Id))) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `cake`.`id` FROM `cake`" + /// ); + /// ``` + fn expr(mut self, expr: T) -> Self + where + T: Into, + { + self.query().expr(expr); + self + } + + /// Add select expressions from vector of [`SelectExpr`]. + /// ``` + /// use sea_orm::sea_query::Expr; + /// use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait}; + /// + /// assert_eq!( + /// cake::Entity::find() + /// .select_only() + /// .exprs([ + /// Expr::col((cake::Entity, cake::Column::Id)), + /// Expr::col((cake::Entity, cake::Column::Name)), + /// ]) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake`" + /// ); + /// ``` + fn exprs(mut self, exprs: I) -> Self + where + T: Into, + I: IntoIterator, + { + self.query().exprs(exprs); + self + } + + /// Select column. + /// ``` + /// use sea_orm::sea_query::{Alias, Expr, Func}; + /// use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait}; + /// + /// assert_eq!( + /// cake::Entity::find() + /// .expr_as( + /// Func::upper(Expr::col((cake::Entity, cake::Column::Name))), + /// "name_upper" + /// ) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `cake`.`id`, `cake`.`name`, UPPER(`cake`.`name`) AS `name_upper` FROM `cake`" + /// ); + /// ``` + fn expr_as(&mut self, expr: T, alias: A) -> &mut Self + where + T: Into, + A: IntoIdentity, + { + self.query().expr_as(expr, alias.into_identity()); + self + } } // LINT: when the column does not appear in tables selected from