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
#![allow(missing_docs)]

use std::{pin::Pin, task::Poll};

#[cfg(feature = "mock")]
use std::sync::Arc;

use futures::Stream;
#[cfg(feature = "sqlx-dep")]
use futures::TryStreamExt;

#[cfg(feature = "sqlx-dep")]
use sqlx::{pool::PoolConnection, Executor};

use crate::{DbErr, InnerConnection, QueryResult, Statement};

/// Creates a stream from a [QueryResult]
#[ouroboros::self_referencing]
pub struct QueryStream {
    stmt: Statement,
    conn: InnerConnection,
    #[borrows(mut conn, stmt)]
    #[not_covariant]
    stream: Pin<Box<dyn Stream<Item = Result<QueryResult, DbErr>> + 'this>>,
}

#[cfg(feature = "sqlx-mysql")]
impl From<(PoolConnection<sqlx::MySql>, Statement)> for QueryStream {
    fn from((conn, stmt): (PoolConnection<sqlx::MySql>, Statement)) -> Self {
        QueryStream::build(stmt, InnerConnection::MySql(conn))
    }
}

#[cfg(feature = "sqlx-postgres")]
impl From<(PoolConnection<sqlx::Postgres>, Statement)> for QueryStream {
    fn from((conn, stmt): (PoolConnection<sqlx::Postgres>, Statement)) -> Self {
        QueryStream::build(stmt, InnerConnection::Postgres(conn))
    }
}

#[cfg(feature = "sqlx-sqlite")]
impl From<(PoolConnection<sqlx::Sqlite>, Statement)> for QueryStream {
    fn from((conn, stmt): (PoolConnection<sqlx::Sqlite>, Statement)) -> Self {
        QueryStream::build(stmt, InnerConnection::Sqlite(conn))
    }
}

#[cfg(feature = "mock")]
impl From<(Arc<crate::MockDatabaseConnection>, Statement)> for QueryStream {
    fn from((conn, stmt): (Arc<crate::MockDatabaseConnection>, Statement)) -> Self {
        QueryStream::build(stmt, InnerConnection::Mock(conn))
    }
}

impl std::fmt::Debug for QueryStream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "QueryStream")
    }
}

impl QueryStream {
    fn build(stmt: Statement, conn: InnerConnection) -> QueryStream {
        QueryStreamBuilder {
            stmt,
            conn,
            stream_builder: |conn, stmt| match conn {
                #[cfg(feature = "sqlx-mysql")]
                InnerConnection::MySql(c) => {
                    let query = crate::driver::sqlx_mysql::sqlx_query(stmt);
                    Box::pin(
                        c.fetch(query)
                            .map_ok(Into::into)
                            .map_err(crate::sqlx_error_to_query_err),
                    )
                }
                #[cfg(feature = "sqlx-postgres")]
                InnerConnection::Postgres(c) => {
                    let query = crate::driver::sqlx_postgres::sqlx_query(stmt);
                    Box::pin(
                        c.fetch(query)
                            .map_ok(Into::into)
                            .map_err(crate::sqlx_error_to_query_err),
                    )
                }
                #[cfg(feature = "sqlx-sqlite")]
                InnerConnection::Sqlite(c) => {
                    let query = crate::driver::sqlx_sqlite::sqlx_query(stmt);
                    Box::pin(
                        c.fetch(query)
                            .map_ok(Into::into)
                            .map_err(crate::sqlx_error_to_query_err),
                    )
                }
                #[cfg(feature = "mock")]
                InnerConnection::Mock(c) => c.fetch(stmt),
            },
        }
        .build()
    }
}

impl Stream for QueryStream {
    type Item = Result<QueryResult, DbErr>;

    fn poll_next(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        this.with_stream_mut(|stream| stream.as_mut().poll_next(cx))
    }
}