Skip to content

Commit

Permalink
feat: add Expr::column
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Dec 31, 2024
1 parent 605ec44 commit a7c055a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,62 @@ impl Expr {
Self::new_with_left(n.into_column_ref())
}

/// Express the target column without table prefix, returning a [`SimpleExpr`].
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .columns([Char::Character, Char::SizeW, Char::SizeH])
/// .from(Char::Table)
/// .and_where(Expr::column(Char::SizeW).eq(1))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` = 1"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" = 1"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" = 1"#
/// );
/// ```
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .columns([Char::Character, Char::SizeW, Char::SizeH])
/// .from(Char::Table)
/// .and_where(Expr::column((Char::Table, Char::SizeW)).eq(1))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` = 1"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" = 1"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" = 1"#
/// );
/// ```
pub fn column<T>(n: T) -> SimpleExpr
where
T: IntoColumnRef,
{
SimpleExpr::Column(n.into_column_ref())
}

/// Wraps tuple of `SimpleExpr`, can be used for tuple comparison
///
/// # Examples
Expand Down

0 comments on commit a7c055a

Please sign in to comment.