Skip to content

Commit

Permalink
Add QuerySelect::columns method (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored and tyt2y3 committed Dec 2, 2022
1 parent cb72d4c commit 0c09c1f
Showing 1 changed file with 66 additions and 0 deletions.
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""#
/// );
/// ```
///
/// 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

0 comments on commit 0c09c1f

Please sign in to comment.