1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::associations::HasTable;
#[cfg(any(feature = "sqlite", feature = "mysql"))]
use crate::associations::Identifiable;
use crate::connection::Connection;
#[cfg(any(feature = "sqlite", feature = "mysql"))]
use crate::dsl::Find;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::dsl::Update;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::expression::{is_aggregate, MixedAggregates, ValidGrouping};
use crate::query_builder::{AsChangeset, IntoUpdateTarget};
#[cfg(any(feature = "sqlite", feature = "mysql"))]
use crate::query_dsl::methods::{ExecuteDsl, FindDsl};
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::query_dsl::{LoadQuery, RunQueryDsl};
use crate::result::QueryResult;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::Table;
pub trait UpdateAndFetchResults<Changes, Output>: Connection {
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output>;
}
#[cfg(feature = "postgres")]
use crate::pg::PgConnection;
#[cfg(feature = "postgres")]
impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for PgConnection
where
Changes: Copy + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Update<Changes, Changes>: LoadQuery<'b, PgConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
crate::update(changeset).set(changeset).get_result(self)
}
}
#[cfg(feature = "sqlite")]
use crate::sqlite::SqliteConnection;
#[cfg(feature = "sqlite")]
impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for SqliteConnection
where
Changes: Copy + Identifiable,
Changes: AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Changes::Table: FindDsl<Changes::Id>,
Update<Changes, Changes>: ExecuteDsl<SqliteConnection>,
Find<Changes::Table, Changes::Id>: LoadQuery<'b, SqliteConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
crate::update(changeset).set(changeset).execute(self)?;
Changes::table().find(changeset.id()).get_result(self)
}
}
#[cfg(feature = "mysql")]
use crate::mysql::MysqlConnection;
#[cfg(feature = "mysql")]
impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for MysqlConnection
where
Changes: Copy + Identifiable,
Changes: AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Changes::Table: FindDsl<Changes::Id>,
Update<Changes, Changes>: ExecuteDsl<MysqlConnection>,
Find<Changes::Table, Changes::Id>: LoadQuery<'b, MysqlConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
crate::update(changeset).set(changeset).execute(self)?;
Changes::table().find(changeset.id()).get_result(self)
}
}
pub trait SaveChangesDsl<Conn> {
fn save_changes<T>(self, connection: &mut Conn) -> QueryResult<T>
where
Self: Sized,
Conn: UpdateAndFetchResults<Self, T>,
{
connection.update_and_fetch(self)
}
}
impl<T, Conn> SaveChangesDsl<Conn> for T where
T: Copy + AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget
{
}