* Test suit load environment variable from env files

* Added expr() and exprs() for QuerySelect trait and Minor typo fix

* fmt

* Added doc testing for the new functions

* Remove excess comment for doc test

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>

* Remove excess comment for doc test

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>

* updated doc tests to make example more realistic

* changed doc test again for more realistic query and added expr_as()

* aligned expr_as() alias input with column_as() input

* update doc test for expr_as() according to previous changes

---------

Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
This commit is contained in:
darkmmon 2023-06-13 21:01:15 +08:00 committed by GitHub
parent d35cd6aa3b
commit 0816faa7f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 2 deletions

View File

@ -3,7 +3,7 @@
# Delete all containers # Delete all containers
# $ docker rm -f $(docker ps -a -q) # $ docker rm -f $(docker ps -a -q)
# #
# Delete all volumns # Delete all volumes
# $ docker volume rm $(docker volume ls -q) # $ docker volume rm $(docker volume ls -q)
# #
# Delete all images # Delete all images

View File

@ -4,7 +4,7 @@ use crate::{
}; };
use sea_query::{ use sea_query::{
Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockType, SeaRc, SelectExpr, Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockType, SeaRc, SelectExpr,
SelectStatement, TableRef, SelectStatement, SimpleExpr, TableRef,
}; };
pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement}; pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement};
@ -446,6 +446,79 @@ pub trait QuerySelect: Sized {
self.query().lock_exclusive(); self.query().lock_exclusive();
self 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<T>(mut self, expr: T) -> Self
where
T: Into<SelectExpr>,
{
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<T, I>(mut self, exprs: I) -> Self
where
T: Into<SelectExpr>,
I: IntoIterator<Item = T>,
{
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<T, A>(&mut self, expr: T, alias: A) -> &mut Self
where
T: Into<SimpleExpr>,
A: IntoIdentity,
{
self.query().expr_as(expr, alias.into_identity());
self
}
} }
// LINT: when the column does not appear in tables selected from // LINT: when the column does not appear in tables selected from