ouisync/db/
connection.rs

1use either::Either;
2use futures_util::{future::BoxFuture, stream::BoxStream};
3use ref_cast::RefCast;
4use sqlx::{
5    sqlite::{SqliteConnection, SqliteQueryResult, SqliteRow, SqliteStatement, SqliteTypeInfo},
6    Describe, Error, Execute, Executor, Sqlite,
7};
8
9/// Database connection
10#[derive(Debug, RefCast)]
11#[repr(transparent)]
12pub struct Connection(pub(super) SqliteConnection);
13
14impl<'t> Executor<'t> for &'t mut Connection {
15    type Database = Sqlite;
16
17    fn fetch_many<'e, 'q: 'e, E>(
18        self,
19        query: E,
20    ) -> BoxStream<'e, Result<Either<SqliteQueryResult, SqliteRow>, Error>>
21    where
22        't: 'e,
23        E: Execute<'q, Sqlite> + 'q,
24    {
25        self.0.fetch_many(query)
26    }
27
28    fn fetch_optional<'e, 'q: 'e, E>(
29        self,
30        query: E,
31    ) -> BoxFuture<'e, Result<Option<SqliteRow>, Error>>
32    where
33        't: 'e,
34        E: Execute<'q, Sqlite> + 'q,
35    {
36        self.0.fetch_optional(query)
37    }
38
39    fn prepare_with<'e, 'q: 'e>(
40        self,
41        sql: &'q str,
42        parameters: &'e [SqliteTypeInfo],
43    ) -> BoxFuture<'e, Result<SqliteStatement<'q>, Error>>
44    where
45        't: 'e,
46    {
47        self.0.prepare_with(sql, parameters)
48    }
49
50    #[doc(hidden)]
51    fn describe<'e, 'q: 'e>(self, query: &'q str) -> BoxFuture<'e, Result<Describe<Sqlite>, Error>>
52    where
53        't: 'e,
54    {
55        self.0.describe(query)
56    }
57}