1use deadpool_sqlite::{Config, Pool, Runtime};
2use rusqlite::{OptionalExtension, types::Value};
3use std::collections::{HashMap, HashSet};
4use time::format_description::well_known::Rfc3339;
5
6use crate::{
7 BelongsTo, CompiledQuery, DatabaseValue, Expr, HasMany, Model, ModelField, QueryAst,
8 QueryBuildError, QueryValue, SelectQuery, SqlCompiler, SqliteDialect, ValidatedWrite,
9};
10
11#[derive(Debug, thiserror::Error)]
12pub enum OrmError {
14 #[error("database pool error: {0}")]
15 Pool(#[from] deadpool_sqlite::PoolError),
16 #[error("database interaction error: {0}")]
17 Interaction(String),
18 #[error("sqlite error: {0}")]
19 Sqlite(#[from] rusqlite::Error),
20 #[error("query build error: {0:?}")]
21 QueryBuild(QueryBuildError),
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct ExecuteResult {
27 pub rows_affected: usize,
28 pub last_insert_rowid: Option<i64>,
29}
30
31#[derive(Clone)]
32pub struct Database {
34 pool: Pool,
35}
36
37impl Database {
38 pub fn pool(&self) -> &Pool {
40 &self.pool
41 }
42
43 pub fn connect(path: impl Into<String>) -> Result<Self, OrmError> {
45 Self::connect_with_pool_size(path, 4)
46 }
47
48 pub fn connect_with_pool_size(
50 path: impl Into<String>,
51 pool_size: usize,
52 ) -> Result<Self, OrmError> {
53 let config = Config::new(path.into());
54 let mut config = config;
55 config.pool = Some(deadpool_sqlite::PoolConfig::new(pool_size));
56 let pool = config
57 .create_pool(Runtime::Tokio1)
58 .map_err(|error| OrmError::Interaction(error.to_string()))?;
59 Ok(Self { pool })
60 }
61
62 pub fn connect_in_memory() -> Result<Self, OrmError> {
64 Self::connect_with_pool_size(":memory:", 1)
65 }
66
67 pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError> {
69 ast.validate().map_err(OrmError::QueryBuild)?;
70 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
71 self.execute_compiled(compiled).await
72 }
73
74 pub async fn create_table<M: Model>(&self) -> Result<ExecuteResult, OrmError> {
76 let schema = M::schema();
77 let compiled = SqlCompiler::<SqliteDialect>::compile_schema(&schema);
78 let pool = self.pool.clone();
79 pool.get()
80 .await?
81 .interact(move |connection| -> rusqlite::Result<ExecuteResult> {
82 configure_connection(connection)?;
83 connection.execute_batch(&compiled.table)?;
84 for index in compiled.indexes {
85 connection.execute_batch(&index)?;
86 }
87 Ok(ExecuteResult {
88 rows_affected: 0,
89 last_insert_rowid: None,
90 })
91 })
92 .await
93 .map_err(|error| OrmError::Interaction(error.to_string()))?
94 .map_err(OrmError::from)
95 }
96
97 pub async fn insert<M: Model>(&self, model: &M) -> Result<ExecuteResult, OrmError> {
99 let ast = QueryAst::Insert(crate::InsertAst {
100 table: crate::TableRef::new(M::table_name()),
101 values: model.insert_values(),
102 returning: Vec::new(),
103 });
104 self.execute(ast).await
105 }
106
107 pub async fn fetch_all<M: Model + Send + 'static>(
109 &self,
110 query: crate::SelectQuery<M>,
111 ) -> Result<Vec<M>, OrmError> {
112 let ast = query.into_ast().map_err(OrmError::QueryBuild)?;
113 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
114 let pool = self.pool.clone();
115 pool.get()
116 .await?
117 .interact(move |connection| {
118 configure_connection(connection)?;
119 let mut statement = connection.prepare(&compiled.sql)?;
120 let params = sqlite_params(&compiled)?;
121 let rows = statement.query_map(rusqlite::params_from_iter(params), M::from_row)?;
122 rows.collect::<rusqlite::Result<Vec<_>>>()
123 })
124 .await
125 .map_err(|error| OrmError::Interaction(error.to_string()))?
126 .map_err(OrmError::from)
127 }
128
129 pub async fn fetch_one<M: Model + Send + 'static>(
131 &self,
132 query: crate::SelectQuery<M>,
133 ) -> Result<Option<M>, OrmError> {
134 let mut rows = self.fetch_all(query.limit(1)).await?;
135 Ok(rows.pop())
136 }
137
138 pub async fn get<M: Model + Send + 'static>(&self, id: i64) -> Result<Option<M>, OrmError> {
140 self.fetch_one(M::query().filter(M::primary_key().eq(id)))
141 .await
142 }
143
144 pub async fn all<M: Model + Send + 'static>(&self) -> Result<Vec<M>, OrmError> {
146 self.fetch_all(M::query()).await
147 }
148
149 pub async fn count<M: Model + Send + 'static>(&self) -> Result<usize, OrmError> {
150 let table = M::table_name();
151 let pool = self.pool.clone();
152 pool.get()
153 .await?
154 .interact(move |connection| {
155 configure_connection(connection)?;
156 connection.query_row(&format!("SELECT COUNT(*) FROM {table}"), [], |row| {
157 row.get::<_, i64>(0)
158 })
159 })
160 .await
161 .map_err(|error| OrmError::Interaction(error.to_string()))?
162 .map(|count| count as usize)
163 .map_err(OrmError::from)
164 }
165
166 pub async fn count_query<M: Model + Send + 'static>(
167 &self,
168 query: SelectQuery<M>,
169 ) -> Result<usize, OrmError> {
170 let ast = query.into_select_ast().map_err(OrmError::QueryBuild)?;
171 let compiled = SqlCompiler::<SqliteDialect>::compile(&QueryAst::Select(ast));
172 let sql = format!("SELECT COUNT(*) FROM ({})", compiled.sql);
173 let pool = self.pool.clone();
174 pool.get()
175 .await?
176 .interact(move |connection| {
177 configure_connection(connection)?;
178 let params = sqlite_params(&compiled)?;
179 connection.query_row(&sql, rusqlite::params_from_iter(params), |row| {
180 row.get::<_, i64>(0)
181 })
182 })
183 .await
184 .map_err(|error| OrmError::Interaction(error.to_string()))?
185 .map(|count| count as usize)
186 .map_err(OrmError::from)
187 }
188
189 pub fn create<M: Model>(&self) -> CreateBuilder<'_, M> {
191 CreateBuilder {
192 database: self,
193 query: M::insert(),
194 }
195 }
196
197 pub fn update<M: Model>(&self, id: i64) -> UpdateBuilder<'_, M> {
199 UpdateBuilder {
200 database: self,
201 query: M::update().filter(M::primary_key().eq(id)),
202 }
203 }
204
205 pub async fn delete<M: Model + Send + 'static>(&self, id: i64) -> Result<bool, OrmError> {
207 let result = self
208 .execute(
209 M::delete()
210 .filter(M::primary_key().eq(id))
211 .into_ast()
212 .map_err(OrmError::QueryBuild)?,
213 )
214 .await?;
215 Ok(result.rows_affected == 1)
216 }
217
218 pub fn query<M: Model>(&self) -> DatabaseQuery<M> {
220 DatabaseQuery { query: M::query() }
221 }
222
223 pub async fn fetch_by<M, T, V>(
228 &self,
229 field: ModelField<M, T>,
230 value: V,
231 ) -> Result<Vec<M>, OrmError>
232 where
233 M: Model + Send + 'static,
234 V: QueryValue<T>,
235 {
236 self.fetch_all(M::query().filter(field.eq(value))).await
237 }
238
239 pub async fn fetch_by_many<M, T, I, V>(
241 &self,
242 field: ModelField<M, T>,
243 values: I,
244 ) -> Result<Vec<M>, OrmError>
245 where
246 M: Model + Send + 'static,
247 I: IntoIterator<Item = V>,
248 V: QueryValue<T>,
249 {
250 const SQLITE_SAFE_VARIABLE_LIMIT: usize = 900;
251 let values = values
252 .into_iter()
253 .map(QueryValue::<T>::into_query_value)
254 .collect::<Vec<_>>();
255 if values.is_empty() {
256 return Ok(Vec::new());
257 }
258 let mut result = Vec::new();
259 for chunk in values.chunks(SQLITE_SAFE_VARIABLE_LIMIT) {
260 let filter = Expr::In {
261 left: Box::new(Expr::Column(field.column())),
262 values: chunk.to_vec(),
263 };
264 result.extend(self.fetch_all(M::query().filter(filter)).await?);
265 }
266 Ok(result)
267 }
268
269 pub async fn transaction<T, F>(&self, action: F) -> Result<T, OrmError>
271 where
272 T: Send + 'static,
273 F: FnOnce(&rusqlite::Connection) -> rusqlite::Result<T> + Send + 'static,
274 {
275 let pool = self.pool.clone();
276 pool.get()
277 .await?
278 .interact(move |connection| -> rusqlite::Result<T> {
279 configure_connection(connection)?;
280 connection.execute_batch("BEGIN")?;
281 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| action(connection)))
282 {
283 Ok(Ok(value)) => match connection.execute_batch("COMMIT") {
284 Ok(()) => Ok(value),
285 Err(error) => {
286 let _ = connection.execute_batch("ROLLBACK");
287 Err(error)
288 }
289 },
290 Ok(Err(error)) => {
291 let _ = connection.execute_batch("ROLLBACK");
292 Err(error)
293 }
294 Err(payload) => {
295 let _ = connection.execute_batch("ROLLBACK");
296 std::panic::resume_unwind(payload);
297 }
298 }
299 })
300 .await
301 .map_err(|error| OrmError::Interaction(error.to_string()))?
302 .map_err(OrmError::from)
303 }
304
305 pub async fn typed_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
309 where
310 T: Send + 'static,
311 F: FnOnce(&Transaction<'_>) -> Result<T, OrmError> + Send + 'static,
312 {
313 let pool = self.pool.clone();
314 pool.get()
315 .await?
316 .interact(move |connection| -> Result<T, OrmError> {
317 configure_connection(connection)?;
318 connection.execute_batch("BEGIN")?;
319 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
320 action(&Transaction { connection })
321 })) {
322 Ok(Ok(value)) => match connection.execute_batch("COMMIT") {
323 Ok(()) => Ok(value),
324 Err(error) => {
325 let _ = connection.execute_batch("ROLLBACK");
326 Err(error.into())
327 }
328 },
329 Ok(Err(error)) => {
330 let _ = connection.execute_batch("ROLLBACK");
331 Err(error)
332 }
333 Err(payload) => {
334 let _ = connection.execute_batch("ROLLBACK");
335 std::panic::resume_unwind(payload);
336 }
337 }
338 })
339 .await
340 .map_err(|error| OrmError::Interaction(error.to_string()))?
341 }
342
343 async fn execute_compiled(&self, compiled: CompiledQuery) -> Result<ExecuteResult, OrmError> {
344 let pool = self.pool.clone();
345 pool.get()
346 .await?
347 .interact(move |connection| -> rusqlite::Result<ExecuteResult> {
348 configure_connection(connection)?;
349 let params = sqlite_params(&compiled)?;
350 let rows_affected =
351 connection.execute(&compiled.sql, rusqlite::params_from_iter(params))?;
352 Ok(ExecuteResult {
353 rows_affected,
354 last_insert_rowid: Some(connection.last_insert_rowid()),
355 })
356 })
357 .await
358 .map_err(|error| OrmError::Interaction(error.to_string()))?
359 .map_err(OrmError::from)
360 }
361
362 async fn execute_returning<M: Model + Send + 'static>(
363 &self,
364 ast: QueryAst,
365 ) -> Result<Option<M>, OrmError> {
366 ast.validate().map_err(OrmError::QueryBuild)?;
367 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
368 let pool = self.pool.clone();
369 pool.get()
370 .await?
371 .interact(move |connection| {
372 configure_connection(connection)?;
373 let mut statement = connection.prepare(&compiled.sql)?;
374 let params = sqlite_params(&compiled)?;
375 statement
376 .query_row(rusqlite::params_from_iter(params), M::from_row)
377 .optional()
378 })
379 .await
380 .map_err(|error| OrmError::Interaction(error.to_string()))?
381 .map_err(OrmError::from)
382 }
383}
384
385pub struct Transaction<'connection> {
387 connection: &'connection rusqlite::Connection,
388}
389
390impl Transaction<'_> {
391 pub fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError> {
393 ast.validate().map_err(OrmError::QueryBuild)?;
394 execute_compiled(self.connection, SqlCompiler::<SqliteDialect>::compile(&ast))
395 }
396
397 pub fn insert<M: Model>(&self, model: &M) -> Result<ExecuteResult, OrmError> {
399 self.execute(QueryAst::Insert(crate::InsertAst {
400 table: crate::TableRef::new(M::table_name()),
401 values: model.insert_values(),
402 returning: Vec::new(),
403 }))
404 }
405
406 pub fn fetch_all<M: Model>(&self, query: SelectQuery<M>) -> Result<Vec<M>, OrmError> {
408 let ast = query.into_ast().map_err(OrmError::QueryBuild)?;
409 load_models(
410 self.connection,
411 &SqlCompiler::<SqliteDialect>::compile(&ast),
412 )
413 .map_err(OrmError::from)
414 }
415
416 pub fn fetch_one<M: Model>(&self, query: SelectQuery<M>) -> Result<Option<M>, OrmError> {
418 Ok(self.fetch_all(query.limit(1))?.pop())
419 }
420
421 pub fn get<M: Model>(&self, id: i64) -> Result<Option<M>, OrmError> {
423 self.fetch_one(M::query().filter(M::primary_key().eq(id)))
424 }
425
426 pub fn create<M: Model>(&self) -> TransactionCreateBuilder<'_, M> {
428 TransactionCreateBuilder {
429 connection: self.connection,
430 query: M::insert(),
431 }
432 }
433
434 pub fn update<M: Model>(&self, id: i64) -> TransactionUpdateBuilder<'_, M> {
436 TransactionUpdateBuilder {
437 connection: self.connection,
438 query: M::update().filter(M::primary_key().eq(id)),
439 }
440 }
441
442 pub fn query<M: Model>(&self) -> TransactionQuery<'_, M> {
444 TransactionQuery {
445 connection: self.connection,
446 query: M::query(),
447 }
448 }
449
450 fn execute_returning<M: Model>(&self, ast: QueryAst) -> Result<Option<M>, OrmError> {
451 ast.validate().map_err(OrmError::QueryBuild)?;
452 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
453 let mut statement = self.connection.prepare(&compiled.sql)?;
454 let params = sqlite_params(&compiled)?;
455 statement
456 .query_row(rusqlite::params_from_iter(params), M::from_row)
457 .optional()
458 .map_err(OrmError::from)
459 }
460}
461
462pub struct TransactionCreateBuilder<'transaction, M> {
464 connection: &'transaction rusqlite::Connection,
465 query: crate::InsertQuery<M>,
466}
467
468impl<M: Model> TransactionCreateBuilder<'_, M> {
469 pub fn set<T, V>(self, field: ModelField<M, T>, value: V) -> Self
470 where
471 V: QueryValue<T>,
472 {
473 Self {
474 connection: self.connection,
475 query: self.query.set(field, value),
476 }
477 }
478
479 pub fn execute(self) -> Result<M, OrmError> {
480 Transaction {
481 connection: self.connection,
482 }
483 .execute_returning(
484 self.query
485 .returning_all()
486 .into_ast()
487 .map_err(OrmError::QueryBuild)?,
488 )?
489 .ok_or_else(|| OrmError::Interaction("insert did not return a row".into()))
490 }
491}
492
493pub struct TransactionUpdateBuilder<'transaction, M> {
495 connection: &'transaction rusqlite::Connection,
496 query: crate::UpdateQuery<M>,
497}
498
499impl<M: Model> TransactionUpdateBuilder<'_, M> {
500 pub fn set<T, V>(self, field: ModelField<M, T>, value: V) -> Self
501 where
502 V: QueryValue<T>,
503 {
504 Self {
505 connection: self.connection,
506 query: self.query.set(field, value),
507 }
508 }
509
510 pub fn execute(self) -> Result<Option<M>, OrmError> {
511 Transaction {
512 connection: self.connection,
513 }
514 .execute_returning(
515 self.query
516 .returning_all()
517 .into_ast()
518 .map_err(OrmError::QueryBuild)?,
519 )
520 }
521}
522
523pub struct TransactionQuery<'transaction, M> {
525 connection: &'transaction rusqlite::Connection,
526 query: SelectQuery<M>,
527}
528
529impl<M: Model> TransactionQuery<'_, M> {
530 pub fn filter(self, expr: Expr) -> Self {
531 Self {
532 connection: self.connection,
533 query: self.query.filter(expr),
534 }
535 }
536
537 pub fn order_by(self, order: crate::OrderBy) -> Self {
538 Self {
539 connection: self.connection,
540 query: self.query.order_by(order),
541 }
542 }
543
544 pub fn limit(self, limit: u64) -> Self {
545 Self {
546 connection: self.connection,
547 query: self.query.limit(limit),
548 }
549 }
550
551 pub fn all(self) -> Result<Vec<M>, OrmError> {
552 Transaction {
553 connection: self.connection,
554 }
555 .fetch_all(self.query)
556 }
557
558 pub fn first(self) -> Result<Option<M>, OrmError> {
559 Transaction {
560 connection: self.connection,
561 }
562 .fetch_one(self.query)
563 }
564}
565
566impl<M: Model + Send + 'static> ValidatedWrite<M> {
567 pub async fn save(self, database: &Database) -> Result<Option<M>, OrmError> {
568 let ast = match self {
569 Self::Create(query) => query
570 .returning_all()
571 .into_ast()
572 .map_err(OrmError::QueryBuild)?,
573 Self::Update(query) => query
574 .returning_all()
575 .into_ast()
576 .map_err(OrmError::QueryBuild)?,
577 };
578 database.execute_returning(ast).await
579 }
580}
581
582pub struct CreateBuilder<'db, M> {
584 database: &'db Database,
585 query: crate::InsertQuery<M>,
586}
587
588impl<M: Model> CreateBuilder<'_, M> {
589 pub fn set<T, V>(self, field: ModelField<M, T>, value: V) -> Self
590 where
591 V: QueryValue<T>,
592 {
593 Self {
594 database: self.database,
595 query: self.query.set(field, value),
596 }
597 }
598
599 pub async fn execute(self) -> Result<M, OrmError>
600 where
601 M: Send + 'static,
602 {
603 let ast = self
604 .query
605 .returning_all()
606 .into_ast()
607 .map_err(OrmError::QueryBuild)?;
608 self.database
609 .execute_returning(ast)
610 .await?
611 .ok_or_else(|| OrmError::Interaction("insert did not return a row".into()))
612 }
613}
614
615pub struct UpdateBuilder<'db, M> {
617 database: &'db Database,
618 query: crate::UpdateQuery<M>,
619}
620
621impl<M: Model> UpdateBuilder<'_, M> {
622 pub fn set<T, V>(self, field: ModelField<M, T>, value: V) -> Self
623 where
624 V: QueryValue<T>,
625 {
626 Self {
627 database: self.database,
628 query: self.query.set(field, value),
629 }
630 }
631
632 pub async fn execute(self) -> Result<Option<M>, OrmError>
633 where
634 M: Send + 'static,
635 {
636 let ast = self
637 .query
638 .returning_all()
639 .into_ast()
640 .map_err(OrmError::QueryBuild)?;
641 self.database.execute_returning(ast).await
642 }
643}
644
645pub struct DatabaseQuery<M> {
647 query: SelectQuery<M>,
648}
649
650impl<M: Model> DatabaseQuery<M> {
651 pub fn new(query: SelectQuery<M>) -> Self {
652 Self { query }
653 }
654
655 pub fn filter(self, expr: Expr) -> Self {
656 Self {
657 query: self.query.filter(expr),
658 }
659 }
660
661 pub fn order_by(self, order: crate::OrderBy) -> Self {
662 Self {
663 query: self.query.order_by(order),
664 }
665 }
666
667 pub fn limit(self, limit: u64) -> Self {
668 Self {
669 query: self.query.limit(limit),
670 }
671 }
672
673 pub fn offset(self, offset: u64) -> Self {
674 Self {
675 query: self.query.offset(offset),
676 }
677 }
678
679 pub fn select_related<R, Relation, Key>(
681 self,
682 relation: BelongsTo<M, R, Relation, Key>,
683 ) -> SelectRelatedQuery<M, R, Relation, Key> {
684 SelectRelatedQuery {
685 query: self.query,
686 relation,
687 }
688 }
689
690 pub fn prefetch_related<P>(self, plan: P) -> P::Query
692 where
693 P: PrefetchRelation<M>,
694 {
695 plan.into_query(self.query)
696 }
697
698 pub async fn all(self, database: &Database) -> Result<Vec<M>, OrmError>
699 where
700 M: Send + 'static,
701 {
702 database.fetch_all(self.query).await
703 }
704
705 pub async fn first(self, database: &Database) -> Result<Option<M>, OrmError>
706 where
707 M: Send + 'static,
708 {
709 database.fetch_one(self.query).await
710 }
711
712 pub async fn count(self, database: &Database) -> Result<usize, OrmError>
713 where
714 M: Send + 'static,
715 {
716 database.count_query(self.query).await
717 }
718
719 pub fn into_select_query(self) -> SelectQuery<M> {
720 self.query
721 }
722}
723
724pub trait PrefetchRelation<Owner: Model> {
725 type Query;
726
727 fn into_query(self, query: SelectQuery<Owner>) -> Self::Query;
728}
729
730#[derive(Debug)]
732pub struct WithOne<M, R, Relation = (), Key = i64> {
733 pub model: M,
734 pub related: R,
735 pub(crate) _relation: std::marker::PhantomData<Relation>,
736 pub(crate) _key: std::marker::PhantomData<Key>,
737}
738
739#[derive(Debug)]
740pub struct WithOptionalOne<M, R, Relation = ()> {
741 pub model: M,
742 pub related: Option<R>,
743 pub(crate) _relation: std::marker::PhantomData<Relation>,
744}
745
746#[derive(Debug)]
748pub struct WithMany<M, R, Relation = (), Key = i64> {
749 pub model: M,
750 pub related: Vec<R>,
751 pub(crate) _relation: std::marker::PhantomData<Relation>,
752 pub(crate) _key: std::marker::PhantomData<Key>,
753}
754
755#[derive(Debug)]
757pub struct Loaded<M, Relations> {
758 pub model: M,
759 pub relations: Relations,
760}
761
762#[derive(Debug)]
764pub struct LoadedMany<R, Relation = ()> {
765 pub related: Vec<R>,
766 pub(crate) _relation: std::marker::PhantomData<Relation>,
767}
768
769#[derive(Debug)]
770pub struct LoadedOne<R, Relation = ()> {
771 pub related: R,
772 pub(crate) _relation: std::marker::PhantomData<Relation>,
773}
774
775pub struct SelectRelatedQuery<M, R, Relation = (), Key = i64> {
777 query: SelectQuery<M>,
778 relation: BelongsTo<M, R, Relation, Key>,
779}
780
781impl<M, R, Relation> SelectRelatedQuery<M, R, Relation, i64>
782where
783 M: Model + Send + 'static,
784 R: Model + Send + 'static,
785{
786 pub fn filter(self, expr: Expr) -> Self {
787 Self {
788 query: self.query.filter(expr),
789 relation: self.relation,
790 }
791 }
792
793 pub fn order_by(self, order: crate::OrderBy) -> Self {
794 Self {
795 query: self.query.order_by(order),
796 relation: self.relation,
797 }
798 }
799
800 pub fn limit(self, limit: u64) -> Self {
801 Self {
802 query: self.query.limit(limit),
803 relation: self.relation,
804 }
805 }
806
807 pub fn offset(self, offset: u64) -> Self {
808 Self {
809 query: self.query.offset(offset),
810 relation: self.relation,
811 }
812 }
813
814 pub fn select_related<R2, Relation2>(
815 self,
816 relation: BelongsTo<M, R2, Relation2, i64>,
817 ) -> MultiSelectRelatedQuery<M, R, Relation, R2, Relation2> {
818 MultiSelectRelatedQuery {
819 query: self.query,
820 first: self.relation,
821 second: relation,
822 }
823 }
824
825 pub async fn all(self, database: &Database) -> Result<Vec<WithOne<M, R, Relation>>, OrmError>
826 where
827 Relation: Send + 'static,
828 {
829 let ast = self
830 .query
831 .into_joined_ast(self.relation)
832 .map_err(OrmError::QueryBuild)?;
833 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
834 let pool = database.pool.clone();
835 pool.get()
836 .await?
837 .interact(move |connection| {
838 configure_connection(connection)?;
839 load_joined_models::<M, R, Relation>(connection, &compiled)
840 })
841 .await
842 .map_err(|error| OrmError::Interaction(error.to_string()))?
843 .map_err(OrmError::from)
844 }
845
846 pub async fn count(self, database: &Database) -> Result<usize, OrmError> {
847 database.count_query(self.query).await
848 }
849}
850
851pub struct MultiSelectRelatedQuery<M, R1, Relation1, R2, Relation2> {
852 query: SelectQuery<M>,
853 first: BelongsTo<M, R1, Relation1, i64>,
854 second: BelongsTo<M, R2, Relation2, i64>,
855}
856
857impl<M, R1, Relation1, R2, Relation2> MultiSelectRelatedQuery<M, R1, Relation1, R2, Relation2>
858where
859 M: Model + Send + 'static,
860 R1: Model + Send + 'static,
861 R2: Model + Send + 'static,
862 Relation1: Send + 'static,
863 Relation2: Send + 'static,
864{
865 pub fn filter(self, expr: Expr) -> Self {
866 Self {
867 query: self.query.filter(expr),
868 first: self.first,
869 second: self.second,
870 }
871 }
872
873 pub fn order_by(self, order: crate::OrderBy) -> Self {
874 Self {
875 query: self.query.order_by(order),
876 first: self.first,
877 second: self.second,
878 }
879 }
880
881 pub async fn all(
882 self,
883 database: &Database,
884 ) -> Result<Vec<Loaded<M, (LoadedOne<R1, Relation1>, LoadedOne<R2, Relation2>)>>, OrmError>
885 {
886 let first = self.first;
887 let second = self.second;
888 let mut ast = self
889 .query
890 .into_joined_ast(first)
891 .map_err(OrmError::QueryBuild)?;
892 if let QueryAst::Select(select) = &mut ast {
893 select.columns.extend(
894 R2::columns()
895 .iter()
896 .map(|column| crate::ColumnRef::new(second.alias(), column)),
897 );
898 select.joins.push(crate::JoinAst {
899 table: crate::TableRef::new(R2::table_name()),
900 alias: second.alias(),
901 kind: crate::JoinType::Inner,
902 on: crate::Expr::Compare {
903 left: Box::new(crate::Expr::Column(second.field())),
904 op: crate::CompareOp::Eq,
905 right: Box::new(crate::Expr::Column(crate::ColumnRef::new(
906 second.alias(),
907 R2::primary_key().column().name,
908 ))),
909 },
910 });
911 }
912 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
913 let pool = database.pool.clone();
914 pool.get()
915 .await?
916 .interact(move |connection| {
917 configure_connection(connection)?;
918 load_multi_joined_models::<M, R1, Relation1, R2, Relation2>(connection, &compiled)
919 })
920 .await
921 .map_err(|error| OrmError::Interaction(error.to_string()))?
922 .map_err(OrmError::from)
923 }
924}
925
926impl<M, R, Relation> SelectRelatedQuery<M, R, Relation, Option<i64>>
927where
928 M: Model + Send + 'static,
929 R: Model + Send + 'static,
930{
931 pub fn filter(self, expr: Expr) -> Self {
932 Self {
933 query: self.query.filter(expr),
934 relation: self.relation,
935 }
936 }
937
938 pub fn order_by(self, order: crate::OrderBy) -> Self {
939 Self {
940 query: self.query.order_by(order),
941 relation: self.relation,
942 }
943 }
944
945 pub fn limit(self, limit: u64) -> Self {
946 Self {
947 query: self.query.limit(limit),
948 relation: self.relation,
949 }
950 }
951
952 pub fn offset(self, offset: u64) -> Self {
953 Self {
954 query: self.query.offset(offset),
955 relation: self.relation,
956 }
957 }
958
959 pub async fn all(
960 self,
961 database: &Database,
962 ) -> Result<Vec<WithOptionalOne<M, R, Relation>>, OrmError>
963 where
964 Relation: Send + 'static,
965 {
966 let ast = self
967 .query
968 .into_optional_joined_ast(self.relation)
969 .map_err(OrmError::QueryBuild)?;
970 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
971 let pool = database.pool.clone();
972 pool.get()
973 .await?
974 .interact(move |connection| {
975 configure_connection(connection)?;
976 load_optional_joined_models::<M, R, Relation>(connection, &compiled)
977 })
978 .await
979 .map_err(|error| OrmError::Interaction(error.to_string()))?
980 .map_err(OrmError::from)
981 }
982
983 pub async fn count(self, database: &Database) -> Result<usize, OrmError> {
984 database.count_query(self.query).await
985 }
986}
987
988pub struct PrefetchRelatedQuery<M, R, Relation = (), Key = i64> {
990 query: SelectQuery<M>,
991 relation: HasMany<M, R, Relation, Key>,
992}
993
994impl<M: Model, R, Relation> PrefetchRelation<M> for HasMany<M, R, Relation, i64> {
995 type Query = PrefetchRelatedQuery<M, R, Relation, i64>;
996
997 fn into_query(self, query: SelectQuery<M>) -> Self::Query {
998 PrefetchRelatedQuery {
999 query,
1000 relation: self,
1001 }
1002 }
1003}
1004
1005pub struct NestedPrefetchPlan<M, R, Relation, C, ChildRelation> {
1006 outer: HasMany<M, R, Relation, i64>,
1007 inner: HasMany<R, C, ChildRelation, i64>,
1008}
1009
1010impl<M, R, Relation> HasMany<M, R, Relation, i64> {
1011 pub fn prefetch<C, ChildRelation>(
1012 self,
1013 inner: HasMany<R, C, ChildRelation, i64>,
1014 ) -> NestedPrefetchPlan<M, R, Relation, C, ChildRelation> {
1015 NestedPrefetchPlan { outer: self, inner }
1016 }
1017}
1018
1019impl<M: Model, R, Relation, C, ChildRelation> PrefetchRelation<M>
1020 for NestedPrefetchPlan<M, R, Relation, C, ChildRelation>
1021{
1022 type Query = NestedPrefetchQuery<M, R, Relation, C, ChildRelation>;
1023
1024 fn into_query(self, query: SelectQuery<M>) -> Self::Query {
1025 NestedPrefetchQuery {
1026 query,
1027 outer: self.outer,
1028 inner: self.inner,
1029 }
1030 }
1031}
1032
1033pub struct NestedPrefetchQuery<M, R, Relation, C, ChildRelation> {
1034 query: SelectQuery<M>,
1035 outer: HasMany<M, R, Relation, i64>,
1036 inner: HasMany<R, C, ChildRelation, i64>,
1037}
1038
1039impl<M, R, Relation> PrefetchRelatedQuery<M, R, Relation, i64>
1040where
1041 M: Model + Send + 'static,
1042 R: Model + Send + 'static,
1043{
1044 pub fn filter(self, expr: Expr) -> Self {
1045 Self {
1046 query: self.query.filter(expr),
1047 relation: self.relation,
1048 }
1049 }
1050
1051 pub fn order_by(self, order: crate::OrderBy) -> Self {
1052 Self {
1053 query: self.query.order_by(order),
1054 relation: self.relation,
1055 }
1056 }
1057
1058 pub fn limit(self, limit: u64) -> Self {
1059 Self {
1060 query: self.query.limit(limit),
1061 relation: self.relation,
1062 }
1063 }
1064
1065 pub fn offset(self, offset: u64) -> Self {
1066 Self {
1067 query: self.query.offset(offset),
1068 relation: self.relation,
1069 }
1070 }
1071
1072 pub fn prefetch_related<R2, Relation2>(
1073 self,
1074 relation: HasMany<M, R2, Relation2>,
1075 ) -> MultiPrefetchRelatedQuery<M, R, Relation, R2, Relation2> {
1076 MultiPrefetchRelatedQuery {
1077 query: self.query,
1078 first: self.relation,
1079 second: relation,
1080 }
1081 }
1082
1083 pub async fn all(
1084 self,
1085 database: &Database,
1086 ) -> Result<Vec<Loaded<M, (LoadedMany<R, Relation>,)>>, OrmError>
1087 where
1088 Relation: Send + 'static,
1089 {
1090 let parent_ast = self.query.into_ast().map_err(OrmError::QueryBuild)?;
1091 let parent_compiled = SqlCompiler::<SqliteDialect>::compile(&parent_ast);
1092 let relation = self.relation;
1093 let pool = database.pool.clone();
1094 pool.get()
1095 .await?
1096 .interact(move |connection| {
1097 configure_connection(connection)?;
1098 connection.execute_batch("BEGIN")?;
1099 let result: rusqlite::Result<Vec<Loaded<M, (LoadedMany<R, Relation>,)>>> = (|| {
1100 let parents = load_models::<M>(connection, &parent_compiled)?;
1101 let parent_ids = parents
1102 .iter()
1103 .map(Model::primary_key_value)
1104 .collect::<HashSet<_>>()
1105 .into_iter()
1106 .collect::<Vec<_>>();
1107 let field = relation.field();
1108 let related = load_models_by_ids(
1109 connection,
1110 ModelField::<R, i64>::new(field.table, field.name),
1111 parent_ids,
1112 )?;
1113 let mut related_by_parent = HashMap::<i64, Vec<R>>::new();
1114 for model in related {
1115 related_by_parent
1116 .entry(relation.foreign_key(&model))
1117 .or_default()
1118 .push(model);
1119 }
1120 Ok(parents
1121 .into_iter()
1122 .map(|model| {
1123 let id = model.primary_key_value();
1124 Loaded {
1125 model,
1126 relations: (LoadedMany {
1127 related: related_by_parent.remove(&id).unwrap_or_default(),
1128 _relation: std::marker::PhantomData,
1129 },),
1130 }
1131 })
1132 .collect())
1133 })(
1134 );
1135 match result {
1136 Ok(value) => {
1137 connection.execute_batch("COMMIT")?;
1138 Ok(value)
1139 }
1140 Err(error) => {
1141 let _ = connection.execute_batch("ROLLBACK");
1142 Err(error)
1143 }
1144 }
1145 })
1146 .await
1147 .map_err(|error| OrmError::Interaction(error.to_string()))?
1148 .map_err(OrmError::from)
1149 }
1150
1151 pub async fn count(self, database: &Database) -> Result<usize, OrmError> {
1152 database.count_query(self.query).await
1153 }
1154}
1155
1156pub struct MultiPrefetchRelatedQuery<M, R1, Relation1, R2, Relation2> {
1158 query: SelectQuery<M>,
1159 first: HasMany<M, R1, Relation1>,
1160 second: HasMany<M, R2, Relation2>,
1161}
1162
1163impl<M, R, Relation, C, ChildRelation> NestedPrefetchQuery<M, R, Relation, C, ChildRelation>
1164where
1165 M: Model + Send + 'static,
1166 R: Model + Send + 'static,
1167 C: Model + Send + 'static,
1168 Relation: Send + 'static,
1169 ChildRelation: Send + 'static,
1170{
1171 pub async fn all(
1172 self,
1173 database: &Database,
1174 ) -> Result<
1175 Vec<Loaded<M, (LoadedMany<Loaded<R, (LoadedMany<C, ChildRelation>,)>, Relation>,)>>,
1176 OrmError,
1177 > {
1178 let parents = database.fetch_all(self.query).await?;
1179 let parent_ids = parents
1180 .iter()
1181 .map(Model::primary_key_value)
1182 .collect::<HashSet<_>>()
1183 .into_iter()
1184 .collect::<Vec<_>>();
1185 let outer_field = self.outer.field();
1186 let children = database
1187 .fetch_by_many(
1188 ModelField::<R, i64>::new(outer_field.table, outer_field.name),
1189 parent_ids,
1190 )
1191 .await?;
1192 let child_ids = children
1193 .iter()
1194 .map(Model::primary_key_value)
1195 .collect::<HashSet<_>>()
1196 .into_iter()
1197 .collect::<Vec<_>>();
1198 let inner_field = self.inner.field();
1199 let grandchildren = database
1200 .fetch_by_many(
1201 ModelField::<C, i64>::new(inner_field.table, inner_field.name),
1202 child_ids,
1203 )
1204 .await?;
1205 let mut grandchildren_by_child = HashMap::<i64, Vec<C>>::new();
1206 for model in grandchildren {
1207 grandchildren_by_child
1208 .entry(self.inner.foreign_key(&model))
1209 .or_default()
1210 .push(model);
1211 }
1212 let children = children
1213 .into_iter()
1214 .map(|model| {
1215 let id = model.primary_key_value();
1216 Loaded {
1217 model,
1218 relations: (LoadedMany {
1219 related: grandchildren_by_child.remove(&id).unwrap_or_default(),
1220 _relation: std::marker::PhantomData,
1221 },),
1222 }
1223 })
1224 .collect::<Vec<_>>();
1225 let mut children_by_parent = HashMap::<i64, Vec<_>>::new();
1226 for child in children {
1227 children_by_parent
1228 .entry(self.outer.foreign_key(&child.model))
1229 .or_default()
1230 .push(child);
1231 }
1232 Ok(parents
1233 .into_iter()
1234 .map(|model| {
1235 let id = model.primary_key_value();
1236 Loaded {
1237 model,
1238 relations: (LoadedMany {
1239 related: children_by_parent.remove(&id).unwrap_or_default(),
1240 _relation: std::marker::PhantomData,
1241 },),
1242 }
1243 })
1244 .collect())
1245 }
1246}
1247
1248impl<M, R1, Relation1, R2, Relation2> MultiPrefetchRelatedQuery<M, R1, Relation1, R2, Relation2>
1249where
1250 M: Model + Send + 'static,
1251 R1: Model + Send + 'static,
1252 R2: Model + Send + 'static,
1253 Relation1: Send + 'static,
1254 Relation2: Send + 'static,
1255{
1256 pub async fn all(
1257 self,
1258 database: &Database,
1259 ) -> Result<Vec<Loaded<M, (LoadedMany<R1, Relation1>, LoadedMany<R2, Relation2>)>>, OrmError>
1260 {
1261 let parents = database.fetch_all(self.query).await?;
1262 let parent_ids = parents
1263 .iter()
1264 .map(Model::primary_key_value)
1265 .collect::<HashSet<_>>()
1266 .into_iter()
1267 .collect::<Vec<_>>();
1268 let first_field = self.first.field();
1269 let second_field = self.second.field();
1270 let first = database
1271 .fetch_by_many(
1272 ModelField::<R1, i64>::new(first_field.table, first_field.name),
1273 parent_ids.clone(),
1274 )
1275 .await?;
1276 let second = database
1277 .fetch_by_many(
1278 ModelField::<R2, i64>::new(second_field.table, second_field.name),
1279 parent_ids,
1280 )
1281 .await?;
1282 let mut first_by_parent = HashMap::<i64, Vec<R1>>::new();
1283 for model in first {
1284 first_by_parent
1285 .entry(self.first.foreign_key(&model))
1286 .or_default()
1287 .push(model);
1288 }
1289 let mut second_by_parent = HashMap::<i64, Vec<R2>>::new();
1290 for model in second {
1291 second_by_parent
1292 .entry(self.second.foreign_key(&model))
1293 .or_default()
1294 .push(model);
1295 }
1296
1297 Ok(parents
1298 .into_iter()
1299 .map(|model| {
1300 let id = model.primary_key_value();
1301 Loaded {
1302 model,
1303 relations: (
1304 LoadedMany {
1305 related: first_by_parent.remove(&id).unwrap_or_default(),
1306 _relation: std::marker::PhantomData,
1307 },
1308 LoadedMany {
1309 related: second_by_parent.remove(&id).unwrap_or_default(),
1310 _relation: std::marker::PhantomData,
1311 },
1312 ),
1313 }
1314 })
1315 .collect())
1316 }
1317}
1318
1319fn load_joined_models<M: Model, R: Model, Relation>(
1320 connection: &rusqlite::Connection,
1321 compiled: &CompiledQuery,
1322) -> rusqlite::Result<Vec<WithOne<M, R, Relation>>> {
1323 let mut statement = connection.prepare(&compiled.sql)?;
1324 let params = sqlite_params(compiled)?;
1325 let offset = M::columns().len();
1326 let rows = statement.query_map(rusqlite::params_from_iter(params), |row| {
1327 Ok(WithOne {
1328 model: M::from_row_at(row, 0)?,
1329 related: R::from_row_at(row, offset)?,
1330 _relation: std::marker::PhantomData,
1331 _key: std::marker::PhantomData,
1332 })
1333 })?;
1334 rows.collect()
1335}
1336
1337fn load_optional_joined_models<M: Model, R: Model, Relation>(
1338 connection: &rusqlite::Connection,
1339 compiled: &CompiledQuery,
1340) -> rusqlite::Result<Vec<WithOptionalOne<M, R, Relation>>> {
1341 let mut statement = connection.prepare(&compiled.sql)?;
1342 let params = sqlite_params(compiled)?;
1343 let offset = M::columns().len();
1344 let related_primary_key_offset = R::columns()
1345 .iter()
1346 .position(|column| *column == R::primary_key().column().name)
1347 .expect("primary key must be one of the model columns");
1348 let rows = statement.query_map(rusqlite::params_from_iter(params), |row| {
1349 let related_id: Option<i64> = row.get(offset + related_primary_key_offset)?;
1350 Ok(WithOptionalOne {
1351 model: M::from_row_at(row, 0)?,
1352 related: related_id
1353 .map(|_| R::from_row_at(row, offset))
1354 .transpose()?,
1355 _relation: std::marker::PhantomData,
1356 })
1357 })?;
1358 rows.collect()
1359}
1360
1361fn load_multi_joined_models<M: Model, R1: Model, Relation1, R2: Model, Relation2>(
1362 connection: &rusqlite::Connection,
1363 compiled: &CompiledQuery,
1364) -> rusqlite::Result<Vec<Loaded<M, (LoadedOne<R1, Relation1>, LoadedOne<R2, Relation2>)>>> {
1365 let mut statement = connection.prepare(&compiled.sql)?;
1366 let params = sqlite_params(compiled)?;
1367 let first_offset = M::columns().len();
1368 let second_offset = first_offset + R1::columns().len();
1369 let rows = statement.query_map(rusqlite::params_from_iter(params), |row| {
1370 Ok(Loaded {
1371 model: M::from_row_at(row, 0)?,
1372 relations: (
1373 LoadedOne {
1374 related: R1::from_row_at(row, first_offset)?,
1375 _relation: std::marker::PhantomData,
1376 },
1377 LoadedOne {
1378 related: R2::from_row_at(row, second_offset)?,
1379 _relation: std::marker::PhantomData,
1380 },
1381 ),
1382 })
1383 })?;
1384 rows.collect()
1385}
1386
1387fn load_models<M: Model>(
1388 connection: &rusqlite::Connection,
1389 compiled: &CompiledQuery,
1390) -> rusqlite::Result<Vec<M>> {
1391 let mut statement = connection.prepare(&compiled.sql)?;
1392 let params = sqlite_params(compiled)?;
1393 let rows = statement.query_map(rusqlite::params_from_iter(params), M::from_row)?;
1394 rows.collect()
1395}
1396
1397fn load_models_by_ids<M: Model>(
1398 connection: &rusqlite::Connection,
1399 field: ModelField<M, i64>,
1400 ids: Vec<i64>,
1401) -> rusqlite::Result<Vec<M>> {
1402 const SQLITE_SAFE_VARIABLE_LIMIT: usize = 900;
1403 let mut result = Vec::new();
1404 for chunk in ids.chunks(SQLITE_SAFE_VARIABLE_LIMIT) {
1405 let ast = M::query()
1406 .filter(Expr::In {
1407 left: Box::new(Expr::Column(field.column())),
1408 values: chunk.iter().copied().map(DatabaseValue::Integer).collect(),
1409 })
1410 .into_ast()
1411 .map_err(|_| rusqlite::Error::InvalidQuery)?;
1412 let compiled = SqlCompiler::<SqliteDialect>::compile(&ast);
1413 result.extend(load_models(connection, &compiled)?);
1414 }
1415 Ok(result)
1416}
1417
1418fn configure_connection(connection: &rusqlite::Connection) -> rusqlite::Result<()> {
1419 connection.execute_batch("PRAGMA foreign_keys = ON")
1420}
1421
1422fn execute_compiled(
1423 connection: &rusqlite::Connection,
1424 compiled: CompiledQuery,
1425) -> Result<ExecuteResult, OrmError> {
1426 let params = sqlite_params(&compiled)?;
1427 let rows_affected = connection.execute(&compiled.sql, rusqlite::params_from_iter(params))?;
1428 Ok(ExecuteResult {
1429 rows_affected,
1430 last_insert_rowid: Some(connection.last_insert_rowid()),
1431 })
1432}
1433
1434fn sqlite_params(compiled: &CompiledQuery) -> rusqlite::Result<Vec<Value>> {
1435 compiled.params.iter().map(database_value).collect()
1436}
1437
1438fn database_value(value: &DatabaseValue) -> rusqlite::Result<Value> {
1439 match value {
1440 DatabaseValue::Integer(value) => Ok(Value::Integer(*value)),
1441 DatabaseValue::Text(value) => Ok(Value::Text(value.clone())),
1442 DatabaseValue::Binary(value) => Ok(Value::Blob(value.clone())),
1443 DatabaseValue::Boolean(value) => Ok(Value::Integer(i64::from(*value))),
1444 DatabaseValue::DateTime(value) => value
1445 .format(&Rfc3339)
1446 .map(Value::Text)
1447 .map_err(|error| rusqlite::Error::ToSqlConversionFailure(error.into())),
1448 DatabaseValue::Null => Ok(Value::Null),
1449 }
1450}