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
73
74
75
76
77
use connection::Connection;
use helper_types::Limit;
use query_builder::{QueryFragment, AsQuery, QueryId};
use query_source::Queryable;
use result::QueryResult;
use super::LimitDsl;
use types::HasSqlType;

/// Methods to execute a query given a connection. These are automatically implemented for the
/// various query types.
pub trait LoadDsl<Conn: Connection>: AsQuery + Sized where
    Conn::Backend: HasSqlType<Self::SqlType>,
{
    /// Executes the given query, returning an `Iterator` over the returned
    /// rows.
    fn load<'a, U>(self, conn: &Conn) -> QueryResult<Vec<U>> where
        U: Queryable<Self::SqlType, Conn::Backend> + 'a;

    /// Attempts to load a single record. Returns `Ok(record)` if found, and
    /// `Err(NotFound)` if no results are returned. If the query truly is
    /// optional, you can call `.optional()` on the result of this to get a
    /// `Result<Option<U>>`.
    fn first<U>(self, conn: &Conn) -> QueryResult<U> where
        Self: LimitDsl,
        Limit<Self>: LoadDsl<Conn, SqlType=Self::SqlType>,
        U: Queryable<Self::SqlType, Conn::Backend>,
    {
        self.limit(1).get_result(conn)
    }

    /// Runs the command, and returns the affected row. `Err(NotFound)` will be
    /// returned if the query affected 0 rows. You can call `.optional()` on the
    /// result of this if the command was optional to get back a
    /// `Result<Option<U>>`
    fn get_result<U>(self, conn: &Conn) -> QueryResult<U> where
        U: Queryable<Self::SqlType, Conn::Backend>;

    /// Runs the command, returning an `Iterator` over the affected rows.
    fn get_results<'a, U>(self, conn: &Conn) -> QueryResult<Vec<U>> where
        U: Queryable<Self::SqlType, Conn::Backend> + 'a,
    {
        self.load(conn)
    }
}

impl<Conn: Connection, T: AsQuery> LoadDsl<Conn> for T where
    T::Query: QueryFragment<Conn::Backend> + QueryId,
    Conn::Backend: HasSqlType<T::SqlType>,
{
    fn load<'a, U>(self, conn: &Conn) -> QueryResult<Vec<U>> where
        U: Queryable<Self::SqlType, Conn::Backend> + 'a,
    {
        conn.query_all(self)
    }

    fn get_result<U>(self, conn: &Conn) -> QueryResult<U> where
        U: Queryable<Self::SqlType, Conn::Backend>,
    {
        conn.query_one(self)
    }
}

pub trait ExecuteDsl<Conn: Connection>: Sized + QueryFragment<Conn::Backend> + QueryId {
    /// Executes the given command, returning the number of rows affected. Used
    /// in conjunction with
    /// [`update`](../query_builder/fn.update.html) and
    /// [`delete`](../query_builder/fn.delete.html)
    fn execute(&self, conn: &Conn) -> QueryResult<usize> {
        conn.execute_returning_count(self)
    }
}

impl<Conn, T> ExecuteDsl<Conn> for T where
    Conn: Connection,
    T: QueryFragment<Conn::Backend> + QueryId,
{
}