Skip to content

Commit

Permalink
Add Expr::asterisk() and Expr::tbl_asterisk(table: DynIden) methods - F…
Browse files Browse the repository at this point in the history
…ix SeaQL#217 (SeaQL#219)

* Add failing tests

* Fix tests: add tbl_ prefix to Table wildcard method

* Fix typo in tests

* Make tests pass

* fix docs tests

* rename wildcard to asterisk
  • Loading branch information
RomainMazB authored Jan 2, 2022
1 parent 0ccd839 commit 4bc3dd1
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ pub trait QueryBuilder: QuotedBuilder {
write!(sql, ".").unwrap();
column.prepare(sql, self.quote());
}
ColumnRef::Asterisk => {
write!(sql, "*").unwrap();
}
ColumnRef::TableAsterisk(table) => {
table.prepare(sql, self.quote());
write!(sql, ".*").unwrap();
}
};
}
SimpleExpr::Tuple(exprs) => {
Expand Down
108 changes: 108 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,58 @@ impl Expr {
}
}

/// Express the asterisk without table prefix.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::asterisk())
/// .from(Char::Table)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT * FROM `character`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT * FROM "character""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT * FROM `character`"#
/// );
/// ```
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .columns(vec![Char::Character, Char::SizeW, Char::SizeH])
/// .from(Char::Table)
/// .and_where(Expr::col((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 asterisk() -> Self {
Self::col(ColumnRef::Asterisk)
}

/// Express the target column without table prefix.
///
/// # Examples
Expand Down Expand Up @@ -146,6 +198,62 @@ impl Expr {
))
}

/// Express the asterisk with table prefix.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::asterisk())
/// .from(Char::Table)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT * FROM `character`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT * FROM "character""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT * FROM `character`"#
/// );
/// ```
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::tbl_asterisk(Char::Table))
/// .column((Font::Table, Font::Name))
/// .from(Char::Table)
/// .inner_join(Font::Table, 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` INNER JOIN `font` ON `character`.`font_id` = `font`.`id`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character".*, "font"."name" FROM "character" INNER JOIN "font" ON "character"."font_id" = "font"."id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `character`.*, `font`.`name` FROM `character` INNER JOIN `font` ON `character`.`font_id` = `font`.`id`"#
/// );
/// ```
pub fn tbl_asterisk<T>(t: T) -> Self
where
T: IntoIden
{
Self::col(ColumnRef::TableAsterisk(t.into_iden()))
}

/// Express the target column with table prefix.
///
/// # Examples
Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub enum ColumnRef {
Column(DynIden),
TableColumn(DynIden, DynIden),
SchemaTableColumn(DynIden, DynIden, DynIden),
Asterisk,
TableAsterisk(DynIden)
}

pub trait IntoColumnRef {
Expand Down
28 changes: 28 additions & 0 deletions tests/mysql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,34 @@ fn select_48() {
);
}

#[test]
fn select_49() {
let statement = sea_query::Query::select()
.expr(Expr::asterisk())
.from(Char::Table)
.to_string(MysqlQueryBuilder);

assert_eq!(
statement,
r#"SELECT * FROM `character`"#
);
}

#[test]
fn select_50() {
let statement = sea_query::Query::select()
.expr(Expr::tbl_asterisk(Char::Table))
.column((Font::Table, Font::Name))
.from(Char::Table)
.inner_join(Font::Table, Expr::tbl(Char::Table, Char::FontId).equals(Font::Table, Font::Id))
.to_string(MysqlQueryBuilder);

assert_eq!(
statement,
r#"SELECT `character`.*, `font`.`name` FROM `character` INNER JOIN `font` ON `character`.`font_id` = `font`.`id`"#
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_2() {
Expand Down
28 changes: 28 additions & 0 deletions tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,34 @@ fn select_48() {
);
}

#[test]
fn select_49() {
let statement = sea_query::Query::select()
.expr(Expr::asterisk())
.from(Char::Table)
.to_string(PostgresQueryBuilder);

assert_eq!(
statement,
r#"SELECT * FROM "character""#
);
}

#[test]
fn select_50() {
let statement = sea_query::Query::select()
.expr(Expr::tbl_asterisk(Char::Table))
.column((Font::Table, Font::Name))
.from(Char::Table)
.inner_join(Font::Table, Expr::tbl(Char::Table, Char::FontId).equals(Font::Table, Font::Id))
.to_string(PostgresQueryBuilder);

assert_eq!(
statement,
r#"SELECT "character".*, "font"."name" FROM "character" INNER JOIN "font" ON "character"."font_id" = "font"."id""#
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_2() {
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,34 @@ fn select_48() {
);
}

#[test]
fn select_49() {
let statement = sea_query::Query::select()
.expr(Expr::asterisk())
.from(Char::Table)
.to_string(SqliteQueryBuilder);

assert_eq!(
statement,
r#"SELECT * FROM `character`"#
);
}

#[test]
fn select_50() {
let statement = sea_query::Query::select()
.expr(Expr::tbl_asterisk(Char::Table))
.column((Font::Table, Font::Name))
.from(Char::Table)
.inner_join(Font::Table, Expr::tbl(Char::Table, Char::FontId).equals(Font::Table, Font::Id))
.to_string(SqliteQueryBuilder);

assert_eq!(
statement,
r#"SELECT `character`.*, `font`.`name` FROM `character` INNER JOIN `font` ON `character`.`font_id` = `font`.`id`"#
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_2() {
Expand Down

0 comments on commit 4bc3dd1

Please sign in to comment.