ouisync/db/
connection.rs

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
use either::Either;
use futures_util::{future::BoxFuture, stream::BoxStream};
use ref_cast::RefCast;
use sqlx::{
    sqlite::{SqliteConnection, SqliteQueryResult, SqliteRow, SqliteStatement, SqliteTypeInfo},
    Describe, Error, Execute, Executor, Sqlite,
};

/// Database connection
#[derive(Debug, RefCast)]
#[repr(transparent)]
pub struct Connection(pub(super) SqliteConnection);

impl<'t> Executor<'t> for &'t mut Connection {
    type Database = Sqlite;

    fn fetch_many<'e, 'q: 'e, E>(
        self,
        query: E,
    ) -> BoxStream<'e, Result<Either<SqliteQueryResult, SqliteRow>, Error>>
    where
        't: 'e,
        E: Execute<'q, Sqlite> + 'q,
    {
        self.0.fetch_many(query)
    }

    fn fetch_optional<'e, 'q: 'e, E>(
        self,
        query: E,
    ) -> BoxFuture<'e, Result<Option<SqliteRow>, Error>>
    where
        't: 'e,
        E: Execute<'q, Sqlite> + 'q,
    {
        self.0.fetch_optional(query)
    }

    fn prepare_with<'e, 'q: 'e>(
        self,
        sql: &'q str,
        parameters: &'e [SqliteTypeInfo],
    ) -> BoxFuture<'e, Result<SqliteStatement<'q>, Error>>
    where
        't: 'e,
    {
        self.0.prepare_with(sql, parameters)
    }

    #[doc(hidden)]
    fn describe<'e, 'q: 'e>(self, query: &'q str) -> BoxFuture<'e, Result<Describe<Sqlite>, Error>>
    where
        't: 'e,
    {
        self.0.describe(query)
    }
}