Last active
April 16, 2024 04:48
-
-
Save jinohkang-theori/4bc96527eaf1c8e22ee7d7252338a591 to your computer and use it in GitHub Desktop.
Workaround for https://github.com/SeaQL/sea-orm/issues/1950
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// © 2024 Theori | |
// Portions © 2023 Seafire Software Limited | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the “Software”), to | |
// deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
// sell copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
// IN THE SOFTWARE. | |
use sea_orm::prelude::ColumnTrait; | |
use sea_orm::sea_query::{Alias, SeaRc, SelectExpr}; | |
use sea_orm::{ | |
EntityTrait, Iden, PartialModelTrait, QuerySelect, SelectA, SelectB, SelectColumns, SelectTwo, | |
SelectTwoModel, Selector, | |
}; | |
struct PrependAlias<T, P> { | |
inner: T, | |
prefix: P, | |
} | |
impl<T, P> PrependAlias<T, P> { | |
fn new(inner: T, prefix: P) -> Self { | |
PrependAlias { inner, prefix } | |
} | |
fn into_inner(self) -> T { | |
self.inner | |
} | |
} | |
impl<T, P> SelectColumns for PrependAlias<T, P> | |
where | |
T: QuerySelect, | |
P: Iden, | |
{ | |
/// Partially copied from [`QuerySelect::column`]. | |
fn select_column<C: ColumnTrait>(mut self, col: C) -> Self { | |
let alias = self.prefix.to_string() + col.as_str(); | |
self.inner.query().expr(SelectExpr { | |
expr: col.select_as(col.into_expr()), | |
alias: Some(SeaRc::new(Alias::new(alias))), | |
window: None, | |
}); | |
self | |
} | |
fn select_column_as<C, I>(mut self, col: C, alias: I) -> Self | |
where | |
C: sea_orm::IntoSimpleExpr, | |
I: sea_orm::IntoIdentity, | |
{ | |
let alias = self.prefix.to_string() + alias.into_identity().to_string().as_str(); | |
self.inner = self.inner.select_column_as(col, alias); | |
self | |
} | |
} | |
pub trait IntoPartialModelIssue1950Workaround { | |
/// Perform a conversion into a [SelectTwoModel] with [PartialModel](PartialModelTrait) | |
fn into_partial_model_issue1950_workaround<M, N>(self) -> Selector<SelectTwoModel<M, N>> | |
where | |
M: PartialModelTrait, | |
N: PartialModelTrait; | |
} | |
impl<E, F> IntoPartialModelIssue1950Workaround for SelectTwo<E, F> | |
where | |
E: EntityTrait, | |
F: EntityTrait, | |
{ | |
/// Partially copied from [`SelectTwo::into_partial_model`]. | |
/// | |
/// See <https://github.com/SeaQL/sea-orm/issues/1950>. | |
fn into_partial_model_issue1950_workaround<M, N>(self) -> Selector<SelectTwoModel<M, N>> | |
where | |
M: PartialModelTrait, | |
N: PartialModelTrait, | |
{ | |
let select = QuerySelect::select_only(self); | |
let select = M::select_cols(PrependAlias::new(select, SelectA)).into_inner(); | |
let select = N::select_cols(PrependAlias::new(select, SelectB)).into_inner(); | |
select.into_model::<M, N>() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment