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 Expr::asterisk() and Expr::tbl_asterisk(table: DynIden) methods - Fix #217 #219

Merged
merged 6 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
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::Wildcard => {
write!(sql, "*").unwrap();
}
ColumnRef::TableWildcard(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 wildcard without table prefix.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::wildcard())
/// .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 wildcard() -> Self {
Self::col(ColumnRef::Wildcard)
}

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

/// Express the wildcard with table prefix.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::wildcard())
/// .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_wildcard(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_wildcard<T>(t: T) -> Self
where
T: IntoIden
{
Self::col(ColumnRef::TableWildcard(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),
Wildcard,
TableWildcard(DynIden)
Copy link
Member

Choose a reason for hiding this comment

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

@billy1624 how will this affect SeaORM / SeaSchema?

Copy link
Member

Choose a reason for hiding this comment

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

No impact for SeaSchema. But this part of code has to be updated in SeaORM.
https://github.com/SeaQL/sea-orm/blob/82bad4a376efb875ac32ab601ac2cd9cce191e3f/src/query/combine.rs#L45-L62

}

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::wildcard())
.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_wildcard(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::wildcard())
.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_wildcard(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::wildcard())
.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_wildcard(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