From 7b5cce42e7bc9b36d1aff6d5f47981dea103afae Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 12 Jun 2023 16:56:10 +0800 Subject: [PATCH 01/10] Test suit load environment variable from env files --- .gitignore | 3 ++- Cargo.toml | 1 + tests/basic.rs | 3 +++ tests/common/mod.rs | 3 +++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 558044adf..74509beb9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ Cargo.lock *.sublime* .vscode .idea/* -*/.idea/* \ No newline at end of file +*/.idea/* +.env.local \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f5aaecd68..8e1821514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,7 @@ time = { version = "0.3", features = ["macros"] } uuid = { version = "1", features = ["v4"] } once_cell = "1.8" arraystring = "0.3" +dotenv = "0.15" [features] debug-print = [] diff --git a/tests/basic.rs b/tests/basic.rs index 8c466ea60..54a9f47c0 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -8,6 +8,9 @@ pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Databa #[sea_orm_macros::test] #[cfg(feature = "sqlx-sqlite")] async fn main() -> Result<(), DbErr> { + dotenv::from_filename(".env.local").ok(); + dotenv::from_filename(".env").ok(); + let base_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite::memory:".to_owned()); let db: DbConn = Database::connect(&base_url).await?; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 68d986b60..359bc9dbd 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -13,6 +13,9 @@ pub struct TestContext { impl TestContext { pub async fn new(test_name: &str) -> Self { + dotenv::from_filename(".env.local").ok(); + dotenv::from_filename(".env").ok(); + let base_url = std::env::var("DATABASE_URL").expect("Enviroment variable 'DATABASE_URL' not set"); let db: DatabaseConnection = setup::setup(&base_url, test_name).await; From 0215d8d8190d6c79c5f7446fcd8f3a521003bb44 Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Mon, 12 Jun 2023 17:14:43 +0800 Subject: [PATCH 02/10] Added expr() and exprs() for QuerySelect trait and Minor typo fix --- build-tools/docker-create.sh | 2 +- src/query/helper.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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..95d5f6b3b 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -446,6 +446,25 @@ pub trait QuerySelect: Sized { self.query().lock_exclusive(); self } + + /// Add an expression to the select expression list. + fn expr(&mut self, expr: T) -> &mut Self + where + T: Into, + { + self.query().expr(expr); + self + } + + /// Add select expressions from vector of [`SelectExpr`]. + fn exprs(&mut self, exprs: I) -> &mut Self + where + T: Into, + I: IntoIterator, + { + self.query().exprs(exprs); + self + } } // LINT: when the column does not appear in tables selected from From bd930881a1635ab5e3d24f404e9b11c8df9597fb Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Mon, 12 Jun 2023 17:26:35 +0800 Subject: [PATCH 03/10] fmt --- src/query/helper.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 95d5f6b3b..72669fbb7 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -448,8 +448,8 @@ pub trait QuerySelect: Sized { } /// Add an expression to the select expression list. - fn expr(&mut self, expr: T) -> &mut Self - where + fn expr(&mut self, expr: T) -> &mut Self + where T: Into, { self.query().expr(expr); From fa3c75aff8560531175def13624c68dd549871f1 Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Mon, 12 Jun 2023 20:50:36 +0800 Subject: [PATCH 04/10] Added doc testing for the new functions --- src/query/helper.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 72669fbb7..2e0881e84 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -448,7 +448,21 @@ pub trait QuerySelect: Sized { } /// Add an expression to the select expression list. - fn expr(&mut self, expr: T) -> &mut Self + /// ``` + /// use sea_orm::QueryTrait; + /// use sea_orm::{entity::*, query::QuerySelect, tests_cfg::cake, DbBackend}; + /// use sea_query::{Alias, Expr}; + /// let mut find = cake::Entity::find(); + /// + /// assert_eq!( + /// cake::Entity::find() + /// .expr(Expr::col(Alias::new("some_column"))) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `cake`.`id`, `cake`.`name`, `some_column` FROM `cake`" + /// ); + /// ``` + fn expr(mut self, expr: T) -> Self where T: Into, { @@ -457,7 +471,25 @@ pub trait QuerySelect: Sized { } /// Add select expressions from vector of [`SelectExpr`]. - fn exprs(&mut self, exprs: I) -> &mut Self + /// ``` + /// use sea_orm::QueryTrait; + /// use sea_orm::{entity::*, query::QuerySelect, tests_cfg::cake, DbBackend}; + /// use sea_query::{Alias, Expr}; + /// let mut find = cake::Entity::find(); + /// + /// assert_eq!( + /// // new method + /// cake::Entity::find() + /// .exprs([ + /// Expr::col(Alias::new("some_column")), + /// Expr::col(Alias::new("some_other_column")), + /// ]) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `cake`.`id`, `cake`.`name`, `some_column`, `some_other_column` FROM `cake`" + /// ); + /// ``` + fn exprs(mut self, exprs: I) -> Self where T: Into, I: IntoIterator, From c45475f48e945dfff5c5462acc3599aacca3a492 Mon Sep 17 00:00:00 2001 From: darkmmon <115683888+darkmmon@users.noreply.github.com> Date: Tue, 13 Jun 2023 10:00:19 +0800 Subject: [PATCH 05/10] Remove excess comment for doc test Co-authored-by: Chris Tsang --- src/query/helper.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 2e0881e84..535462990 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -478,7 +478,6 @@ pub trait QuerySelect: Sized { /// let mut find = cake::Entity::find(); /// /// assert_eq!( - /// // new method /// cake::Entity::find() /// .exprs([ /// Expr::col(Alias::new("some_column")), From 3c000f88a0afa004f467a4a4b3552808e2eef702 Mon Sep 17 00:00:00 2001 From: darkmmon <115683888+darkmmon@users.noreply.github.com> Date: Tue, 13 Jun 2023 10:00:19 +0800 Subject: [PATCH 06/10] Remove excess comment for doc test Co-authored-by: Chris Tsang --- src/query/helper.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 2e0881e84..535462990 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -478,7 +478,6 @@ pub trait QuerySelect: Sized { /// let mut find = cake::Entity::find(); /// /// assert_eq!( - /// // new method /// cake::Entity::find() /// .exprs([ /// Expr::col(Alias::new("some_column")), From d0acc86d7f160a19caadb691690882a60a42512a Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Tue, 13 Jun 2023 10:23:20 +0800 Subject: [PATCH 07/10] updated doc tests to make example more realistic --- src/query/helper.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 535462990..2a92d3cca 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -456,10 +456,10 @@ pub trait QuerySelect: Sized { /// /// assert_eq!( /// cake::Entity::find() - /// .expr(Expr::col(Alias::new("some_column"))) + /// .expr(Expr::col(Alias::new("price"))) /// .build(DbBackend::MySql) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name`, `some_column` FROM `cake`" + /// "SELECT `cake`.`id`, `cake`.`name`, `price` FROM `cake`" /// ); /// ``` fn expr(mut self, expr: T) -> Self @@ -480,12 +480,12 @@ pub trait QuerySelect: Sized { /// assert_eq!( /// cake::Entity::find() /// .exprs([ - /// Expr::col(Alias::new("some_column")), - /// Expr::col(Alias::new("some_other_column")), + /// Expr::col(Alias::new("price")), + /// Expr::col(Alias::new("stock")), /// ]) /// .build(DbBackend::MySql) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name`, `some_column`, `some_other_column` FROM `cake`" + /// "SELECT `cake`.`id`, `cake`.`name`, `price`, `stock` FROM `cake`" /// ); /// ``` fn exprs(mut self, exprs: I) -> Self From 8e6d2d91c2d1eb4d83d23b17013cc837f87b2faa Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Tue, 13 Jun 2023 11:44:59 +0800 Subject: [PATCH 08/10] changed doc test again for more realistic query and added expr_as() --- src/query/helper.rs | 51 ++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 2a92d3cca..7a5f53dbe 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}; @@ -449,17 +449,16 @@ pub trait QuerySelect: Sized { /// Add an expression to the select expression list. /// ``` - /// use sea_orm::QueryTrait; - /// use sea_orm::{entity::*, query::QuerySelect, tests_cfg::cake, DbBackend}; - /// use sea_query::{Alias, Expr}; - /// let mut find = cake::Entity::find(); + /// use sea_orm::sea_query::Expr; + /// use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait}; /// /// assert_eq!( /// cake::Entity::find() - /// .expr(Expr::col(Alias::new("price"))) + /// .select_only() + /// .expr(Expr::col((cake::Entity, cake::Column::Id))) /// .build(DbBackend::MySql) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name`, `price` FROM `cake`" + /// "SELECT `cake`.`id` FROM `cake`" /// ); /// ``` fn expr(mut self, expr: T) -> Self @@ -472,20 +471,19 @@ pub trait QuerySelect: Sized { /// Add select expressions from vector of [`SelectExpr`]. /// ``` - /// use sea_orm::QueryTrait; - /// use sea_orm::{entity::*, query::QuerySelect, tests_cfg::cake, DbBackend}; - /// use sea_query::{Alias, Expr}; - /// let mut find = cake::Entity::find(); + /// 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(Alias::new("price")), - /// Expr::col(Alias::new("stock")), + /// Expr::col((cake::Entity, cake::Column::Id)), + /// Expr::col((cake::Entity, cake::Column::Name)), /// ]) /// .build(DbBackend::MySql) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name`, `price`, `stock` FROM `cake`" + /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake`" /// ); /// ``` fn exprs(mut self, exprs: I) -> Self @@ -496,6 +494,31 @@ pub trait QuerySelect: Sized { 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))), + /// Alias::new("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: IntoIden, + { + self.query().expr_as(expr, alias); + self + } } // LINT: when the column does not appear in tables selected from From 5e022316cca5dc7f3634126ea1e95fbb975da124 Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Tue, 13 Jun 2023 12:24:52 +0800 Subject: [PATCH 09/10] aligned expr_as() alias input with column_as() input --- src/query/helper.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 7a5f53dbe..c3c5a78f8 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -514,9 +514,9 @@ pub trait QuerySelect: Sized { fn expr_as(&mut self, expr: T, alias: A) -> &mut Self where T: Into, - A: IntoIden, + A: IntoIdentity, { - self.query().expr_as(expr, alias); + self.query().expr_as(expr, alias.into_identity()); self } } From db06b622307f52f5a15f6e82dcebe89824459371 Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Tue, 13 Jun 2023 13:07:36 +0800 Subject: [PATCH 10/10] update doc test for expr_as() according to previous changes --- src/query/helper.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index c3c5a78f8..43bc9da9f 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -504,7 +504,7 @@ pub trait QuerySelect: Sized { /// cake::Entity::find() /// .expr_as( /// Func::upper(Expr::col((cake::Entity, cake::Column::Name))), - /// Alias::new("name_upper") + /// "name_upper" /// ) /// .build(DbBackend::MySql) /// .to_string(),