es_entity/operation/
mod.rs1pub mod hooks;
4mod with_time;
5
6use sqlx::{Acquire, Transaction};
7
8use crate::{clock::ClockHandle, db};
9
10pub use with_time::*;
11
12pub struct DbOp<'c> {
21 tx: Transaction<'c, db::Db>,
22 clock: ClockHandle,
23 now: Option<chrono::DateTime<chrono::Utc>>,
24 commit_hooks: Option<hooks::CommitHooks>,
25}
26
27impl<'c> DbOp<'c> {
28 fn new(
29 tx: Transaction<'c, db::Db>,
30 clock: ClockHandle,
31 time: Option<chrono::DateTime<chrono::Utc>>,
32 ) -> Self {
33 Self {
34 tx,
35 clock,
36 now: time,
37 commit_hooks: Some(hooks::CommitHooks::new()),
38 }
39 }
40
41 pub async fn init(pool: &db::Pool) -> Result<DbOp<'static>, sqlx::Error> {
45 Self::init_with_clock(pool, crate::clock::Clock::handle()).await
46 }
47
48 pub async fn init_with_clock(
52 pool: &db::Pool,
53 clock: &ClockHandle,
54 ) -> Result<DbOp<'static>, sqlx::Error> {
55 let tx = pool.begin().await?;
56
57 let time = clock.manual_now();
60
61 Ok(DbOp::new(tx, clock.clone(), time))
62 }
63
64 pub fn with_time(self, time: chrono::DateTime<chrono::Utc>) -> DbOpWithTime<'c> {
66 DbOpWithTime::new(self, time)
67 }
68
69 pub fn with_clock_time(self) -> DbOpWithTime<'c> {
73 let time = self.now.unwrap_or_else(|| self.clock.now());
74 DbOpWithTime::new(self, time)
75 }
76
77 pub async fn with_db_time(mut self) -> Result<DbOpWithTime<'c>, sqlx::Error> {
84 let time = if let Some(time) = self.now {
85 time
86 } else if let Some(manual_time) = self.clock.manual_now() {
87 manual_time
88 } else {
89 db::database_now(&mut *self.tx).await?
90 };
91
92 Ok(DbOpWithTime::new(self, time))
93 }
94
95 pub fn maybe_now(&self) -> Option<chrono::DateTime<chrono::Utc>> {
97 self.now
98 }
99
100 pub async fn begin(&mut self) -> Result<DbOp<'_>, sqlx::Error> {
102 Ok(DbOp::new(
103 self.tx.begin().await?,
104 self.clock.clone(),
105 self.now,
106 ))
107 }
108
109 pub async fn commit(mut self) -> Result<(), sqlx::Error> {
111 let commit_hooks = self.commit_hooks.take().expect("no hooks");
112 let post_hooks = commit_hooks.execute_pre(&mut self).await?;
113 self.tx.commit().await?;
114 post_hooks.execute();
115 Ok(())
116 }
117
118 pub fn tx_mut(&mut self) -> &mut Transaction<'c, db::Db> {
120 &mut self.tx
121 }
122}
123
124impl<'o> AtomicOperation for DbOp<'o> {
125 fn maybe_now(&self) -> Option<chrono::DateTime<chrono::Utc>> {
126 self.maybe_now()
127 }
128
129 fn clock(&self) -> &ClockHandle {
130 &self.clock
131 }
132
133 fn as_executor(&mut self) -> &mut db::Connection {
134 self.tx.as_executor()
135 }
136
137 fn add_commit_hook<H: hooks::CommitHook>(&mut self, hook: H) -> Result<(), H> {
138 self.commit_hooks.as_mut().expect("no hooks").add(hook);
139 Ok(())
140 }
141
142 fn commit_hook<H: hooks::CommitHook>(&self) -> Option<&H> {
143 self.commit_hooks.as_ref()?.get_last::<H>()
144 }
145}
146
147pub struct DbOpWithTime<'c> {
151 inner: DbOp<'c>,
152 now: chrono::DateTime<chrono::Utc>,
153}
154
155impl<'c> DbOpWithTime<'c> {
156 fn new(mut inner: DbOp<'c>, time: chrono::DateTime<chrono::Utc>) -> Self {
157 inner.now = Some(time);
158 Self { inner, now: time }
159 }
160
161 pub fn now(&self) -> chrono::DateTime<chrono::Utc> {
163 self.now
164 }
165
166 pub async fn begin(&mut self) -> Result<DbOpWithTime<'_>, sqlx::Error> {
168 Ok(DbOpWithTime::new(self.inner.begin().await?, self.now))
169 }
170
171 pub async fn commit(self) -> Result<(), sqlx::Error> {
173 self.inner.commit().await
174 }
175
176 pub fn tx_mut(&mut self) -> &mut Transaction<'c, db::Db> {
178 self.inner.tx_mut()
179 }
180}
181
182impl<'o> AtomicOperation for DbOpWithTime<'o> {
183 fn maybe_now(&self) -> Option<chrono::DateTime<chrono::Utc>> {
184 Some(self.now())
185 }
186
187 fn clock(&self) -> &ClockHandle {
188 self.inner.clock()
189 }
190
191 fn as_executor(&mut self) -> &mut db::Connection {
192 self.inner.as_executor()
193 }
194
195 fn add_commit_hook<H: hooks::CommitHook>(&mut self, hook: H) -> Result<(), H> {
196 self.inner.add_commit_hook(hook)
197 }
198
199 fn commit_hook<H: hooks::CommitHook>(&self) -> Option<&H> {
200 self.inner.commit_hook::<H>()
201 }
202}
203
204impl<'o> AtomicOperationWithTime for DbOpWithTime<'o> {
205 fn now(&self) -> chrono::DateTime<chrono::Utc> {
206 self.now
207 }
208}
209
210pub trait AtomicOperation: Send {
218 fn maybe_now(&self) -> Option<chrono::DateTime<chrono::Utc>> {
220 None
221 }
222
223 fn clock(&self) -> &ClockHandle {
227 crate::clock::Clock::handle()
228 }
229
230 fn as_executor(&mut self) -> &mut sqlx::PgConnection;
247
248 fn add_commit_hook<H: hooks::CommitHook>(&mut self, hook: H) -> Result<(), H> {
251 Err(hook)
252 }
253
254 fn commit_hook<H: hooks::CommitHook>(&self) -> Option<&H> {
258 None
259 }
260}
261
262impl<'c> AtomicOperation for sqlx::Transaction<'c, db::Db> {
263 fn as_executor(&mut self) -> &mut db::Connection {
264 &mut *self
265 }
266}