Skip to content

Commit

Permalink
Merge pull request #29 from Follpvosten/join-aliases
Browse files Browse the repository at this point in the history
add join_alias methods to SelectStatement
  • Loading branch information
tyt2y3 authored Mar 5, 2021
2 parents 31db459 + e64a54b commit e9a5ea6
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/query/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,48 @@ impl SelectStatement {
self
}

/// Join with other table by [`JoinType`], assigning an alias to the joined table.
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*};
///
/// let query = Query::select()
/// .column(Char::Character)
/// .table_column(Font::Table, Font::Name)
/// .from(Char::Table)
/// .join_alias(
/// JoinType::RightJoin,
/// Font::Table,
/// Alias::new("f"),
/// Expr::tbl(Char::Table, Char::FontId).equals(Font::Table, Font::Id)
/// )
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `character`, `font`.`name` FROM `character` RIGHT JOIN `font` AS `f` ON `character`.`font_id` = `font`.`id`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character", "font"."name" FROM "character" RIGHT JOIN "font" AS "f" ON "character"."font_id" = "font"."id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `character`, `font`.`name` FROM `character` RIGHT JOIN `font` AS `f` ON `character`.`font_id` = `font`.`id`"#
/// );
/// ```
pub fn join_alias<T: 'static, A: 'static>(&mut self, join: JoinType, table: T, alias: A, condition: SimpleExpr) -> &mut Self
where T: Iden, A: Iden {
self.join_alias_dyn(join, Rc::new(table), Rc::new(alias), condition)
}

/// Join with other table by [`JoinType`] and alias, variation of [`SelectStatement::join_alias`].
pub fn join_alias_dyn(&mut self, join: JoinType, table: Rc<dyn Iden>, alias: Rc<dyn Iden>, condition: SimpleExpr) -> &mut Self {
self.join_join(join, TableRef::TableAlias(table, alias), JoinOn::Condition(Box::new(condition)))
}

/// Join with sub-query.
///
/// # Examples
Expand Down

0 comments on commit e9a5ea6

Please sign in to comment.