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
//! Increments and decrements through a cast: value objects (money,
//! counters) that know how to add up.
use crate::column::{Assignment, Column};
use crate::error::Result;
use crate::exec::{Access, Executor};
use crate::model::Model;
use crate::persist;
use crate::record::Record;
impl<M: Model> Record<M> {
/// Adds `amount` to cast field `column`, atomically: the record's field
/// becomes the cast's [`increment`](crate::cast::Cast::increment) of
/// it, and the database runs `SET column = column + <amount's stored
/// form>` (plus the update timestamp), so concurrent increments add
/// up. The stored form must be a number (money stored as cents, a
/// counter stored as an integer).
///
/// Returns `false` when a hook cancelled it; a record that is not
/// stored is [`OrmError::NotPersisted`](crate::OrmError::NotPersisted),
/// never a table-wide increment.
///
/// ```no_run
/// # use navree_orm::__doc_models::{Record, Result, Uow};
/// use navree_orm::Model;
/// use navree_orm::cast::{Cast, CastError};
///
/// /// An amount in cents that adds up through its cast.
/// #[derive(Clone, Copy, Debug, Default, PartialEq, serde::Serialize)]
/// pub struct Cents(pub i64);
///
/// pub struct AsCents;
///
/// impl Cast<Cents> for AsCents {
/// type Stored = i64;
///
/// fn get(stored: i64) -> Result<Cents, CastError> {
/// Ok(Cents(stored))
/// }
///
/// fn set(value: &Cents) -> Result<i64, CastError> {
/// Ok(value.0)
/// }
///
/// fn increment(value: &Cents, amount: &Cents) -> Result<Cents, CastError> {
/// value.0.checked_add(amount.0).map(Cents).ok_or_else(|| CastError::new("the balance overflows"))
/// }
/// }
///
/// #[derive(Model, Clone, Debug, Default, serde::Serialize)]
/// #[model(table = "accounts")]
/// pub struct Account {
/// pub id: i64,
/// #[model(cast = AsCents)]
/// pub balance: Cents,
/// }
///
/// # async fn demo(uow: Uow, mut account: Record<Account>) -> navree_orm::Result<()> {
/// account.increment_cast(&uow, Account::BALANCE, Cents(500)).await?;
/// # Ok(()) }
/// ```
pub async fn increment_cast<E, T>(
&mut self,
exec: &E,
column: Column<M, T>,
amount: T,
) -> Result<bool>
where
E: Executor + ?Sized,
T: Send + Sync,
{
self.increment_cast_with(exec, column, amount, std::iter::empty())
.await
}
/// Like [`increment_cast`](Record::increment_cast), also setting
/// `extra` columns.
pub async fn increment_cast_with<E, T>(
&mut self,
exec: &E,
column: Column<M, T>,
amount: T,
extra: impl IntoIterator<Item = Assignment>,
) -> Result<bool>
where
E: Executor + ?Sized,
T: Send + Sync,
{
let extra: Vec<Assignment> = extra.into_iter().collect();
let target = exec
.target_routed(Access::Write, crate::query::model_route::<M>())
.await?;
persist::increment_cast(&target, self, &column, &amount, false, extra).await
}
/// Subtracts `amount` from cast field `column` (the cast's
/// [`decrement`](crate::cast::Cast::decrement)), atomically.
pub async fn decrement_cast<E, T>(
&mut self,
exec: &E,
column: Column<M, T>,
amount: T,
) -> Result<bool>
where
E: Executor + ?Sized,
T: Send + Sync,
{
self.decrement_cast_with(exec, column, amount, std::iter::empty())
.await
}
/// Like [`decrement_cast`](Record::decrement_cast), also setting
/// `extra` columns.
pub async fn decrement_cast_with<E, T>(
&mut self,
exec: &E,
column: Column<M, T>,
amount: T,
extra: impl IntoIterator<Item = Assignment>,
) -> Result<bool>
where
E: Executor + ?Sized,
T: Send + Sync,
{
let extra: Vec<Assignment> = extra.into_iter().collect();
let target = exec
.target_routed(Access::Write, crate::query::model_route::<M>())
.await?;
persist::increment_cast(&target, self, &column, &amount, true, extra).await
}
}