-
-
Notifications
You must be signed in to change notification settings - Fork 521
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
Fix related & linked with enum columns #376
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
use crate::{ | ||
EntityTrait, IdenStatic, IntoSimpleExpr, Iterable, QueryTrait, Select, SelectTwo, SelectTwoMany, | ||
ColumnTrait, EntityTrait, IdenStatic, IntoSimpleExpr, Iterable, QueryTrait, Select, SelectTwo, | ||
SelectTwoMany, | ||
}; | ||
use core::marker::PhantomData; | ||
pub use sea_query::JoinType; | ||
use sea_query::{Alias, ColumnRef, Iden, Order, SeaRc, SelectExpr, SelectStatement, SimpleExpr}; | ||
use sea_query::{ | ||
Alias, ColumnRef, DynIden, Expr, Iden, Order, SeaRc, SelectExpr, SelectStatement, SimpleExpr, | ||
}; | ||
|
||
macro_rules! select_def { | ||
( $ident: ident, $str: expr ) => { | ||
|
@@ -42,10 +45,17 @@ where | |
None => { | ||
let col = match &sel.expr { | ||
SimpleExpr::Column(col_ref) => match &col_ref { | ||
ColumnRef::Column(col) => col, | ||
ColumnRef::TableColumn(_, col) => col, | ||
ColumnRef::Column(col) | ColumnRef::TableColumn(_, col) => col, | ||
}, | ||
SimpleExpr::AsEnum(_, simple_expr) => match simple_expr.as_ref() { | ||
SimpleExpr::Column(col_ref) => match &col_ref { | ||
ColumnRef::Column(col) | ColumnRef::TableColumn(_, col) => col, | ||
}, | ||
_ => { | ||
panic!("cannot apply alias for AsEnum with expr other than Column") | ||
} | ||
}, | ||
_ => panic!("cannot apply alias for expr other than Column"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It panic at this line. It didn't catch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
_ => panic!("cannot apply alias for expr other than Column or AsEnum"), | ||
}; | ||
let alias = format!("{}{}", pre, col.to_string().as_str()); | ||
sel.alias = Some(SeaRc::new(Alias::new(&alias))); | ||
|
@@ -128,10 +138,18 @@ where | |
F: EntityTrait, | ||
S: QueryTrait<QueryStatement = SelectStatement>, | ||
{ | ||
let text_type = SeaRc::new(Alias::new("text")) as DynIden; | ||
for col in <F::Column as Iterable>::iter() { | ||
let col_def = col.def(); | ||
let col_type = col_def.get_column_type(); | ||
let alias = format!("{}{}", SelectB.as_str(), col.as_str()); | ||
let expr = Expr::expr(col.into_simple_expr()); | ||
let expr = match col_type.get_enum_name() { | ||
Some(_) => expr.as_enum(text_type.clone()), | ||
None => expr.into(), | ||
}; | ||
selector.query().expr(SelectExpr { | ||
expr: col.into_simple_expr(), | ||
expr, | ||
alias: Some(SeaRc::new(Alias::new(&alias))), | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
use crate::{ | ||
join_tbl_on_condition, unpack_table_ref, EntityTrait, IdenStatic, Iterable, Linked, | ||
QuerySelect, Related, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, | ||
join_tbl_on_condition, unpack_table_ref, ColumnTrait, EntityTrait, IdenStatic, Iterable, | ||
Linked, QuerySelect, Related, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, | ||
}; | ||
pub use sea_query::JoinType; | ||
use sea_query::{Alias, Expr, IntoIden, SeaRc, SelectExpr}; | ||
use sea_query::{Alias, DynIden, Expr, IntoIden, SeaRc, SelectExpr}; | ||
|
||
impl<E> Select<E> | ||
where | ||
|
@@ -79,21 +79,28 @@ where | |
|
||
slf.query().join_as( | ||
JoinType::LeftJoin, | ||
unpack_table_ref(&rel.to_tbl), | ||
rel.to_tbl, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Same here |
||
SeaRc::clone(&to_tbl), | ||
join_tbl_on_condition(from_tbl, to_tbl, rel.from_col, rel.to_col), | ||
); | ||
} | ||
slf = slf.apply_alias(SelectA.as_str()); | ||
let text_type = SeaRc::new(Alias::new("text")) as DynIden; | ||
let mut select_two = SelectTwo::new_without_prepare(slf.query); | ||
for col in <T::Column as Iterable>::iter() { | ||
let col_def = col.def(); | ||
let col_type = col_def.get_column_type(); | ||
let alias = format!("{}{}", SelectB.as_str(), col.as_str()); | ||
let expr = Expr::tbl( | ||
Alias::new(&format!("r{}", l.link().len() - 1)).into_iden(), | ||
col.into_iden(), | ||
); | ||
let expr = match col_type.get_enum_name() { | ||
Some(_) => expr.as_enum(text_type.clone()), | ||
None => expr.into(), | ||
}; | ||
select_two.query().expr(SelectExpr { | ||
expr: Expr::tbl( | ||
Alias::new(&format!("r{}", l.link().len() - 1)).into_iden(), | ||
col.into_iden(), | ||
) | ||
.into(), | ||
expr, | ||
alias: Some(SeaRc::new(Alias::new(&alias))), | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I caught some other bug as well. This should pass the
TableRef
in directly, so that schema name will also be included if needed.