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

Add QuerySelect::columns method #1264

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/query/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,72 @@ pub trait QuerySelect: Sized {
self
}

/// Select columns
///
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
///
/// assert_eq!(
/// cake::Entity::find()
/// .select_only()
/// .columns([cake::Column::Id, cake::Column::Name])
/// .build(DbBackend::Postgres)
/// .to_string(),
/// r#"SELECT "cake"."id", "cake"."name" FROM "cake""#
/// );
/// ```
///
/// Conditionally select all columns expect a specific column
///
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
///
/// assert_eq!(
/// cake::Entity::find()
/// .select_only()
/// .columns(cake::Column::iter().filter(|col| match col {
/// cake::Column::Id => false,
/// _ => true,
/// }))
/// .build(DbBackend::Postgres)
/// .to_string(),
/// r#"SELECT "cake"."name" FROM "cake""#
/// );
/// ```
Comment on lines +115 to +131
Copy link
Member Author

Choose a reason for hiding this comment

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

match were used because neither Column nor ColumnTrait implemented Eq. And we cannot implement Eq in Column, it will cause nasty conflicting method name since we already have ColumnTrait::eq method.

///
/// Enum column will be casted into text (PostgreSQL only)
///
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::lunch_set, DbBackend};
///
/// assert_eq!(
/// lunch_set::Entity::find()
/// .select_only()
/// .columns([lunch_set::Column::Name, lunch_set::Column::Tea])
/// .build(DbBackend::Postgres)
/// .to_string(),
/// r#"SELECT "lunch_set"."name", CAST("lunch_set"."tea" AS text) FROM "lunch_set""#
/// );
/// assert_eq!(
/// lunch_set::Entity::find()
/// .select_only()
/// .columns([lunch_set::Column::Name, lunch_set::Column::Tea])
/// .build(DbBackend::MySql)
/// .to_string(),
/// r#"SELECT `lunch_set`.`name`, `lunch_set`.`tea` FROM `lunch_set`"#
/// );
/// ```
fn columns<C, I>(mut self, cols: I) -> Self
where
C: ColumnTrait,
I: IntoIterator<Item = C>,
{
for col in cols.into_iter() {
self = self.column(col);
}
self
}

/// Add an offset expression
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
Expand Down