1use std::future::Future;
4
5use futures::TryStreamExt;
6use sqlx::Row as _;
7use sqlx::any::AnyQueryResult;
8use sqlx::{
9 Arguments, ColumnIndex, Database, Decode, Encode, Error, Executor, IntoArguments, SqlStr, Type,
10};
11
12use super::engine::{Claimed, Events, IdAt, undecodable};
13use super::form::advisory::events::Candidates;
14use super::queue::Queue;
15
16#[cfg(feature = "any")]
17mod any;
18pub(crate) mod built_in;
19mod insert;
20pub(crate) mod notify;
21
22#[cfg(feature = "any")]
23pub use any::AnyDialect;
24pub use built_in::{BuiltIn, BuiltInDialect};
25pub use insert::{InsertSql, OnConnection, no_insert};
26pub use notify::Notifies;
27
28pub trait QueueDatabase: Database {
81 #[doc(hidden)]
83 fn bind_str(arguments: &mut Self::Arguments, value: &str) -> Result<(), Error>;
84
85 #[doc(hidden)]
87 fn bind_i64(arguments: &mut Self::Arguments, value: i64) -> Result<(), Error>;
88
89 #[doc(hidden)]
91 fn execute<'c>(
92 conn: &'c mut Self::Connection,
93 sql: &'static str,
94 arguments: Self::Arguments,
95 ) -> impl Future<Output = Result<u64, Error>> + Send + 'c;
96
97 #[doc(hidden)]
100 fn execute_text<'c>(
101 conn: &'c mut Self::Connection,
102 sql: &'static str,
103 ) -> impl Future<Output = Result<(), Error>> + Send + 'c;
104
105 #[doc(hidden)]
109 fn fetch_rows<'c, Row>(
110 conn: &'c mut Self::Connection,
111 sql: &'static str,
112 arguments: Self::Arguments,
113 queue: &'static Queue,
114 out: &'c mut Vec<Claimed<Row>>,
115 ) -> impl Future<Output = Result<(), Error>> + Send + 'c
116 where
117 Row: Events<Self>,
118 Row::Id: for<'r> Decode<'r, Self> + Type<Self>;
119
120 #[doc(hidden)]
122 fn column<T>(row: &Self::Row, name: &str) -> Result<T, Error>
123 where
124 T: for<'r> Decode<'r, Self> + Type<Self>;
125
126 #[doc(hidden)]
128 fn fetch_ids<'c, Id>(
129 conn: &'c mut Self::Connection,
130 sql: &'static str,
131 arguments: Self::Arguments,
132 out: &'c mut Vec<Id>,
133 ) -> impl Future<Output = Result<(), Error>> + Send + 'c
134 where
135 Id: for<'r> Decode<'r, Self> + Type<Self> + Send + Unpin + 'c;
136
137 #[doc(hidden)]
139 fn prepare<'c>(
140 conn: &'c mut Self::Connection,
141 sql: &'static str,
142 ) -> impl Future<Output = Result<(), Error>> + Send + 'c;
143
144 #[doc(hidden)]
147 fn fetch_text<'c>(
148 conn: &'c mut Self::Connection,
149 sql: &'static str,
150 ) -> impl Future<Output = Result<String, Error>> + Send + 'c;
151
152 #[doc(hidden)]
156 fn fetch_flag<'c>(
157 conn: &'c mut Self::Connection,
158 sql: &'static str,
159 arguments: Self::Arguments,
160 ) -> impl Future<Output = Result<bool, Error>> + Send + 'c;
161
162 #[doc(hidden)]
165 fn fetch_candidates<'c, Id>(
166 conn: &'c mut Self::Connection,
167 sql: &'static str,
168 arguments: Self::Arguments,
169 out: &'c mut Candidates<Id>,
170 ) -> impl Future<Output = Result<(), Error>> + Send + 'c
171 where
172 Id: for<'r> Decode<'r, Self> + Type<Self> + Send + 'c;
173
174 #[doc(hidden)]
177 fn fetch_found<'c>(
178 conn: &'c mut Self::Connection,
179 sql: &'static str,
180 arguments: Self::Arguments,
181 ) -> impl Future<Output = Result<bool, Error>> + Send + 'c;
182}
183
184impl<DB> QueueDatabase for DB
185where
186 DB: Database,
187 for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
188 DB::Arguments: IntoArguments<DB>,
189 for<'q> &'q str: Encode<'q, DB> + Type<DB>,
190 for<'r> &'r str: Decode<'r, DB> + ColumnIndex<DB::Row>,
191 for<'a> i64: Encode<'a, DB> + Decode<'a, DB> + Type<DB>,
192 for<'r> String: Decode<'r, DB> + Type<DB>,
193 usize: ColumnIndex<DB::Row>,
194 DB::QueryResult: Into<AnyQueryResult>,
195{
196 fn bind_str(arguments: &mut Self::Arguments, value: &str) -> Result<(), Error> {
197 arguments.add(value).map_err(Error::Encode)
198 }
199
200 fn bind_i64(arguments: &mut Self::Arguments, value: i64) -> Result<(), Error> {
201 arguments.add(value).map_err(Error::Encode)
202 }
203
204 async fn execute(
205 conn: &mut Self::Connection,
206 sql: &'static str,
207 arguments: Self::Arguments,
208 ) -> Result<u64, Error> {
209 let result: AnyQueryResult = sqlx::query_with(sql, arguments).execute(conn).await?.into();
210 Ok(result.rows_affected())
211 }
212
213 async fn execute_text(conn: &mut Self::Connection, sql: &'static str) -> Result<(), Error> {
214 conn.execute(sql).await?;
216 Ok(())
217 }
218
219 async fn fetch_rows<'c, Row>(
220 conn: &'c mut Self::Connection,
221 sql: &'static str,
222 arguments: Self::Arguments,
223 queue: &'static Queue,
224 out: &'c mut Vec<Claimed<Row>>,
225 ) -> Result<(), Error>
226 where
227 Row: Events<Self>,
228 Row::Id: for<'r> Decode<'r, Self> + Type<Self>,
229 {
230 let mut rows = sqlx::query_with::<Self, _>(sql, arguments).fetch(conn);
232 while let Some(raw) = rows.try_next().await? {
233 match Row::from_row(&raw) {
234 Ok(row) => out.push(Claimed::Row(row)),
235 Err(error) => {
236 let id = match queue.id_at {
240 IdAt::First => raw.try_get::<Row::Id, _>(0_usize),
241 IdAt::Named(name) => raw.try_get::<Row::Id, _>(name),
242 };
243 match id {
244 Ok(id) => out.push(Claimed::Undecodable {
245 id,
246 attempt: Row::read_attempt(&raw, queue),
247 error: Box::new(undecodable(error)),
248 }),
249 Err(_) => return Err(error),
250 }
251 }
252 }
253 }
254 Ok(())
255 }
256
257 fn column<T>(row: &Self::Row, name: &str) -> Result<T, Error>
258 where
259 T: for<'r> Decode<'r, Self> + Type<Self>,
260 {
261 row.try_get::<T, _>(name)
262 }
263
264 async fn fetch_ids<'c, Id>(
265 conn: &'c mut Self::Connection,
266 sql: &'static str,
267 arguments: Self::Arguments,
268 out: &'c mut Vec<Id>,
269 ) -> Result<(), Error>
270 where
271 Id: for<'r> Decode<'r, Self> + Type<Self> + Send + Unpin + 'c,
272 {
273 let mut rows = sqlx::query_with::<Self, _>(sql, arguments).fetch(conn);
277 while let Some(row) = rows.try_next().await? {
278 out.push(row.try_get::<Id, _>(0_usize)?);
279 }
280 Ok(())
281 }
282
283 async fn prepare(conn: &mut Self::Connection, sql: &'static str) -> Result<(), Error> {
284 conn.prepare(SqlStr::from_static(sql)).await?;
285 Ok(())
286 }
287
288 fn fetch_text<'c>(
289 conn: &'c mut Self::Connection,
290 sql: &'static str,
291 ) -> impl Future<Output = Result<String, Error>> + Send + 'c {
292 sqlx::query_scalar::<Self, String>(sql).fetch_one(conn)
293 }
294
295 async fn fetch_flag(
296 conn: &mut Self::Connection,
297 sql: &'static str,
298 arguments: Self::Arguments,
299 ) -> Result<bool, Error> {
300 let flag: i64 = sqlx::query_scalar_with::<Self, i64, _>(sql, arguments)
301 .fetch_one(conn)
302 .await?;
303 Ok(flag != 0)
304 }
305
306 async fn fetch_candidates<'c, Id>(
307 conn: &'c mut Self::Connection,
308 sql: &'static str,
309 arguments: Self::Arguments,
310 out: &'c mut Candidates<Id>,
311 ) -> Result<(), Error>
312 where
313 Id: for<'r> Decode<'r, Self> + Type<Self> + Send + 'c,
314 {
315 let mut rows = sqlx::query_with::<Self, _>(sql, arguments).fetch(conn);
316 while let Some(row) = rows.try_next().await? {
317 let key = row.try_get::<&str, _>(1_usize)?;
319 out.push(row.try_get::<Id, _>(0_usize)?, key);
320 }
321 Ok(())
322 }
323
324 async fn fetch_found(
325 conn: &mut Self::Connection,
326 sql: &'static str,
327 arguments: Self::Arguments,
328 ) -> Result<bool, Error> {
329 let mut rows = sqlx::query_with::<Self, _>(sql, arguments).fetch(conn);
331 let mut found = false;
332 while rows.try_next().await?.is_some() {
333 found = true;
334 }
335 Ok(found)
336 }
337}