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 1 commit
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
Prev Previous commit
Next Next commit
Rename & Refactoring
  • Loading branch information
billy1624 committed Oct 26, 2021
commit 32e2bee553de36702d7b3c3717cc55a4b1d2c0d6
4 changes: 2 additions & 2 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ impl QueryBuilder for PostgresQueryBuilder {
collector: &mut dyn FnMut(Value),
) {
match simple_expr {
SimpleExpr::EnumValue(type_name, expr) => {
let simple_expr = expr.clone().cast_expr_as(type_name);
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),
Expand Down
2 changes: 1 addition & 1 deletion src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub trait QueryBuilder: QuotedBuilder {
SimpleExpr::Keyword(keyword) => {
self.prepare_keyword(keyword, sql, collector);
}
SimpleExpr::EnumValue(_, expr) => {
SimpleExpr::AsEnum(_, expr) => {
self.prepare_simple_expr(expr, sql, collector);
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum SimpleExpr {
Custom(String),
CustomWithValues(String, Vec<Value>),
Keyword(Keyword),
EnumValue(String, Box<SimpleExpr>),
AsEnum(DynIden, Box<SimpleExpr>),
}

impl Expr {
Expand Down Expand Up @@ -1425,12 +1425,11 @@ impl Expr {
self.into()
}

pub fn enum_value<T, I>(type_name: T, expr: I) -> SimpleExpr
pub fn as_enum<T>(self, type_name: T) -> SimpleExpr
where
T: ToString,
I: Into<SimpleExpr>,
T: IntoIden,
{
SimpleExpr::EnumValue(type_name.to_string(), Box::new(expr.into()))
SimpleExpr::AsEnum(type_name.into_iden(), Box::new(self.into()))
}

fn func_with_args(func: Function, args: Vec<SimpleExpr>) -> SimpleExpr {
Expand Down Expand Up @@ -1803,11 +1802,16 @@ impl SimpleExpr {
self.concatenate(right)
}

pub fn cast_expr_as<T>(self, type_name: T) -> Self
pub fn cast_as<T>(self, type_name: T) -> Self
where
T: ToString,
T: IntoIden,
{
Self::FunctionCall(Function::Cast, vec![self])
.binary(BinOper::As, Expr::cust(type_name.to_string().as_str()))
Self::FunctionCall(
Function::Cast,
vec![self.binary(
BinOper::As,
Expr::cust(type_name.into_iden().to_string().as_str()),
)],
)
}
}