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

Fix related & linked with enum columns #376

Merged
merged 2 commits into from
Dec 10, 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
2 changes: 1 addition & 1 deletion src/entity/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait Linked {

select.query().join_as(
JoinType::InnerJoin,
unpack_table_ref(&rel.from_tbl),
rel.from_tbl,
Copy link
Member Author

@billy1624 billy1624 Dec 10, 2021

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.

SeaRc::clone(&from_tbl),
join_tbl_on_condition(from_tbl, to_tbl, rel.from_col, rel.to_col),
);
Expand Down
30 changes: 24 additions & 6 deletions src/query/combine.rs
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 ) => {
Expand Down Expand Up @@ -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"),
Copy link
Member Author

Choose a reason for hiding this comment

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

It panic at this line. It didn't catch ColumnRef::AsEnum and apply alias for it.

Copy link
Member Author

Choose a reason for hiding this comment

The 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)));
Expand Down Expand Up @@ -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))),
});
}
Expand Down
25 changes: 16 additions & 9 deletions src/query/join.rs
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
Expand Down Expand Up @@ -79,21 +79,28 @@ where

slf.query().join_as(
JoinType::LeftJoin,
unpack_table_ref(&rel.to_tbl),
rel.to_tbl,
Copy link
Member Author

@billy1624 billy1624 Dec 10, 2021

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.

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))),
});
}
Expand Down
Loading