Skip to content

Commit

Permalink
#1696 fix (#1702)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Jun 13, 2023
1 parent d35cd6a commit 0816faa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build-tools/docker-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 74 additions & 1 deletion src/query/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<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
Expand Down

0 comments on commit 0816faa

Please sign in to comment.