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

Move Asteriks from Expr to new type #596

Merged
merged 2 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
103 changes: 4 additions & 99 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,56 +100,9 @@ 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([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"#
/// );
/// ```
#[deprecated(since = "0.29.0", note = "Please use the [`Asterisk`]")]
pub fn asterisk() -> Self {
Self::col(ColumnRef::Asterisk)
Self::col(Asterisk)
}

/// Express the target column without table prefix.
Expand Down Expand Up @@ -245,60 +198,12 @@ 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::table_asterisk(Char::Table))
/// .column((Font::Table, Font::Name))
/// .from(Char::Table)
/// .inner_join(Font::Table, Expr::col((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""#
/// );
/// ```
#[deprecated(since = "0.29.0", note = "Please use the [`Asterisk`]")]
pub fn table_asterisk<T>(t: T) -> Self
where
T: IntoIden,
{
Self::col(ColumnRef::TableAsterisk(t.into_iden()))
Self::col((t.into_iden(), Asterisk))
}

/// Express a [`Value`], returning a [`Expr`].
Expand Down
71 changes: 71 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,62 @@ pub struct Alias(String);
#[derive(Default, Debug, Copy, Clone)]
pub struct NullAlias;

/// Asterisk ("*")
///
/// Express the asterisk without table prefix.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(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""#
/// );
/// ```
///
/// Express the asterisk with table prefix.
///
/// Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .column((Char::Table, Asterisk))
/// .from(Char::Table)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `character`.* FROM `character`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character".* FROM "character""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT "character".* FROM "character""#
/// );
/// ```
#[derive(Default, Debug, Clone, Copy)]
pub struct Asterisk;

/// SQL Keywords
#[derive(Debug, Clone)]
pub enum Keyword {
Expand Down Expand Up @@ -302,6 +358,12 @@ where
}
}

impl IntoColumnRef for Asterisk {
fn into_column_ref(self) -> ColumnRef {
ColumnRef::Asterisk
}
}

impl<S: 'static, T: 'static> IntoColumnRef for (S, T)
where
S: IntoIden,
Expand All @@ -312,6 +374,15 @@ where
}
}

impl<T: 'static> IntoColumnRef for (T, Asterisk)
where
T: IntoIden,
{
fn into_column_ref(self) -> ColumnRef {
ColumnRef::TableAsterisk(self.0.into_iden())
}
}

impl<S: 'static, T: 'static, U: 'static> IntoColumnRef for (S, T, U)
where
S: IntoIden,
Expand Down
8 changes: 4 additions & 4 deletions tests/mysql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ fn select_48a() {
#[test]
fn select_49() {
let statement = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.to_string(MysqlQueryBuilder);

Expand All @@ -802,7 +802,7 @@ fn select_49() {
#[test]
fn select_50() {
let statement = Query::select()
.expr(Expr::table_asterisk(Char::Table))
.column((Char::Table, Asterisk))
.column((Font::Table, Font::Name))
.from(Char::Table)
.inner_join(
Expand Down Expand Up @@ -901,7 +901,7 @@ fn select_53() {
#[test]
fn select_54() {
let statement = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.from(Font::Table)
.and_where(Expr::col((Font::Table, Font::Id)).equals((Char::Table, Char::FontId)))
Expand Down Expand Up @@ -1418,7 +1418,7 @@ fn sub_query_with_fn() {
pub struct ArrayFunc;

let sub_select = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.to_owned();

Expand Down
10 changes: 5 additions & 5 deletions tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ fn select_48a() {
#[test]
fn select_49() {
let statement = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.to_string(PostgresQueryBuilder);

Expand All @@ -786,7 +786,7 @@ fn select_49() {
#[test]
fn select_50() {
let statement = Query::select()
.expr(Expr::table_asterisk(Char::Table))
.column((Char::Table, Asterisk))
.column((Font::Table, Font::Name))
.from(Char::Table)
.inner_join(
Expand Down Expand Up @@ -907,7 +907,7 @@ fn select_54() {
#[test]
fn select_55() {
let statement = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.from(Font::Table)
.and_where(Expr::col((Font::Table, Font::Id)).equals((Char::Table, Char::FontId)))
Expand Down Expand Up @@ -1071,7 +1071,7 @@ fn select_61() {
#[test]
fn select_62() {
let select = SelectStatement::new()
.expr(Expr::asterisk())
.column(Asterisk)
.from_values([(1i32, "hello"), (2, "world")], Alias::new("x"))
.to_owned();
let cte = CommonTableExpression::new()
Expand Down Expand Up @@ -1711,7 +1711,7 @@ fn sub_query_with_fn() {
pub struct ArrayFunc;

let sub_select = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.to_owned();

Expand Down
8 changes: 4 additions & 4 deletions tests/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ fn select_48a() {
#[test]
fn select_49() {
let statement = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.to_string(SqliteQueryBuilder);

Expand All @@ -786,7 +786,7 @@ fn select_49() {
#[test]
fn select_50() {
let statement = Query::select()
.expr(Expr::table_asterisk(Char::Table))
.column((Character::Table, Asterisk))
.column((Font::Table, Font::Name))
.from(Char::Table)
.inner_join(
Expand Down Expand Up @@ -879,7 +879,7 @@ fn select_53() {
#[test]
fn select_54() {
let statement = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.from(Font::Table)
.and_where(Expr::col((Font::Table, Font::Id)).equals((Char::Table, Char::FontId)))
Expand Down Expand Up @@ -1560,7 +1560,7 @@ fn sub_query_with_fn() {
pub struct ArrayFunc;

let sub_select = Query::select()
.expr(Expr::asterisk())
.column(Asterisk)
.from(Char::Table)
.to_owned();

Expand Down