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
use crate::database::Database;
use crate::describe::Describe;
use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
use futures_util::TryStreamExt;

/// Encapsulates query execution on the database.
///
/// Implemented primarily by [crate::Pool].
pub trait Executor {
    type Database: Database + ?Sized;

    /// Send a raw SQL command to the database.
    ///
    /// This is intended for queries that cannot or should not be prepared (ex. `BEGIN`).
    ///
    /// Does not support fetching results.
    fn send<'e, 'q: 'e>(&'e mut self, command: &'q str) -> BoxFuture<'e, crate::Result<()>>;

    /// Execute the query, returning the number of rows affected.
    fn execute<'e, 'q: 'e>(
        &'e mut self,
        query: &'q str,
        args: <Self::Database as Database>::Arguments,
    ) -> BoxFuture<'e, crate::Result<u64>>;

    /// Executes the query and returns a [Stream] of [Row].
    fn fetch<'e, 'q: 'e>(
        &'e mut self,
        query: &'q str,
        args: <Self::Database as Database>::Arguments,
    ) -> BoxStream<'e, crate::Result<<Self::Database as Database>::Row>>;

    /// Executes the query and returns up to resulting record.
    ///
    /// * [crate::Error::FoundMoreThanOne] will be returned if the query produced more than 1 row.
    fn fetch_optional<'e, 'q: 'e>(
        &'e mut self,
        query: &'q str,
        args: <Self::Database as Database>::Arguments,
    ) -> BoxFuture<'e, crate::Result<Option<<Self::Database as Database>::Row>>> {
        let mut s = self.fetch(query, args);
        Box::pin(async move {
            match s.try_next().await? {
                Some(val) => {
                    if s.try_next().await?.is_some() {
                        Err(crate::Error::FoundMoreThanOne)
                    } else {
                        Ok(Some(val))
                    }
                },
                None => Ok(None)
            }
        })
    }

    /// Execute the query and return at most one resulting record.
    fn fetch_one<'e, 'q: 'e>(
        &'e mut self,
        query: &'q str,
        args: <Self::Database as Database>::Arguments,
    ) -> BoxFuture<'e, crate::Result<<Self::Database as Database>::Row>> {
        let mut s = self.fetch(query, args);
        Box::pin(async move { s.try_next().await?.ok_or(crate::Error::NotFound) })
    }

    /// Analyze the SQL query and report the inferred bind parameter types and returned columns.
    fn describe<'e, 'q: 'e>(
        &'e mut self,
        query: &'q str,
    ) -> BoxFuture<'e, crate::Result<Describe<Self::Database>>>;
}