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
use associations::HasTable;
#[cfg(any(feature = "sqlite", feature = "mysql"))]
use associations::Identifiable;
#[cfg(any(feature = "sqlite", feature = "mysql"))]
use dsl::Find;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use dsl::Update;
use query_builder::{AsChangeset, IntoUpdateTarget};
#[cfg(any(feature = "sqlite", feature = "mysql"))]
use query_dsl::methods::{ExecuteDsl, FindDsl};
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use query_dsl::{LoadQuery, RunQueryDsl};
use result::QueryResult;
pub trait InternalSaveChangesDsl<Conn, T>: Sized {
fn internal_save_changes(self, connection: &Conn) -> QueryResult<T>;
}
#[cfg(feature = "postgres")]
use pg::PgConnection;
#[cfg(feature = "postgres")]
impl<T, U> InternalSaveChangesDsl<PgConnection, U> for T
where
T: Copy + AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget,
Update<T, T>: LoadQuery<PgConnection, U>,
{
fn internal_save_changes(self, conn: &PgConnection) -> QueryResult<U> {
::update(self).set(self).get_result(conn)
}
}
#[cfg(feature = "sqlite")]
use sqlite::SqliteConnection;
#[cfg(feature = "sqlite")]
impl<T, U> InternalSaveChangesDsl<SqliteConnection, U> for T
where
T: Copy + Identifiable,
T: AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget,
T::Table: FindDsl<T::Id>,
Update<T, T>: ExecuteDsl<SqliteConnection>,
Find<T::Table, T::Id>: LoadQuery<SqliteConnection, U>,
{
fn internal_save_changes(self, conn: &SqliteConnection) -> QueryResult<U> {
try!(::update(self).set(self).execute(conn));
T::table().find(self.id()).get_result(conn)
}
}
#[cfg(feature = "mysql")]
use mysql::MysqlConnection;
#[cfg(feature = "mysql")]
impl<T, U> InternalSaveChangesDsl<MysqlConnection, U> for T
where
T: Copy + Identifiable,
T: AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget,
T::Table: FindDsl<T::Id>,
Update<T, T>: ExecuteDsl<MysqlConnection>,
Find<T::Table, T::Id>: LoadQuery<MysqlConnection, U>,
{
fn internal_save_changes(self, conn: &MysqlConnection) -> QueryResult<U> {
try!(::update(self).set(self).execute(conn));
T::table().find(self.id()).get_result(conn)
}
}
pub trait SaveChangesDsl<Conn> {
fn save_changes<T>(self, connection: &Conn) -> QueryResult<T>
where
Self: InternalSaveChangesDsl<Conn, T>,
{
self.internal_save_changes(connection)
}
}
impl<T, Conn> SaveChangesDsl<Conn> for T
where
T: Copy + AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget,
{
}