1use crate::{
2 BelongsTo, CompiledQuery, CurrentDialect, DatabaseValue, Expr, HasMany, MigrationGraph, Model,
3 ModelField, QueryAst, QueryBuildError, QueryValue, Row, SelectQuery, SqlCompiler,
4 ValidatedWrite, backend,
5};
6use std::{
7 collections::{HashMap, HashSet},
8 future::Future,
9 marker::PhantomData,
10 pin::Pin,
11};
12
13#[derive(Debug, thiserror::Error)]
14pub enum OrmError {
15 #[error("database configuration error: {0}")]
16 Configuration(String),
17 #[error("database pool error: {0}")]
18 Pool(String),
19 #[error("database interaction error: {0}")]
20 Interaction(String),
21 #[cfg(feature = "sqlite")]
22 #[error("sqlite error: {0}")]
23 Sqlite(#[from] rusqlite::Error),
24 #[cfg(feature = "postgres")]
25 #[error("postgres error: {0}")]
26 Postgres(#[from] tokio_postgres::Error),
27 #[error("row decoding error: {0}")]
28 Decode(#[from] crate::DecodeError),
29 #[error("query build error: {0:?}")]
30 QueryBuild(QueryBuildError),
31 #[error("migration error: {0}")]
32 Migration(String),
33}
34
35impl From<QueryBuildError> for OrmError {
36 fn from(value: QueryBuildError) -> Self {
37 Self::QueryBuild(value)
38 }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum ConstraintKind {
43 Unique,
44 ForeignKey,
45 NotNull,
46 Check,
47}
48
49impl OrmError {
50 pub fn constraint_kind(&self) -> Option<ConstraintKind> {
51 #[cfg(feature = "postgres")]
52 if let Some(code) = self.sql_state() {
53 return match code {
54 "23505" => Some(ConstraintKind::Unique),
55 "23503" => Some(ConstraintKind::ForeignKey),
56 "23502" => Some(ConstraintKind::NotNull),
57 "23514" => Some(ConstraintKind::Check),
58 _ => None,
59 };
60 }
61 #[cfg(feature = "sqlite")]
62 if let Self::Sqlite(rusqlite::Error::SqliteFailure(error, _)) = self {
63 return match error.extended_code {
64 rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE
65 | rusqlite::ffi::SQLITE_CONSTRAINT_PRIMARYKEY => Some(ConstraintKind::Unique),
66 rusqlite::ffi::SQLITE_CONSTRAINT_FOREIGNKEY => Some(ConstraintKind::ForeignKey),
67 rusqlite::ffi::SQLITE_CONSTRAINT_NOTNULL => Some(ConstraintKind::NotNull),
68 rusqlite::ffi::SQLITE_CONSTRAINT_CHECK => Some(ConstraintKind::Check),
69 _ => None,
70 };
71 }
72 None
73 }
74 pub fn sql_state(&self) -> Option<&str> {
76 #[cfg(feature = "postgres")]
77 if let Self::Postgres(error) = self {
78 return error.code().map(|code| code.code());
79 }
80 None
81 }
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct ExecuteResult {
86 pub rows_affected: usize,
87 pub last_insert_rowid: Option<i64>,
89}
90
91#[derive(Clone)]
93pub struct Database {
94 pool: backend::Pool,
95}
96
97pub type TransactionFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, OrmError>> + Send + 'a>>;
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum IsolationLevel {
101 ReadCommitted,
102 RepeatableRead,
103 Serializable,
104}
105
106impl Database {
107 pub async fn connect(url: impl Into<String>) -> Result<Self, OrmError> {
108 let url = url.into();
109 let size = if url == ":memory:" || url == "sqlite://:memory:" {
110 1
111 } else {
112 4
113 };
114 Self::connect_with_pool_size(url, size).await
115 }
116 pub async fn connect_with_pool_size(
117 url: impl Into<String>,
118 size: usize,
119 ) -> Result<Self, OrmError> {
120 Ok(Self {
121 pool: backend::connect(url.into(), size).await?,
122 })
123 }
124 #[cfg(feature = "sqlite")]
125 pub async fn connect_in_memory() -> Result<Self, OrmError> {
126 Self::connect(":memory:").await
127 }
128
129 pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError> {
130 ast.validate()?;
131 backend::Session::acquire(&self.pool)
132 .await?
133 .execute(SqlCompiler::<CurrentDialect>::compile(&ast))
134 .await
135 }
136 pub async fn execute_sql(
138 &self,
139 sql: impl Into<String>,
140 params: Vec<DatabaseValue>,
141 ) -> Result<ExecuteResult, OrmError> {
142 backend::Session::acquire(&self.pool)
143 .await?
144 .execute(CompiledQuery {
145 sql: sql.into(),
146 params,
147 })
148 .await
149 }
150 pub async fn query_sql(
151 &self,
152 sql: impl Into<String>,
153 params: Vec<DatabaseValue>,
154 ) -> Result<Vec<Row>, OrmError> {
155 backend::Session::acquire(&self.pool)
156 .await?
157 .query(CompiledQuery {
158 sql: sql.into(),
159 params,
160 })
161 .await
162 }
163 pub async fn create_table<M: Model>(&self) -> Result<ExecuteResult, OrmError> {
164 M::schema().validate().map_err(|error| {
165 OrmError::QueryBuild(QueryBuildError::InvalidSchema(format!("{error:?}")))
166 })?;
167 let schema = SqlCompiler::<CurrentDialect>::compile_schema(&M::schema());
168 self.transaction(move |tx| {
169 Box::pin(async move {
170 tx.session.batch(&schema.table).await?;
171 for sql in schema.indexes {
172 tx.session.batch(&sql).await?;
173 }
174 Ok(ExecuteResult {
175 rows_affected: 0,
176 last_insert_rowid: None,
177 })
178 })
179 })
180 .await
181 }
182 pub async fn insert<M: Model>(&self, model: &M) -> Result<ExecuteResult, OrmError> {
184 self.execute(insert_ast(model)).await
185 }
186 pub async fn fetch_all<M: Model + Send + 'static>(
187 &self,
188 query: SelectQuery<M>,
189 ) -> Result<Vec<M>, OrmError> {
190 decode_models(self.rows(query.into_ast()?).await?)
191 }
192 pub async fn fetch_one<M: Model + Send + 'static>(
193 &self,
194 query: SelectQuery<M>,
195 ) -> Result<Option<M>, OrmError> {
196 Ok(self.fetch_all(query.limit(1)).await?.pop())
197 }
198 pub async fn get<M: Model + Send + 'static>(
199 &self,
200 id: M::PrimaryKey,
201 ) -> Result<Option<M>, OrmError> {
202 self.fetch_one(M::query().filter(M::primary_key().eq(id)))
203 .await
204 }
205 pub async fn all<M: Model + Send + 'static>(&self) -> Result<Vec<M>, OrmError> {
206 self.fetch_all(M::query()).await
207 }
208 pub async fn count<M: Model + Send + 'static>(&self) -> Result<usize, OrmError> {
209 self.count_query(M::query()).await
210 }
211 pub async fn count_query<M: Model + Send + 'static>(
212 &self,
213 query: SelectQuery<M>,
214 ) -> Result<usize, OrmError> {
215 let query = count_query(query)?;
216 count_rows(
217 backend::Session::acquire(&self.pool)
218 .await?
219 .query(query)
220 .await?,
221 )
222 }
223 pub fn create<M: Model>(&self) -> CreateBuilder<'_, M> {
224 CreateBuilder {
225 database: self,
226 query: M::insert(),
227 }
228 }
229 pub fn update<M: Model>(&self, id: M::PrimaryKey) -> UpdateBuilder<'_, M> {
230 UpdateBuilder {
231 database: self,
232 query: M::update().filter(M::primary_key().eq(id)),
233 }
234 }
235 pub async fn delete<M: Model + Send + 'static>(
236 &self,
237 id: M::PrimaryKey,
238 ) -> Result<bool, OrmError> {
239 Ok(self
240 .execute(M::delete().filter(M::primary_key().eq(id)).into_ast()?)
241 .await?
242 .rows_affected
243 == 1)
244 }
245 pub fn query<M: Model>(&self) -> DatabaseQuery<M> {
246 DatabaseQuery::new(M::query())
247 }
248 pub async fn fetch_by<M, T, V>(
249 &self,
250 field: ModelField<M, T>,
251 value: V,
252 ) -> Result<Vec<M>, OrmError>
253 where
254 M: Model + Send + 'static,
255 V: QueryValue<T>,
256 {
257 self.fetch_all(M::query().filter(field.eq(value))).await
258 }
259 pub async fn fetch_by_many<M, T, I, V>(
260 &self,
261 field: ModelField<M, T>,
262 values: I,
263 ) -> Result<Vec<M>, OrmError>
264 where
265 M: Model + Send + 'static,
266 I: IntoIterator<Item = V>,
267 V: QueryValue<T>,
268 {
269 let values = values
270 .into_iter()
271 .map(QueryValue::<T>::into_query_value)
272 .collect::<Vec<_>>();
273 let mut result = Vec::new();
274 for chunk in values.chunks(backend::MAX_PARAMETERS) {
275 result.extend(
276 self.fetch_all(M::query().filter(Expr::In {
277 left: Box::new(Expr::Column(field.column())),
278 values: chunk.to_vec(),
279 }))
280 .await?,
281 );
282 }
283 let mut seen = HashSet::new();
284 result.retain(|model| seen.insert(model.primary_key_value()));
285 Ok(result)
286 }
287
288 pub async fn transaction<T, F>(&self, action: F) -> Result<T, OrmError>
292 where
293 T: Send + 'static,
294 F: for<'a> FnOnce(&'a Transaction) -> TransactionFuture<'a, T> + Send,
295 {
296 self.transaction_with_isolation(IsolationLevel::Serializable, action)
297 .await
298 }
299
300 pub async fn transaction_with_isolation<T, F>(
302 &self,
303 isolation: IsolationLevel,
304 action: F,
305 ) -> Result<T, OrmError>
306 where
307 T: Send + 'static,
308 F: for<'a> FnOnce(&'a Transaction) -> TransactionFuture<'a, T> + Send,
309 {
310 let session = backend::Session::acquire(&self.pool).await?;
311 let mut guard = TransactionGuard {
312 session: session.clone(),
313 active: true,
314 };
315 #[cfg(feature = "sqlite")]
316 let begin = {
317 let _ = isolation;
318 "BEGIN"
319 };
320 #[cfg(all(feature = "postgres", not(feature = "sqlite")))]
321 let begin = match isolation {
322 IsolationLevel::ReadCommitted => "BEGIN ISOLATION LEVEL READ COMMITTED",
323 IsolationLevel::RepeatableRead => "BEGIN ISOLATION LEVEL REPEATABLE READ",
324 IsolationLevel::Serializable => "BEGIN ISOLATION LEVEL SERIALIZABLE",
325 };
326 session.batch(begin).await?;
327 let transaction = Transaction { session };
328 match action(&transaction).await {
329 Ok(value) => {
330 transaction.session.commit().await?;
331 guard.active = false;
332 Ok(value)
333 }
334 Err(error) => {
335 transaction.session.batch("ROLLBACK").await?;
336 guard.active = false;
337 Err(error)
338 }
339 }
340 }
341 #[deprecated(since = "0.3.0", note = "use Database::transaction instead")]
343 pub async fn typed_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
344 where
345 T: Send + 'static,
346 F: for<'a> FnOnce(&'a Transaction) -> TransactionFuture<'a, T> + Send,
347 {
348 self.transaction(action).await
349 }
350
351 #[cfg(feature = "sqlite")]
353 pub async fn raw_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
354 where
355 T: Send + 'static,
356 F: FnOnce(&rusqlite::Connection) -> rusqlite::Result<T> + Send + 'static,
357 {
358 self.transaction(move |tx| {
359 Box::pin(async move {
360 tx.session
361 .interact(move |connection| action(connection).map_err(Into::into))
362 .await
363 })
364 })
365 .await
366 }
367 pub async fn migrate(&self, graph: MigrationGraph) -> Result<usize, OrmError> {
368 self.migrate_to(graph, None).await
369 }
370 pub async fn migrate_to(
371 &self,
372 graph: MigrationGraph,
373 target: Option<crate::MigrationId>,
374 ) -> Result<usize, OrmError> {
375 backend::Session::acquire(&self.pool)
376 .await?
377 .migrate(graph, target)
378 .await
379 }
380 pub async fn migration_history(&self) -> Result<HashMap<(String, String), String>, OrmError> {
381 backend::Session::acquire(&self.pool).await?.history().await
382 }
383 async fn rows(&self, ast: QueryAst) -> Result<Vec<Row>, OrmError> {
384 ast.validate()?;
385 backend::Session::acquire(&self.pool)
386 .await?
387 .query(SqlCompiler::<CurrentDialect>::compile(&ast))
388 .await
389 }
390 async fn execute_returning<M: Model + Send + 'static>(
391 &self,
392 ast: QueryAst,
393 ) -> Result<Option<M>, OrmError> {
394 Ok(decode_models(self.rows(ast).await?)?.pop())
395 }
396}
397
398pub(crate) struct TransactionGuard {
399 pub(crate) session: backend::Session,
400 pub(crate) active: bool,
401}
402impl Drop for TransactionGuard {
403 fn drop(&mut self) {
404 if self.active {
405 let session = self.session.clone();
406 tokio::spawn(async move {
407 let _ = session.batch("ROLLBACK").await;
408 });
409 }
410 }
411}
412
413pub struct Transaction {
415 session: backend::Session,
416}
417impl Transaction {
418 pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError> {
419 ast.validate()?;
420 self.session
421 .execute(SqlCompiler::<CurrentDialect>::compile(&ast))
422 .await
423 }
424 pub async fn insert<M: Model>(&self, model: &M) -> Result<ExecuteResult, OrmError> {
425 self.execute(insert_ast(model)).await
426 }
427 pub async fn fetch_all<M: Model + Send + 'static>(
428 &self,
429 query: SelectQuery<M>,
430 ) -> Result<Vec<M>, OrmError> {
431 decode_models(self.rows(query.into_ast()?).await?)
432 }
433 pub async fn fetch_one<M: Model + Send + 'static>(
434 &self,
435 query: SelectQuery<M>,
436 ) -> Result<Option<M>, OrmError> {
437 Ok(self.fetch_all(query.limit(1)).await?.pop())
438 }
439 pub async fn get<M: Model + Send + 'static>(
440 &self,
441 id: M::PrimaryKey,
442 ) -> Result<Option<M>, OrmError> {
443 self.fetch_one(M::query().filter(M::primary_key().eq(id)))
444 .await
445 }
446 pub async fn all<M: Model + Send + 'static>(&self) -> Result<Vec<M>, OrmError> {
447 self.fetch_all(M::query()).await
448 }
449 pub async fn count<M: Model + Send + 'static>(&self) -> Result<usize, OrmError> {
450 count_rows(self.session.query(count_query(M::query())?).await?)
451 }
452 pub async fn delete<M: Model + Send + 'static>(
453 &self,
454 id: M::PrimaryKey,
455 ) -> Result<bool, OrmError> {
456 Ok(self
457 .execute(M::delete().filter(M::primary_key().eq(id)).into_ast()?)
458 .await?
459 .rows_affected
460 == 1)
461 }
462 pub fn create<M: Model>(&self) -> TransactionCreateBuilder<'_, M> {
463 TransactionCreateBuilder {
464 transaction: self,
465 query: M::insert(),
466 }
467 }
468 pub fn update<M: Model>(&self, id: M::PrimaryKey) -> TransactionUpdateBuilder<'_, M> {
469 TransactionUpdateBuilder {
470 transaction: self,
471 query: M::update().filter(M::primary_key().eq(id)),
472 }
473 }
474 pub fn query<M: Model>(&self) -> TransactionQuery<'_, M> {
475 TransactionQuery {
476 transaction: self,
477 query: M::query(),
478 }
479 }
480 pub async fn fetch_by_many<M, T, I, V>(
481 &self,
482 field: ModelField<M, T>,
483 values: I,
484 ) -> Result<Vec<M>, OrmError>
485 where
486 M: Model + Send + 'static,
487 I: IntoIterator<Item = V>,
488 V: QueryValue<T>,
489 {
490 let values = values
491 .into_iter()
492 .map(QueryValue::<T>::into_query_value)
493 .collect::<Vec<_>>();
494 let mut result = Vec::new();
495 for chunk in values.chunks(backend::MAX_PARAMETERS) {
496 result.extend(
497 self.fetch_all(M::query().filter(Expr::In {
498 left: Box::new(Expr::Column(field.column())),
499 values: chunk.to_vec(),
500 }))
501 .await?,
502 );
503 }
504 let mut seen = HashSet::new();
505 result.retain(|model| seen.insert(model.primary_key_value()));
506 Ok(result)
507 }
508 async fn rows(&self, ast: QueryAst) -> Result<Vec<Row>, OrmError> {
509 ast.validate()?;
510 self.session
511 .query(SqlCompiler::<CurrentDialect>::compile(&ast))
512 .await
513 }
514 async fn execute_returning<M: Model + Send + 'static>(
515 &self,
516 ast: QueryAst,
517 ) -> Result<Option<M>, OrmError> {
518 Ok(decode_models(self.rows(ast).await?)?.pop())
519 }
520}
521
522fn insert_ast<M: Model>(model: &M) -> QueryAst {
523 QueryAst::Insert(crate::InsertAst {
524 table: crate::TableRef::new(M::table_name()),
525 values: model.insert_values(),
526 returning: Vec::new(),
527 })
528}
529fn count_query<M: Model>(query: SelectQuery<M>) -> Result<CompiledQuery, OrmError> {
530 let ast = query.into_select_ast()?;
531 let compiled = SqlCompiler::<CurrentDialect>::compile(&QueryAst::Select(ast));
532 Ok(CompiledQuery {
533 sql: format!("SELECT COUNT(*) FROM ({}) AS che_count", compiled.sql),
534 params: compiled.params,
535 })
536}
537fn count_rows(rows: Vec<Row>) -> Result<usize, OrmError> {
538 let count: i64 = rows
539 .first()
540 .ok_or_else(|| OrmError::Interaction("count returned no row".into()))?
541 .get(0)?;
542 usize::try_from(count).map_err(|error| OrmError::Interaction(error.to_string()))
543}
544fn decode_models<M: Model>(rows: Vec<Row>) -> Result<Vec<M>, OrmError> {
545 rows.iter()
546 .map(|row| M::from_row(row).map_err(Into::into))
547 .collect()
548}
549
550pub struct TransactionCreateBuilder<'tx, M> {
551 transaction: &'tx Transaction,
552 query: crate::InsertQuery<M>,
553}
554impl<M: Model> TransactionCreateBuilder<'_, M> {
555 pub fn set<T, V: QueryValue<T>>(self, field: ModelField<M, T>, value: V) -> Self {
556 Self {
557 query: self.query.set(field, value),
558 ..self
559 }
560 }
561 pub async fn execute(self) -> Result<M, OrmError>
562 where
563 M: Send + 'static,
564 {
565 self.transaction
566 .execute_returning(self.query.returning_all().into_ast()?)
567 .await?
568 .ok_or_else(|| OrmError::Interaction("insert did not return a row".into()))
569 }
570}
571pub struct TransactionUpdateBuilder<'tx, M> {
572 transaction: &'tx Transaction,
573 query: crate::UpdateQuery<M>,
574}
575impl<M: Model> TransactionUpdateBuilder<'_, M> {
576 pub fn set<T, V: QueryValue<T>>(self, field: ModelField<M, T>, value: V) -> Self {
577 Self {
578 query: self.query.set(field, value),
579 ..self
580 }
581 }
582 pub async fn execute(self) -> Result<Option<M>, OrmError>
583 where
584 M: Send + 'static,
585 {
586 self.transaction
587 .execute_returning(self.query.returning_all().into_ast()?)
588 .await
589 }
590}
591pub struct TransactionQuery<'tx, M> {
592 transaction: &'tx Transaction,
593 query: SelectQuery<M>,
594}
595impl<M: Model> TransactionQuery<'_, M> {
596 pub fn filter(self, expr: Expr) -> Self {
597 Self {
598 query: self.query.filter(expr),
599 ..self
600 }
601 }
602 pub fn order_by(self, order: crate::OrderBy) -> Self {
603 Self {
604 query: self.query.order_by(order),
605 ..self
606 }
607 }
608 pub fn limit(self, limit: u64) -> Self {
609 Self {
610 query: self.query.limit(limit),
611 ..self
612 }
613 }
614 pub fn offset(self, offset: u64) -> Self {
615 Self {
616 query: self.query.offset(offset),
617 ..self
618 }
619 }
620 pub async fn all(self) -> Result<Vec<M>, OrmError>
621 where
622 M: Send + 'static,
623 {
624 self.transaction.fetch_all(self.query).await
625 }
626 pub async fn first(self) -> Result<Option<M>, OrmError>
627 where
628 M: Send + 'static,
629 {
630 self.transaction.fetch_one(self.query).await
631 }
632}
633impl<M: Model + Send + 'static> ValidatedWrite<M> {
634 pub async fn save(self, database: &Database) -> Result<Option<M>, OrmError> {
635 let ast = match self {
636 Self::Create(query) => query.returning_all().into_ast()?,
637 Self::Update(query) => query.returning_all().into_ast()?,
638 };
639 database.execute_returning(ast).await
640 }
641}
642pub struct CreateBuilder<'db, M> {
643 database: &'db Database,
644 query: crate::InsertQuery<M>,
645}
646impl<M: Model> CreateBuilder<'_, M> {
647 pub fn set<T, V: QueryValue<T>>(self, field: ModelField<M, T>, value: V) -> Self {
648 Self {
649 query: self.query.set(field, value),
650 ..self
651 }
652 }
653 pub async fn execute(self) -> Result<M, OrmError>
654 where
655 M: Send + 'static,
656 {
657 self.database
658 .execute_returning(self.query.returning_all().into_ast()?)
659 .await?
660 .ok_or_else(|| OrmError::Interaction("insert did not return a row".into()))
661 }
662}
663pub struct UpdateBuilder<'db, M> {
664 database: &'db Database,
665 query: crate::UpdateQuery<M>,
666}
667impl<M: Model> UpdateBuilder<'_, M> {
668 pub fn set<T, V: QueryValue<T>>(self, field: ModelField<M, T>, value: V) -> Self {
669 Self {
670 query: self.query.set(field, value),
671 ..self
672 }
673 }
674 pub async fn execute(self) -> Result<Option<M>, OrmError>
675 where
676 M: Send + 'static,
677 {
678 self.database
679 .execute_returning(self.query.returning_all().into_ast()?)
680 .await
681 }
682}
683pub struct DatabaseQuery<M> {
684 query: SelectQuery<M>,
685}
686impl<M: Model> DatabaseQuery<M> {
687 pub fn new(query: SelectQuery<M>) -> Self {
688 Self { query }
689 }
690 pub fn filter(self, expr: Expr) -> Self {
691 Self {
692 query: self.query.filter(expr),
693 }
694 }
695 pub fn order_by(self, order: crate::OrderBy) -> Self {
696 Self {
697 query: self.query.order_by(order),
698 }
699 }
700 pub fn limit(self, limit: u64) -> Self {
701 Self {
702 query: self.query.limit(limit),
703 }
704 }
705 pub fn offset(self, offset: u64) -> Self {
706 Self {
707 query: self.query.offset(offset),
708 }
709 }
710 pub fn select_related<R, Relation, Key>(
711 self,
712 relation: BelongsTo<M, R, Relation, Key>,
713 ) -> SelectRelatedQuery<M, R, Relation, Key> {
714 SelectRelatedQuery {
715 query: self.query,
716 relation,
717 }
718 }
719 pub fn prefetch_related<P: PrefetchRelation<M>>(self, plan: P) -> P::Query {
720 plan.into_query(self.query)
721 }
722 pub async fn all(self, db: &Database) -> Result<Vec<M>, OrmError>
723 where
724 M: Send + 'static,
725 {
726 db.fetch_all(self.query).await
727 }
728 pub async fn first(self, db: &Database) -> Result<Option<M>, OrmError>
729 where
730 M: Send + 'static,
731 {
732 db.fetch_one(self.query).await
733 }
734 pub async fn count(self, db: &Database) -> Result<usize, OrmError>
735 where
736 M: Send + 'static,
737 {
738 db.count_query(self.query).await
739 }
740 pub fn into_select_query(self) -> SelectQuery<M> {
741 self.query
742 }
743}
744pub trait PrefetchRelation<Owner: Model> {
745 type Query;
746 fn into_query(self, query: SelectQuery<Owner>) -> Self::Query;
747}
748
749#[derive(Debug)]
750pub struct WithOne<M, R, Relation = (), Key = i64> {
751 pub model: M,
752 pub related: R,
753 pub(crate) _relation: PhantomData<Relation>,
754 pub(crate) _key: PhantomData<Key>,
755}
756#[derive(Debug)]
757pub struct WithOptionalOne<M, R, Relation = ()> {
758 pub model: M,
759 pub related: Option<R>,
760 pub(crate) _relation: PhantomData<Relation>,
761}
762#[derive(Debug)]
763pub struct WithMany<M, R, Relation = (), Key = i64> {
764 pub model: M,
765 pub related: Vec<R>,
766 pub(crate) _relation: PhantomData<Relation>,
767 pub(crate) _key: PhantomData<Key>,
768}
769#[derive(Debug)]
770pub struct Loaded<M, Relations> {
771 pub model: M,
772 pub relations: Relations,
773}
774#[derive(Debug)]
775pub struct LoadedMany<R, Relation = ()> {
776 pub related: Vec<R>,
777 pub(crate) _relation: PhantomData<Relation>,
778}
779#[derive(Debug)]
780pub struct LoadedOne<R, Relation = ()> {
781 pub related: R,
782 pub(crate) _relation: PhantomData<Relation>,
783}
784
785pub struct SelectRelatedQuery<M, R, Relation = (), Key = i64> {
786 query: SelectQuery<M>,
787 relation: BelongsTo<M, R, Relation, Key>,
788}
789impl<M: Model, R, Relation, Key> SelectRelatedQuery<M, R, Relation, Key> {
790 pub fn filter(self, expr: Expr) -> Self {
791 Self {
792 query: self.query.filter(expr),
793 ..self
794 }
795 }
796 pub fn order_by(self, order: crate::OrderBy) -> Self {
797 Self {
798 query: self.query.order_by(order),
799 ..self
800 }
801 }
802 pub fn limit(self, limit: u64) -> Self {
803 Self {
804 query: self.query.limit(limit),
805 ..self
806 }
807 }
808 pub fn offset(self, offset: u64) -> Self {
809 Self {
810 query: self.query.offset(offset),
811 ..self
812 }
813 }
814 pub async fn count(self, db: &Database) -> Result<usize, OrmError>
815 where
816 M: Send + 'static,
817 R: Model,
818 Key: crate::ForeignKeyValue<R::PrimaryKey>,
819 {
820 let kind = if Key::OPTIONAL {
821 crate::JoinType::Left
822 } else {
823 crate::JoinType::Inner
824 };
825 let ast = self.query.into_joined_ast_with_kind(self.relation, kind)?;
826 let compiled = SqlCompiler::<CurrentDialect>::compile(&ast);
827 count_rows(
828 db.query_sql(
829 format!("SELECT COUNT(*) FROM ({}) AS che_count", compiled.sql),
830 compiled.params,
831 )
832 .await?,
833 )
834 }
835}
836impl<M, R, Relation, Key: crate::PrimaryKey> SelectRelatedQuery<M, R, Relation, Key>
837where
838 M: Model + Send + 'static,
839 R: Model + Send + 'static,
840{
841 pub fn select_related<R2: Model, Relation2>(
842 self,
843 relation: BelongsTo<M, R2, Relation2, R2::PrimaryKey>,
844 ) -> MultiSelectRelatedQuery<M, R, Relation, R2, Relation2, Key> {
845 MultiSelectRelatedQuery {
846 query: self.query,
847 first: self.relation,
848 second: relation,
849 }
850 }
851 pub async fn all(self, db: &Database) -> Result<Vec<WithOne<M, R, Relation, Key>>, OrmError> {
852 let offset = M::columns().len();
853 db.rows(self.query.into_joined_ast(self.relation)?)
854 .await?
855 .iter()
856 .map(|row| {
857 Ok(WithOne {
858 model: M::from_row_at(row, 0)?,
859 related: R::from_row_at(row, offset)?,
860 _relation: PhantomData,
861 _key: PhantomData,
862 })
863 })
864 .collect()
865 }
866}
867impl<M, R, Relation, Key: crate::PrimaryKey> SelectRelatedQuery<M, R, Relation, Option<Key>>
868where
869 M: Model + Send + 'static,
870 R: Model + Send + 'static,
871{
872 pub async fn all(
873 self,
874 db: &Database,
875 ) -> Result<Vec<WithOptionalOne<M, R, Relation>>, OrmError> {
876 let offset = M::columns().len();
877 let pk_offset = R::columns()
878 .iter()
879 .position(|column| *column == R::primary_key().column().name)
880 .ok_or_else(|| {
881 OrmError::Interaction("primary key missing from model columns".into())
882 })?;
883 db.rows(self.query.into_optional_joined_ast(self.relation)?)
884 .await?
885 .iter()
886 .map(|row| {
887 let id: Option<R::PrimaryKey> = row.get(offset + pk_offset)?;
888 Ok(WithOptionalOne {
889 model: M::from_row_at(row, 0)?,
890 related: id.map(|_| R::from_row_at(row, offset)).transpose()?,
891 _relation: PhantomData,
892 })
893 })
894 .collect()
895 }
896}
897pub struct MultiSelectRelatedQuery<M, R1, Relation1, R2: Model, Relation2, Key1 = i64> {
898 query: SelectQuery<M>,
899 first: BelongsTo<M, R1, Relation1, Key1>,
900 second: BelongsTo<M, R2, Relation2, R2::PrimaryKey>,
901}
902impl<M, R1, Relation1, R2, Relation2, Key1>
903 MultiSelectRelatedQuery<M, R1, Relation1, R2, Relation2, Key1>
904where
905 M: Model + Send + 'static,
906 R1: Model + Send + 'static,
907 R2: Model + Send + 'static,
908{
909 pub fn filter(self, expr: Expr) -> Self {
910 Self {
911 query: self.query.filter(expr),
912 ..self
913 }
914 }
915 pub fn order_by(self, order: crate::OrderBy) -> Self {
916 Self {
917 query: self.query.order_by(order),
918 ..self
919 }
920 }
921 pub async fn all(
922 self,
923 db: &Database,
924 ) -> Result<Vec<Loaded<M, (LoadedOne<R1, Relation1>, LoadedOne<R2, Relation2>)>>, OrmError>
925 {
926 let second = self.second;
927 let mut ast = self.query.into_joined_ast(self.first)?;
928 if let QueryAst::Select(select) = &mut ast {
929 select.columns.extend(
930 R2::columns()
931 .iter()
932 .map(|column| crate::ColumnRef::new(second.alias(), column)),
933 );
934 select.joins.push(crate::JoinAst {
935 table: crate::TableRef::new(R2::table_name()),
936 alias: second.alias(),
937 kind: crate::JoinType::Inner,
938 on: crate::Expr::Compare {
939 left: Box::new(Expr::Column(second.field())),
940 op: crate::CompareOp::Eq,
941 right: Box::new(Expr::Column(crate::ColumnRef::new(
942 second.alias(),
943 R2::primary_key().column().name,
944 ))),
945 },
946 });
947 }
948 let offset = M::columns().len();
949 db.rows(ast)
950 .await?
951 .iter()
952 .map(|row| {
953 Ok(Loaded {
954 model: M::from_row_at(row, 0)?,
955 relations: (
956 LoadedOne {
957 related: R1::from_row_at(row, offset)?,
958 _relation: PhantomData,
959 },
960 LoadedOne {
961 related: R2::from_row_at(row, offset + R1::columns().len())?,
962 _relation: PhantomData,
963 },
964 ),
965 })
966 })
967 .collect()
968 }
969}
970
971pub struct PrefetchRelatedQuery<M, R, Relation = (), Key = i64> {
972 query: SelectQuery<M>,
973 relation: HasMany<M, R, Relation, Key>,
974}
975impl<M: Model, R, Relation, Key: crate::ForeignKeyValue<M::PrimaryKey>> PrefetchRelation<M>
976 for HasMany<M, R, Relation, Key>
977{
978 type Query = PrefetchRelatedQuery<M, R, Relation, Key>;
979 fn into_query(self, query: SelectQuery<M>) -> Self::Query {
980 PrefetchRelatedQuery {
981 query,
982 relation: self,
983 }
984 }
985}
986fn ids<M: Model>(models: &[M]) -> Vec<M::PrimaryKey> {
987 models
988 .iter()
989 .map(Model::primary_key_value)
990 .collect::<HashSet<_>>()
991 .into_iter()
992 .collect()
993}
994fn group<M: Model, R, Relation, Key: crate::ForeignKeyValue<M::PrimaryKey>>(
995 relation: &HasMany<M, R, Relation, Key>,
996 models: Vec<R>,
997) -> HashMap<M::PrimaryKey, Vec<R>> {
998 let mut grouped = HashMap::<M::PrimaryKey, Vec<R>>::new();
999 for model in models {
1000 if let Some(key) = crate::ForeignKeyValue::owner_key(relation.foreign_key(&model)) {
1001 grouped.entry(key).or_default().push(model);
1002 }
1003 }
1004 grouped
1005}
1006impl<M, R, Relation, Key> PrefetchRelatedQuery<M, R, Relation, Key>
1007where
1008 M: Model + Send + 'static,
1009 R: Model + Send + 'static,
1010 Key: crate::ForeignKeyValue<M::PrimaryKey>,
1011{
1012 pub fn filter(self, expr: Expr) -> Self {
1013 Self {
1014 query: self.query.filter(expr),
1015 ..self
1016 }
1017 }
1018 pub fn order_by(self, order: crate::OrderBy) -> Self {
1019 Self {
1020 query: self.query.order_by(order),
1021 ..self
1022 }
1023 }
1024 pub fn limit(self, limit: u64) -> Self {
1025 Self {
1026 query: self.query.limit(limit),
1027 ..self
1028 }
1029 }
1030 pub fn offset(self, offset: u64) -> Self {
1031 Self {
1032 query: self.query.offset(offset),
1033 ..self
1034 }
1035 }
1036 pub fn prefetch_related<R2, Relation2, Key2: crate::ForeignKeyValue<M::PrimaryKey>>(
1037 self,
1038 relation: HasMany<M, R2, Relation2, Key2>,
1039 ) -> MultiPrefetchRelatedQuery<M, R, Relation, R2, Relation2, Key, Key2> {
1040 MultiPrefetchRelatedQuery {
1041 query: self.query,
1042 first: self.relation,
1043 second: relation,
1044 }
1045 }
1046 pub async fn count(self, db: &Database) -> Result<usize, OrmError> {
1047 db.count_query(self.query).await
1048 }
1049 pub async fn all(
1050 self,
1051 db: &Database,
1052 ) -> Result<Vec<Loaded<M, (LoadedMany<R, Relation>,)>>, OrmError>
1053 where
1054 Relation: Send + 'static,
1055 {
1056 db.transaction(move |tx| {
1057 Box::pin(async move {
1058 let parents = tx.fetch_all(self.query).await?;
1059 let field = self.relation.field();
1060 let related = tx
1061 .fetch_by_many(
1062 ModelField::<R, M::PrimaryKey>::new(field.table, field.name),
1063 ids(&parents),
1064 )
1065 .await?;
1066 let mut related = group(&self.relation, related);
1067 Ok(parents
1068 .into_iter()
1069 .map(|model| {
1070 let key = model.primary_key_value();
1071 Loaded {
1072 model,
1073 relations: (LoadedMany {
1074 related: related.remove(&key).unwrap_or_default(),
1075 _relation: PhantomData,
1076 },),
1077 }
1078 })
1079 .collect())
1080 })
1081 })
1082 .await
1083 }
1084}
1085pub struct MultiPrefetchRelatedQuery<
1086 M: Model,
1087 R1,
1088 Relation1,
1089 R2,
1090 Relation2,
1091 Key1 = <M as Model>::PrimaryKey,
1092 Key2 = <M as Model>::PrimaryKey,
1093> {
1094 query: SelectQuery<M>,
1095 first: HasMany<M, R1, Relation1, Key1>,
1096 second: HasMany<M, R2, Relation2, Key2>,
1097}
1098impl<M, R1, Relation1, R2, Relation2, Key1, Key2>
1099 MultiPrefetchRelatedQuery<M, R1, Relation1, R2, Relation2, Key1, Key2>
1100where
1101 M: Model + Send + 'static,
1102 R1: Model + Send + 'static,
1103 R2: Model + Send + 'static,
1104 Relation1: Send + 'static,
1105 Relation2: Send + 'static,
1106 Key1: crate::ForeignKeyValue<M::PrimaryKey>,
1107 Key2: crate::ForeignKeyValue<M::PrimaryKey>,
1108{
1109 pub async fn all(
1110 self,
1111 db: &Database,
1112 ) -> Result<Vec<Loaded<M, (LoadedMany<R1, Relation1>, LoadedMany<R2, Relation2>)>>, OrmError>
1113 {
1114 db.transaction(move |tx| {
1115 Box::pin(async move {
1116 let parents = tx.fetch_all(self.query).await?;
1117 let keys = ids(&parents);
1118 let field = self.first.field();
1119 let first = tx
1120 .fetch_by_many(
1121 ModelField::<R1, M::PrimaryKey>::new(field.table, field.name),
1122 keys.clone(),
1123 )
1124 .await?;
1125 let field = self.second.field();
1126 let second = tx
1127 .fetch_by_many(
1128 ModelField::<R2, M::PrimaryKey>::new(field.table, field.name),
1129 keys,
1130 )
1131 .await?;
1132 let mut first = group(&self.first, first);
1133 let mut second = group(&self.second, second);
1134 Ok(parents
1135 .into_iter()
1136 .map(|model| {
1137 let key = model.primary_key_value();
1138 Loaded {
1139 model,
1140 relations: (
1141 LoadedMany {
1142 related: first.remove(&key).unwrap_or_default(),
1143 _relation: PhantomData,
1144 },
1145 LoadedMany {
1146 related: second.remove(&key).unwrap_or_default(),
1147 _relation: PhantomData,
1148 },
1149 ),
1150 }
1151 })
1152 .collect())
1153 })
1154 })
1155 .await
1156 }
1157}
1158pub struct NestedPrefetchPlan<
1159 M: Model,
1160 R: Model,
1161 Relation,
1162 C,
1163 ChildRelation,
1164 OuterKey = <M as Model>::PrimaryKey,
1165 InnerKey = <R as Model>::PrimaryKey,
1166> {
1167 outer: HasMany<M, R, Relation, OuterKey>,
1168 inner: HasMany<R, C, ChildRelation, InnerKey>,
1169}
1170impl<M: Model, R: Model, Relation, Key: crate::ForeignKeyValue<M::PrimaryKey>>
1171 HasMany<M, R, Relation, Key>
1172{
1173 pub fn prefetch<C, ChildRelation, InnerKey: crate::ForeignKeyValue<R::PrimaryKey>>(
1174 self,
1175 inner: HasMany<R, C, ChildRelation, InnerKey>,
1176 ) -> NestedPrefetchPlan<M, R, Relation, C, ChildRelation, Key, InnerKey> {
1177 NestedPrefetchPlan { outer: self, inner }
1178 }
1179}
1180impl<
1181 M: Model,
1182 R: Model,
1183 Relation,
1184 C,
1185 ChildRelation,
1186 OuterKey: crate::ForeignKeyValue<M::PrimaryKey>,
1187 InnerKey: crate::ForeignKeyValue<R::PrimaryKey>,
1188> PrefetchRelation<M> for NestedPrefetchPlan<M, R, Relation, C, ChildRelation, OuterKey, InnerKey>
1189{
1190 type Query = NestedPrefetchQuery<M, R, Relation, C, ChildRelation, OuterKey, InnerKey>;
1191 fn into_query(self, query: SelectQuery<M>) -> Self::Query {
1192 NestedPrefetchQuery {
1193 query,
1194 outer: self.outer,
1195 inner: self.inner,
1196 }
1197 }
1198}
1199pub struct NestedPrefetchQuery<
1200 M: Model,
1201 R: Model,
1202 Relation,
1203 C,
1204 ChildRelation,
1205 OuterKey = <M as Model>::PrimaryKey,
1206 InnerKey = <R as Model>::PrimaryKey,
1207> {
1208 query: SelectQuery<M>,
1209 outer: HasMany<M, R, Relation, OuterKey>,
1210 inner: HasMany<R, C, ChildRelation, InnerKey>,
1211}
1212impl<M, R, Relation, C, ChildRelation, OuterKey, InnerKey>
1213 NestedPrefetchQuery<M, R, Relation, C, ChildRelation, OuterKey, InnerKey>
1214where
1215 M: Model + Send + 'static,
1216 R: Model + Send + 'static,
1217 C: Model + Send + 'static,
1218 Relation: Send + 'static,
1219 ChildRelation: Send + 'static,
1220 OuterKey: crate::ForeignKeyValue<M::PrimaryKey>,
1221 InnerKey: crate::ForeignKeyValue<R::PrimaryKey>,
1222{
1223 pub async fn all(
1224 self,
1225 db: &Database,
1226 ) -> Result<
1227 Vec<Loaded<M, (LoadedMany<Loaded<R, (LoadedMany<C, ChildRelation>,)>, Relation>,)>>,
1228 OrmError,
1229 > {
1230 db.transaction(move |tx| {
1231 Box::pin(async move {
1232 let parents = tx.fetch_all(self.query).await?;
1233 let field = self.outer.field();
1234 let children = tx
1235 .fetch_by_many(
1236 ModelField::<R, M::PrimaryKey>::new(field.table, field.name),
1237 ids(&parents),
1238 )
1239 .await?;
1240 let field = self.inner.field();
1241 let grandchildren = tx
1242 .fetch_by_many(
1243 ModelField::<C, R::PrimaryKey>::new(field.table, field.name),
1244 ids(&children),
1245 )
1246 .await?;
1247 let mut grandchildren = group(&self.inner, grandchildren);
1248 let mut grouped = HashMap::<M::PrimaryKey, Vec<_>>::new();
1249 for model in children {
1250 if let Some(owner) =
1251 crate::ForeignKeyValue::owner_key(self.outer.foreign_key(&model))
1252 {
1253 let related = grandchildren
1254 .remove(&model.primary_key_value())
1255 .unwrap_or_default();
1256 grouped.entry(owner).or_default().push(Loaded {
1257 model,
1258 relations: (LoadedMany {
1259 related,
1260 _relation: PhantomData,
1261 },),
1262 });
1263 }
1264 }
1265 Ok(parents
1266 .into_iter()
1267 .map(|model| {
1268 let key = model.primary_key_value();
1269 Loaded {
1270 model,
1271 relations: (LoadedMany {
1272 related: grouped.remove(&key).unwrap_or_default(),
1273 _relation: PhantomData,
1274 },),
1275 }
1276 })
1277 .collect())
1278 })
1279 })
1280 .await
1281 }
1282}