Skip to content

Commit 2cc693a

Browse files
committed
add get_rows, which will return all rows of a given table
1 parent 210343c commit 2cc693a

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

backend/db/src/calls/mod.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ use diesel::query_builder::UpdateStatement;
5353
use diesel::helper_types::Update;
5454
use crate::calls::article::ArticleChangeset;
5555
use crate::Article;
56+
use diesel::select;
57+
use diesel::expression::exists::exists;
58+
use diesel::query_builder::SelectQuery;
59+
use diesel::expression::subselect::ValidSubselect;
60+
use diesel::query_builder::SelectStatement;
61+
use diesel::expression::exists::Exists;
62+
use diesel::helper_types::Select;
63+
use diesel::query_dsl::select_dsl::SelectDsl;
5664

5765

5866
/// Generic function for getting a whole row from a given table.
@@ -71,6 +79,15 @@ fn get_user(uuid: UserUuid,conn: &PgConnection) -> Result<User, Error> {
7179
get_row::<User,_>(schema::users::table, uuid.0, conn)
7280
}
7381

82+
#[inline(always)]
83+
pub fn get_rows<'a, Model, Table>(table: Table, conn: &PgConnection) -> Result<Vec<Model>, Error>
84+
where
85+
Table: RunQueryDsl<Model> + Query + LoadQuery<PgConnection, Model>,
86+
{
87+
table.load::<Model>(conn)
88+
}
89+
90+
7491

7592
/// Generic function for deleting a row from a given table.
7693
#[inline(always)]
@@ -131,4 +148,26 @@ where
131148

132149
fn create_user(new_user: NewUser, conn: &PgConnection) -> Result<User, Error> {
133150
create_row::<User, NewUser,_>(schema::users::table, new_user, conn)
134-
}
151+
}
152+
153+
//fn row_exists<'a, Model, Tab>(table: Tab, uuid: Uuid, conn: &PgConnection) -> Result<bool, Error>
154+
//where
155+
// Tab: FindDsl<Uuid> + Table,
156+
// <Tab as FindDsl<uuid::Uuid>>::Output: SelectQuery,
157+
// <Tab as FindDsl<uuid::Uuid>>::Output: QueryId,
158+
// <Tab as FindDsl<uuid::Uuid>>::Output: ValidSubselect<()>,
159+
// <Tab as FindDsl<Uuid>>::Output : SelectDsl<Exists<<Tab as FindDsl<Uuid>>::Output>>,
160+
// Find<Tab, Uuid>: LoadQuery<PgConnection, Model>,
161+
//// SelectStatement<(), Select=Exists<<Table as FindDsl<Uuid>>::Output>>: QueryFragment<Pg>//, SelectClause<Exists<<Table as FindDsl<uuid::Uuid>>::Output>>>: QueryFragment<Pg>
162+
//{
163+
//// table.find(uuid)
164+
//// .select(exists)
165+
//
166+
//// select(exists(table.filter(table.primary_key().eq(uuid))))
167+
// table
168+
//// .find(uuid)
169+
// .count()
170+
// .get_result::<i32>(conn)
171+
//}
172+
//
173+
//use diesel::QueryDsl;

0 commit comments

Comments
 (0)