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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use core::marker::PhantomData;

use crate::arguments::Arguments;
use crate::database::Database;
use crate::encode::Encode;
use crate::executor::Execute;
use crate::types::Type;

/// Raw SQL query with bind parameters, mapped to a concrete type
/// using [`FromRow`](trait.FromRow.html). Returned
/// by [`query_as`](fn.query_as.html).
#[must_use = "query must be executed to affect database"]
pub struct QueryAs<'q, DB, O>
where
    DB: Database,
{
    query: &'q str,
    arguments: <DB as Database>::Arguments,
    database: PhantomData<DB>,
    output: PhantomData<O>,
}

impl<'q, DB, O> QueryAs<'q, DB, O>
where
    DB: Database,
{
    /// Bind a value for use with this SQL query.
    #[inline]
    pub fn bind<T>(mut self, value: T) -> Self
    where
        T: Type<DB>,
        T: Encode<DB>,
    {
        self.arguments.add(value);
        self
    }
}

impl<'q, DB, O: Send> Execute<'q, DB> for QueryAs<'q, DB, O>
where
    DB: Database,
{
    #[inline]
    fn into_parts(self) -> (&'q str, Option<<DB as Database>::Arguments>) {
        (self.query, Some(self.arguments))
    }

    #[inline]
    #[doc(hidden)]
    fn query_string(&self) -> &'q str {
        self.query
    }
}

/// Construct a raw SQL query that is mapped to a concrete type
/// using [`FromRow`](crate::row::FromRow).
///
/// Returns [`QueryAs`].
pub fn query_as<DB, O>(sql: &str) -> QueryAs<DB, O>
where
    DB: Database,
{
    QueryAs {
        query: sql,
        arguments: Default::default(),
        database: PhantomData,
        output: PhantomData,
    }
}

// We need database-specific QueryAs traits to work around:
//  https://github.com/rust-lang/rust/issues/62529

// If for some reason we miss that issue being resolved in a _stable_ edition of
// rust, please open up a 100 issues and shout as loud as you can to remove
// this unseemly hack.

#[allow(unused_macros)]
macro_rules! make_query_as {
    ($name:ident, $db:ident, $row:ident) => {
        pub trait $name<'q, O> {
            fn fetch<'e, E>(
                self,
                executor: E,
            ) -> futures_core::stream::BoxStream<'e, crate::Result<O>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + Unpin + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e;

            fn fetch_all<'e, E>(
                self,
                executor: E,
            ) -> futures_core::future::BoxFuture<'e, crate::Result<Vec<O>>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e;

            fn fetch_one<'e, E>(
                self,
                executor: E,
            ) -> futures_core::future::BoxFuture<'e, crate::Result<O>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e;

            fn fetch_optional<'e, E>(
                self,
                executor: E,
            ) -> futures_core::future::BoxFuture<'e, crate::Result<Option<O>>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e;
        }

        impl<'q, O> $name<'q, O> for crate::query_as::QueryAs<'q, $db, O> {
            fn fetch<'e, E>(
                self,
                executor: E,
            ) -> futures_core::stream::BoxStream<'e, crate::Result<O>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + Unpin + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e,
            {
                use crate::cursor::Cursor;

                Box::pin(async_stream::try_stream! {
                    let mut cursor = executor.fetch_by_ref(self);

                    while let Some(row) = cursor.next().await? {
                        let obj = O::from_row(&row)?;

                        yield obj;
                    }
                })
            }

            fn fetch_optional<'e, E>(
                self,
                executor: E,
            ) -> futures_core::future::BoxFuture<'e, crate::Result<Option<O>>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e,
            {
                use crate::cursor::Cursor;

                Box::pin(async move {
                    let mut cursor = executor.fetch_by_ref(self);
                    let row = cursor.next().await?;

                    row.as_ref().map(O::from_row).transpose()
                })
            }

            fn fetch_one<'e, E>(
                self,
                executor: E,
            ) -> futures_core::future::BoxFuture<'e, crate::Result<O>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e,
            {
                use futures_util::TryFutureExt;

                Box::pin(self.fetch_optional(executor).and_then(|row| match row {
                    Some(row) => futures_util::future::ready(Ok(row)),
                    None => futures_util::future::ready(Err(crate::Error::RowNotFound)),
                }))
            }

            fn fetch_all<'e, E>(
                self,
                executor: E,
            ) -> futures_core::future::BoxFuture<'e, crate::Result<Vec<O>>>
            where
                E: 'e + Send + crate::executor::RefExecutor<'e, Database = $db>,
                O: 'e + Send + for<'c> crate::row::FromRow<'c, $row<'c>>,
                'q: 'e,
            {
                use crate::cursor::Cursor;

                Box::pin(async move {
                    let mut cursor = executor.fetch_by_ref(self);
                    let mut out = Vec::new();

                    while let Some(row) = cursor.next().await? {
                        let obj = O::from_row(&row)?;

                        out.push(obj);
                    }

                    Ok(out)
                })
            }
        }
    };
}