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

Use same table join code for both read and list functions #3663

Merged
merged 32 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3da5f83
Try stuff
dullbananas Jul 17, 2023
178bd43
Revert "Try stuff"
dullbananas Jul 17, 2023
b9f9a23
Revert "Revert "Try stuff""
dullbananas Jul 17, 2023
ccd498d
Revert "Revert "Revert "Try stuff"""
dullbananas Jul 17, 2023
a1fa3ee
Revert "Revert "Revert "Revert "Try stuff""""
dullbananas Jul 18, 2023
47000ff
Try more stuff
dullbananas Jul 19, 2023
2aec4d6
Add queries function
dullbananas Jul 19, 2023
d3d309c
Simplify queries function
dullbananas Jul 19, 2023
69afed0
Move aliases to db_schema
dullbananas Jul 19, 2023
b43e830
Revert "Move aliases to db_schema"
dullbananas Jul 19, 2023
60dddd7
Add ReadFuture and ListFuture
dullbananas Jul 19, 2023
078385d
Refactor queries function and add Queries struct
dullbananas Jul 19, 2023
4e00652
Box futures in Queries::new
dullbananas Jul 19, 2023
d472aee
Use from_tuple
dullbananas Jul 19, 2023
d2d08dd
Add comment_view::queries and improve comment_report_view::queries
dullbananas Jul 20, 2023
5874aec
Add local_user_view::queries
dullbananas Jul 20, 2023
56fd2d7
Add post_report_view::queries
dullbananas Jul 20, 2023
80bbd11
Ad post_view::queries
dullbananas Jul 20, 2023
e6dca52
Add private_message_report_view::queries
dullbananas Jul 20, 2023
02ca335
private_message_view, registration_application_view
dullbananas Jul 20, 2023
f6f392c
Use 'a in BoxedQuery
dullbananas Jul 20, 2023
c1fec40
comment_reply_view, community_view
dullbananas Jul 21, 2023
460e532
Change aliases to inline module
dullbananas Jul 21, 2023
4cd95af
person_mention_view
dullbananas Jul 21, 2023
d98f541
person_view
dullbananas Jul 21, 2023
bcadb9c
Use separate community_person_ban joins instead of including boolean …
dullbananas Jul 22, 2023
44ffd91
Merge remote-tracking branch 'upstream/main' into simp
dullbananas Jul 22, 2023
b3ced19
Merge branch 'main' into simp
dullbananas Jul 25, 2023
cc29a42
Fix comment_view
dullbananas Jul 26, 2023
c2706e2
Merge remote-tracking branch 'upstream/main' into simp
dullbananas Jul 26, 2023
dc49a67
rerun ci
dullbananas Jul 27, 2023
ce13c9c
Merge branch 'main' into simp
dullbananas Jul 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/db_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub mod newtypes;
#[rustfmt::skip]
#[allow(clippy::wildcard_imports)]
pub mod schema;
#[cfg(feature = "full")]
pub mod aliases {
use crate::schema::person;
diesel::alias!(person as person1: Person1, person as person2: Person2);
}
pub mod source;
#[cfg(feature = "full")]
pub mod traits;
Expand Down
91 changes: 90 additions & 1 deletion crates/db_schema/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
diesel::Connection,
diesel_migrations::MigrationHarness,
newtypes::DbUrl,
traits::JoinView,
CommentSortType,
PersonSortType,
SortType,
Expand All @@ -26,7 +27,7 @@ use diesel_async::{
},
};
use diesel_migrations::EmbeddedMigrations;
use futures_util::{future::BoxFuture, FutureExt};
use futures_util::{future::BoxFuture, Future, FutureExt};
use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
settings::structs::Settings,
Expand Down Expand Up @@ -420,6 +421,94 @@ where
}
}

pub type ResultFuture<'a, T> = BoxFuture<'a, Result<T, DieselError>>;

pub trait ReadFn<'a, T: JoinView, Args>:
Fn(DbConn<'a>, Args) -> ResultFuture<'a, <T as JoinView>::JoinTuple>
{
}

impl<
'a,
T: JoinView,
Args,
F: Fn(DbConn<'a>, Args) -> ResultFuture<'a, <T as JoinView>::JoinTuple>,
> ReadFn<'a, T, Args> for F
{
}

pub trait ListFn<'a, T: JoinView, Args>:
Fn(DbConn<'a>, Args) -> ResultFuture<'a, Vec<<T as JoinView>::JoinTuple>>
{
}

impl<
'a,
T: JoinView,
Args,
F: Fn(DbConn<'a>, Args) -> ResultFuture<'a, Vec<<T as JoinView>::JoinTuple>>,
> ListFn<'a, T, Args> for F
{
}

/// Allows read and list functions to capture a shared closure that has an inferred return type, which is useful for join logic
pub struct Queries<RF, LF> {
pub read_fn: RF,
pub list_fn: LF,
}

// `()` is used to prevent type inference error
impl Queries<(), ()> {
Copy link
Member

Choose a reason for hiding this comment

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

I really do wish there was a better way to handle this, but oh well. Seems like such a common case, but because our type is so complicated, its near impossible to reverse.

We'll still merge, but if you get some time, ask @weiznich on matrix or here if there really is no better alternative for what we need.

pub fn new<'a, RFut, LFut, RT, LT, RA, LA, RF2, LF2>(
read_fn: RF2,
list_fn: LF2,
) -> Queries<impl ReadFn<'a, RT, RA>, impl ListFn<'a, LT, LA>>
where
RFut: Future<Output = Result<<RT as JoinView>::JoinTuple, DieselError>> + Sized + Send + 'a,
LFut:
Future<Output = Result<Vec<<LT as JoinView>::JoinTuple>, DieselError>> + Sized + Send + 'a,
RT: JoinView,
LT: JoinView,
RF2: Fn(DbConn<'a>, RA) -> RFut,
LF2: Fn(DbConn<'a>, LA) -> LFut,
{
Queries {
read_fn: move |conn, args| read_fn(conn, args).boxed(),
list_fn: move |conn, args| list_fn(conn, args).boxed(),
}
}
}

impl<RF, LF> Queries<RF, LF> {
pub async fn read<'a, T, Args>(
self,
pool: &'a mut DbPool<'_>,
args: Args,
) -> Result<T, DieselError>
where
T: JoinView,
RF: ReadFn<'a, T, Args>,
{
let conn = get_conn(pool).await?;
let res = (self.read_fn)(conn, args).await?;
Ok(T::from_tuple(res))
}

pub async fn list<'a, T, Args>(
self,
pool: &'a mut DbPool<'_>,
args: Args,
) -> Result<Vec<T>, DieselError>
where
T: JoinView,
LF: ListFn<'a, T, Args>,
{
let conn = get_conn(pool).await?;
let res = (self.list_fn)(conn, args).await?;
Ok(res.into_iter().map(T::from_tuple).collect())
}
}

#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
Expand Down