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 crate::backend::{Backend, DieselReserveSpecialization};
use crate::expression::grouped::Grouped;
use crate::expression::operators::Eq;
use crate::expression::AppearsOnTable;
use crate::query_builder::*;
use crate::query_source::{Column, QuerySource};
use crate::result::QueryResult;
use crate::Table;
pub trait AsChangeset {
type Target: QuerySource;
type Changeset;
#[allow(clippy::wrong_self_convention)]
fn as_changeset(self) -> Self::Changeset;
}
#[allow(unreachable_pub)]
#[doc(inline)]
pub use diesel_derives::AsChangeset;
impl<T: AsChangeset> AsChangeset for Option<T> {
type Target = T::Target;
type Changeset = Option<T::Changeset>;
fn as_changeset(self) -> Self::Changeset {
self.map(AsChangeset::as_changeset)
}
}
impl<Left, Right> AsChangeset for Eq<Left, Right>
where
Left: AssignmentTarget,
Right: AppearsOnTable<Left::Table>,
{
type Target = Left::Table;
type Changeset = Assign<<Left as AssignmentTarget>::QueryAstNode, Right>;
fn as_changeset(self) -> Self::Changeset {
Assign {
target: self.left.into_target(),
expr: self.right,
}
}
}
impl<Left, Right> AsChangeset for Grouped<Eq<Left, Right>>
where
Eq<Left, Right>: AsChangeset,
{
type Target = <Eq<Left, Right> as AsChangeset>::Target;
type Changeset = <Eq<Left, Right> as AsChangeset>::Changeset;
fn as_changeset(self) -> Self::Changeset {
self.0.as_changeset()
}
}
#[derive(Debug, Clone, Copy, QueryId)]
pub struct Assign<Target, Expr> {
target: Target,
expr: Expr,
}
impl<T, U, DB> QueryFragment<DB> for Assign<T, U>
where
DB: Backend,
T: QueryFragment<DB>,
U: QueryFragment<DB>,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
QueryFragment::walk_ast(&self.target, out.reborrow())?;
out.push_sql(" = ");
QueryFragment::walk_ast(&self.expr, out.reborrow())
}
}
pub trait AssignmentTarget {
type Table: Table;
type QueryAstNode;
fn into_target(self) -> Self::QueryAstNode;
}
#[derive(Debug, Clone, Copy)]
pub struct ColumnWrapperForUpdate<C>(pub C);
impl<DB, C> QueryFragment<DB> for ColumnWrapperForUpdate<C>
where
DB: Backend + DieselReserveSpecialization,
C: Column,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
out.push_identifier(C::NAME)
}
}
impl<C> AssignmentTarget for C
where
C: Column,
{
type Table = C::Table;
type QueryAstNode = ColumnWrapperForUpdate<C>;
fn into_target(self) -> Self::QueryAstNode {
ColumnWrapperForUpdate(self)
}
}