Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1696 fix #1702

Merged
merged 12 commits into from
Jun 13, 2023
Merged

#1696 fix #1702

merged 12 commits into from
Jun 13, 2023

Conversation

darkmmon
Copy link
Contributor

PR Info

New Features

  • expr() and exprs() for QuerySelect trait

Changes

  • typo fix in docker-create.sh

@tyt2y3
Copy link
Member

tyt2y3 commented Jun 12, 2023

Is it ready to review?

@darkmmon
Copy link
Contributor Author

Yes. I added the functions and appropriate doc test already.

src/query/helper.rs Outdated Show resolved Hide resolved
@tyt2y3
Copy link
Member

tyt2y3 commented Jun 12, 2023

Next time please make a better PR title

src/query/helper.rs Outdated Show resolved Hide resolved
Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @darkmmon, good first PR!

Take below for example, my imaginated real life example

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)))
        .expr(Expr::col((cake::Entity, cake::Column::Name)))
        .build(DbBackend::MySql)
        .to_string(),
    "SELECT `cake`.`id`, `cake`.`name` FROM `cake`"
);
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`"
);

Note that I import sea_orm::sea_query::Expr instead of sea_query::Expr. Because the end user does not necessary depends on sea_query but it must depends on sea_orm. So, we better direct user to import the sea_query symbols re-exported by sea_orm. Hope that make sense :)

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A feature creep: I think we can add expr_as as well
https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html#method.expr_as

use sea_orm::sea_query::{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`"
);

With a real life example. Copy and paste is okay. But feel free to add your own style :)

@darkmmon
Copy link
Contributor Author

Thanks @billy1624 and @tyt2y3 for all the suggestions! I integrated them to the code.

Comment on lines 514 to 521
fn expr_as<T, A>(&mut self, expr: T, alias: A) -> &mut Self
where
T: Into<SimpleExpr>,
A: IntoIden,
{
self.query().expr_as(expr, alias);
self
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final change, we may well align it with QuerySelect::column_as method and take an alias of I: IntoIdentity

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hint: better update the example to supply an &str as the alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment on lines 514 to 521
fn expr_as<T, A>(&mut self, expr: T, alias: A) -> &mut Self
where
T: Into<SimpleExpr>,
A: IntoIden,
{
self.query().expr_as(expr, alias);
self
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hint: better update the example to supply an &str as the alias

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well done!

@billy1624 billy1624 merged commit 0816faa into SeaQL:master Jun 13, 2023
darkmmon added a commit to darkmmon/seaql.github.io that referenced this pull request Jul 12, 2023
billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Jul 19, 2023
* Optional Field SeaQL/sea-orm#1513

* .gitignore SeaQL/sea-orm#1334

* migration table custom name SeaQL/sea-orm#1511

* OR condition relation SeaQL/sea-orm#1433

* DerivePartialModel SeaQL/sea-orm#1597

* space for migration file naming SeaQL/sea-orm#1570

* composite key up to 12 SeaQL/sea-orm#1508

* seaography integration SeaQL/sea-orm#1599

* QuerySelect SimpleExpr SeaQL/sea-orm#1702

* sqlErr SeaQL/sea-orm#1707

* migration check SeaQL/sea-orm#1519

* postgres array SeaQL/sea-orm#1565

* param intoString SeaQL/sea-orm#1439

* **skipped** re-export SeaQL/sea-orm#1661

* ping SeaQL/sea-orm#1627

* on empty do nothing SeaQL/sea-orm#1708

* on conflict do nothing SeaQL/sea-orm#1712

* **skipped** upgrade versions

* active enum fail safe SeaQL/sea-orm#1374

* relation generation check SeaQL/sea-orm#1435

* entity generation bug SeaQL/sea-schema#105

* **skipped** bug fix that does not require edits

* EnumIter change SeaQL/sea-orm#1535

* completed and fixed a previous todo SeaQL/sea-orm#1570

* amended wordings and structures

* Edit

* Remove temp file

---------

Co-authored-by: Billy Chan <[email protected]>
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

🎉 Released In 0.12.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants