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

Cast expression as custom type #170

Merged
merged 8 commits into from
Nov 1, 2021
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
15 changes: 15 additions & 0 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ impl QueryBuilder for PostgresQueryBuilder {
_ => self.prepare_function_common(function, sql, collector),
}
}

fn prepare_simple_expr(
&self,
simple_expr: &SimpleExpr,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
match simple_expr {
SimpleExpr::AsEnum(type_name, expr) => {
let simple_expr = expr.clone().cast_as(SeaRc::clone(type_name));
self.prepare_simple_expr_common(&simple_expr, sql, collector);
}
_ => QueryBuilder::prepare_simple_expr_common(self, simple_expr, sql, collector),
}
}
}
12 changes: 12 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ pub trait QueryBuilder: QuotedBuilder {
simple_expr: &SimpleExpr,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
self.prepare_simple_expr_common(simple_expr, sql, collector);
}

fn prepare_simple_expr_common(
&self,
simple_expr: &SimpleExpr,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
match simple_expr {
SimpleExpr::Column(column_ref) => {
Expand Down Expand Up @@ -328,6 +337,9 @@ pub trait QueryBuilder: QuotedBuilder {
SimpleExpr::Keyword(keyword) => {
self.prepare_keyword(keyword, sql, collector);
}
SimpleExpr::AsEnum(_, expr) => {
self.prepare_simple_expr(expr, sql, collector);
}
}
}

Expand Down
89 changes: 89 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum SimpleExpr {
Custom(String),
CustomWithValues(String, Vec<Value>),
Keyword(Keyword),
AsEnum(DynIden, Box<SimpleExpr>),
}

impl Expr {
Expand Down Expand Up @@ -1424,6 +1425,57 @@ impl Expr {
self.into()
}

/// Express a `AS enum` expression.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::col(Char::FontSize).as_enum(Alias::new("text")))
/// .from(Char::Table)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `font_size` FROM `character`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT CAST("font_size" AS text) FROM "character""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `font_size` FROM `character`"#
/// );
///
/// let query = Query::insert()
/// .into_table(Char::Table)
/// .columns(vec![Char::FontSize])
/// .exprs_panic(vec![Expr::val("large").as_enum(Alias::new("FontSizeEnum"))])
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS FontSizeEnum))"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
/// );
/// ```
pub fn as_enum<T>(self, type_name: T) -> SimpleExpr
where
T: IntoIden,
{
SimpleExpr::AsEnum(type_name.into_iden(), Box::new(self.into()))
}

fn func_with_args(func: Function, args: Vec<SimpleExpr>) -> SimpleExpr {
let mut expr = Expr::new();
expr.func = Some(func);
Expand Down Expand Up @@ -1793,4 +1845,41 @@ impl SimpleExpr {
{
self.concatenate(right)
}

/// Express a `CAST AS` expression.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Expr::value("1").cast_as(Alias::new("integer")))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT CAST('1' AS integer)"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT CAST('1' AS integer)"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT CAST('1' AS integer)"#
/// );
/// ```
pub fn cast_as<T>(self, type_name: T) -> Self
where
T: IntoIden,
{
Self::FunctionCall(
Function::Cast,
vec![self.binary(
BinOper::As,
Expr::cust(type_name.into_iden().to_string().as_str()),
)],
)
}
}