1use crate::{
2 BelongsTo, Database, DatabaseQuery, Expr, ForeignKeyValue, HasMany, Loaded, LoadedMany,
3 LoadedOne, Model, MultiPrefetchRelatedQuery, MultiSelectRelatedQuery, NestedPrefetchQuery,
4 OrderBy, OrmError, PrefetchRelatedQuery, PrefetchRelation, PrimaryKey, SelectQuery,
5 SelectRelatedQuery, WithOne, WithOptionalOne,
6};
7
8pub struct BoundQuery<'db, Q> {
14 database: &'db Database,
15 query: Q,
16}
17
18impl<'db, Q> BoundQuery<'db, Q> {
19 pub(crate) fn new(database: &'db Database, query: Q) -> Self {
20 Self { database, query }
21 }
22}
23
24macro_rules! filter_order {
25 () => {
26 pub fn filter(self, expr: Expr) -> Self {
27 Self::new(self.database, self.query.filter(expr))
28 }
29 pub fn order_by(self, order: OrderBy) -> Self {
30 Self::new(self.database, self.query.order_by(order))
31 }
32 };
33}
34
35macro_rules! pagination {
36 () => {
37 pub fn limit(self, limit: u64) -> Self {
38 Self::new(self.database, self.query.limit(limit))
39 }
40 pub fn offset(self, offset: u64) -> Self {
41 Self::new(self.database, self.query.offset(offset))
42 }
43 };
44}
45
46impl<'db, M: Model> BoundQuery<'db, DatabaseQuery<M>> {
47 filter_order!();
48 pagination!();
49
50 pub fn select_related<R, Relation, Key>(
51 self,
52 relation: BelongsTo<M, R, Relation, Key>,
53 ) -> BoundQuery<'db, SelectRelatedQuery<M, R, Relation, Key>> {
54 BoundQuery::new(self.database, self.query.select_related(relation))
55 }
56
57 pub fn prefetch_related<P: PrefetchRelation<M>>(self, plan: P) -> BoundQuery<'db, P::Query> {
58 BoundQuery::new(self.database, self.query.prefetch_related(plan))
59 }
60
61 pub async fn all(self) -> Result<Vec<M>, OrmError>
62 where
63 M: Send + 'static,
64 {
65 self.query.all(self.database).await
66 }
67
68 pub async fn first(self) -> Result<Option<M>, OrmError>
69 where
70 M: Send + 'static,
71 {
72 self.query.first(self.database).await
73 }
74
75 pub async fn count(self) -> Result<usize, OrmError>
76 where
77 M: Send + 'static,
78 {
79 self.query.count(self.database).await
80 }
81
82 pub fn into_select_query(self) -> SelectQuery<M> {
83 self.query.into_select_query()
84 }
85}
86
87impl<M: Model, R, Relation, Key> BoundQuery<'_, SelectRelatedQuery<M, R, Relation, Key>> {
88 filter_order!();
89 pagination!();
90
91 pub async fn count(self) -> Result<usize, OrmError>
92 where
93 M: Send + 'static,
94 R: Model,
95 Key: ForeignKeyValue<R::PrimaryKey>,
96 {
97 self.query.count(self.database).await
98 }
99}
100
101impl<'db, M, R, Relation, Key: PrimaryKey> BoundQuery<'db, SelectRelatedQuery<M, R, Relation, Key>>
102where
103 M: Model + Send + 'static,
104 R: Model + Send + 'static,
105{
106 pub fn select_related<R2: Model, Relation2>(
107 self,
108 relation: BelongsTo<M, R2, Relation2, R2::PrimaryKey>,
109 ) -> BoundQuery<'db, MultiSelectRelatedQuery<M, R, Relation, R2, Relation2, Key>> {
110 BoundQuery::new(self.database, self.query.select_related(relation))
111 }
112
113 pub async fn all(self) -> Result<Vec<WithOne<M, R, Relation, Key>>, OrmError> {
114 self.query.all(self.database).await
115 }
116}
117
118impl<M, R, Relation, Key: PrimaryKey>
119 BoundQuery<'_, SelectRelatedQuery<M, R, Relation, Option<Key>>>
120where
121 M: Model + Send + 'static,
122 R: Model + Send + 'static,
123{
124 pub async fn all(self) -> Result<Vec<WithOptionalOne<M, R, Relation>>, OrmError> {
125 self.query.all(self.database).await
126 }
127}
128
129impl<M, R1, Relation1, R2, Relation2, Key1>
130 BoundQuery<'_, MultiSelectRelatedQuery<M, R1, Relation1, R2, Relation2, Key1>>
131where
132 M: Model + Send + 'static,
133 R1: Model + Send + 'static,
134 R2: Model + Send + 'static,
135{
136 filter_order!();
137
138 pub async fn all(
139 self,
140 ) -> Result<Vec<Loaded<M, (LoadedOne<R1, Relation1>, LoadedOne<R2, Relation2>)>>, OrmError>
141 {
142 self.query.all(self.database).await
143 }
144}
145
146impl<'db, M, R, Relation, Key> BoundQuery<'db, PrefetchRelatedQuery<M, R, Relation, Key>>
147where
148 M: Model + Send + 'static,
149 R: Model + Send + 'static,
150 Key: ForeignKeyValue<M::PrimaryKey>,
151{
152 filter_order!();
153 pagination!();
154
155 pub fn prefetch_related<R2, Relation2, Key2: ForeignKeyValue<M::PrimaryKey>>(
156 self,
157 relation: HasMany<M, R2, Relation2, Key2>,
158 ) -> BoundQuery<'db, MultiPrefetchRelatedQuery<M, R, Relation, R2, Relation2, Key, Key2>> {
159 BoundQuery::new(self.database, self.query.prefetch_related(relation))
160 }
161
162 pub async fn count(self) -> Result<usize, OrmError> {
163 self.query.count(self.database).await
164 }
165
166 pub async fn all(self) -> Result<Vec<Loaded<M, (LoadedMany<R, Relation>,)>>, OrmError>
167 where
168 Relation: Send + 'static,
169 {
170 self.query.all(self.database).await
171 }
172}
173
174impl<M, R1, Relation1, R2, Relation2, Key1, Key2>
175 BoundQuery<'_, MultiPrefetchRelatedQuery<M, R1, Relation1, R2, Relation2, Key1, Key2>>
176where
177 M: Model + Send + 'static,
178 R1: Model + Send + 'static,
179 R2: Model + Send + 'static,
180 Relation1: Send + 'static,
181 Relation2: Send + 'static,
182 Key1: ForeignKeyValue<M::PrimaryKey>,
183 Key2: ForeignKeyValue<M::PrimaryKey>,
184{
185 pub async fn all(
186 self,
187 ) -> Result<Vec<Loaded<M, (LoadedMany<R1, Relation1>, LoadedMany<R2, Relation2>)>>, OrmError>
188 {
189 self.query.all(self.database).await
190 }
191}
192
193impl<M, R, Relation, C, ChildRelation, OuterKey, InnerKey>
194 BoundQuery<'_, NestedPrefetchQuery<M, R, Relation, C, ChildRelation, OuterKey, InnerKey>>
195where
196 M: Model + Send + 'static,
197 R: Model + Send + 'static,
198 C: Model + Send + 'static,
199 Relation: Send + 'static,
200 ChildRelation: Send + 'static,
201 OuterKey: ForeignKeyValue<M::PrimaryKey>,
202 InnerKey: ForeignKeyValue<R::PrimaryKey>,
203{
204 pub async fn all(
205 self,
206 ) -> Result<
207 Vec<Loaded<M, (LoadedMany<Loaded<R, (LoadedMany<C, ChildRelation>,)>, Relation>,)>>,
208 OrmError,
209 > {
210 self.query.all(self.database).await
211 }
212}