diesel/associations/belongs_to.rs
1use super::{HasTable, Identifiable};
2use dsl::{Eq, EqAny, Filter, FindBy};
3use expression::array_comparison::AsInExpression;
4use expression::AsExpression;
5use prelude::*;
6use query_dsl::methods::FilterDsl;
7
8use std::borrow::Borrow;
9use std::hash::Hash;
10
11/// Indicates that a type belongs to `Parent`
12///
13/// Specifically, this means that this struct has fields
14/// which correspond to the primary key of `Parent`.
15/// This implies that a foreign key relationship exists on the tables.
16///
17/// This trait is not capable of supporting composite foreign keys
18pub trait BelongsTo<Parent> {
19 /// The foreign key of this struct
20 type ForeignKey: Hash + ::std::cmp::Eq;
21 /// The database column representing the foreign key
22 /// of the table this struct represents
23 type ForeignKeyColumn: Column;
24
25 /// Returns the foreign key for `self`
26 fn foreign_key(&self) -> Option<&Self::ForeignKey>;
27 /// Returns the foreign key column of this struct's table
28 fn foreign_key_column() -> Self::ForeignKeyColumn;
29}
30
31/// The `grouped_by` function groups records by their parent.
32///
33/// `grouped_by` is called on a `Vec<Child>` with a `&[Parent]`.
34/// The return value will be `Vec<Vec<Child>>` indexed to match their parent.
35/// Or to put it another way, the returned data can be passed to `zip`,
36/// and it will be combined with its parent.
37/// This function does not generate a `GROUP BY` SQL statement,
38/// as it operates on data structures already loaded from the database
39///
40/// **Child** refers to the "many" part of a "one to many" relationship. It "belongs to" its parent
41/// **Parent** refers to the "one" part of a "one to many" relationship and can "have many" children.
42/// The child always has a foreign key, which refers to its parent's primary key.
43/// In the following relationship, User has many Posts,
44/// so User is the parent and Posts are children.
45///
46/// # Example
47///
48/// ```rust
49/// # #[macro_use] extern crate diesel;
50/// # include!("../doctest_setup.rs");
51/// # use schema::{posts, users};
52/// #
53/// # #[derive(Identifiable, Queryable, PartialEq, Debug)]
54/// # pub struct User {
55/// # id: i32,
56/// # name: String,
57/// # }
58/// #
59/// # #[derive(Debug, PartialEq)]
60/// # #[derive(Identifiable, Queryable, Associations)]
61/// # #[belongs_to(User)]
62/// # pub struct Post {
63/// # id: i32,
64/// # user_id: i32,
65/// # title: String,
66/// # }
67/// #
68/// # fn main() {
69/// # run_test();
70/// # }
71/// #
72/// # fn run_test() -> QueryResult<()> {
73/// # let connection = establish_connection();
74/// let users = users::table.load::<User>(&connection)?;
75/// let posts = Post::belonging_to(&users)
76/// .load::<Post>(&connection)?
77/// .grouped_by(&users);
78/// let data = users.into_iter().zip(posts).collect::<Vec<_>>();
79///
80/// let expected_data = vec![
81/// (
82/// User { id: 1, name: "Sean".into() },
83/// vec![
84/// Post { id: 1, user_id: 1, title: "My first post".into() },
85/// Post { id: 2, user_id: 1, title: "About Rust".into() },
86/// ],
87/// ),
88/// (
89/// User { id: 2, name: "Tess".into() },
90/// vec![
91/// Post { id: 3, user_id: 2, title: "My first post too".into() },
92/// ],
93/// ),
94/// ];
95///
96/// assert_eq!(expected_data, data);
97/// # Ok(())
98/// # }
99/// ```
100///
101/// See [the module documentation] for more examples
102///
103/// [the module documentation]: index.html
104pub trait GroupedBy<'a, Parent>: IntoIterator + Sized {
105 /// See the trait documentation.
106 fn grouped_by(self, parents: &'a [Parent]) -> Vec<Vec<Self::Item>>;
107}
108
109type Id<T> = <T as Identifiable>::Id;
110
111impl<'a, Parent: 'a, Child, Iter> GroupedBy<'a, Parent> for Iter
112where
113 Iter: IntoIterator<Item = Child>,
114 Child: BelongsTo<Parent>,
115 &'a Parent: Identifiable,
116 Id<&'a Parent>: Borrow<Child::ForeignKey>,
117{
118 fn grouped_by(self, parents: &'a [Parent]) -> Vec<Vec<Child>> {
119 use std::collections::HashMap;
120
121 let id_indices: HashMap<_, _> = parents
122 .iter()
123 .enumerate()
124 .map(|(i, u)| (u.id(), i))
125 .collect();
126 let mut result = parents.iter().map(|_| Vec::new()).collect::<Vec<_>>();
127 for child in self {
128 if let Some(index) = child.foreign_key().map(|i| id_indices[i]) {
129 result[index].push(child);
130 }
131 }
132 result
133 }
134}
135
136impl<'a, Parent, Child> BelongingToDsl<&'a Parent> for Child
137where
138 &'a Parent: Identifiable,
139 Child: HasTable + BelongsTo<Parent>,
140 Id<&'a Parent>: AsExpression<<Child::ForeignKeyColumn as Expression>::SqlType>,
141 Child::Table: FilterDsl<Eq<Child::ForeignKeyColumn, Id<&'a Parent>>>,
142 Child::ForeignKeyColumn: ExpressionMethods,
143{
144 type Output = FindBy<Child::Table, Child::ForeignKeyColumn, Id<&'a Parent>>;
145
146 fn belonging_to(parent: &'a Parent) -> Self::Output {
147 FilterDsl::filter(Child::table(), Child::foreign_key_column().eq(parent.id()))
148 }
149}
150
151impl<'a, Parent, Child> BelongingToDsl<&'a [Parent]> for Child
152where
153 &'a Parent: Identifiable,
154 Child: HasTable + BelongsTo<Parent>,
155 Vec<Id<&'a Parent>>: AsInExpression<<Child::ForeignKeyColumn as Expression>::SqlType>,
156 <Child as HasTable>::Table: FilterDsl<EqAny<Child::ForeignKeyColumn, Vec<Id<&'a Parent>>>>,
157 Child::ForeignKeyColumn: ExpressionMethods,
158{
159 type Output = Filter<Child::Table, EqAny<Child::ForeignKeyColumn, Vec<Id<&'a Parent>>>>;
160
161 fn belonging_to(parents: &'a [Parent]) -> Self::Output {
162 let ids = parents.iter().map(Identifiable::id).collect::<Vec<_>>();
163 FilterDsl::filter(Child::table(), Child::foreign_key_column().eq_any(ids))
164 }
165}
166
167impl<'a, Parent, Child> BelongingToDsl<&'a Vec<Parent>> for Child
168where
169 Child: BelongingToDsl<&'a [Parent]>,
170{
171 type Output = Child::Output;
172
173 fn belonging_to(parents: &'a Vec<Parent>) -> Self::Output {
174 Self::belonging_to(&**parents)
175 }
176}