use self::private::LoadIter; use super::RunQueryDsl; use crate::backend::Backend; use crate::connection::{Connection, DefaultLoadingMode, LoadConnection}; use crate::deserialize::FromSqlRow; use crate::expression::QueryMetadata; use crate::query_builder::{AsQuery, QueryFragment, QueryId}; use crate::result::QueryResult; #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")] pub use self::private::CompatibleType; #[cfg(not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"))] pub(crate) use self::private::CompatibleType; /// The `load` method /// /// This trait should not be relied on directly by most apps. Its behavior is /// provided by [`RunQueryDsl`]. However, you may need a where clause on this trait /// to call `load` from generic code. /// /// [`RunQueryDsl`]: crate::RunQueryDsl pub trait LoadQuery<'query, Conn, U, B = DefaultLoadingMode>: RunQueryDsl { /// Return type of `LoadQuery::internal_load` type RowIter<'conn>: Iterator> where Conn: 'conn; /// Load this query #[diesel_derives::__diesel_public_if( feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" )] fn internal_load(self, conn: &mut Conn) -> QueryResult>; } #[doc(hidden)] #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))] #[deprecated(note = "Use `LoadQuery::Iter` directly")] pub type LoadRet<'conn, 'query, Q, C, U, B = DefaultLoadingMode> = >::RowIter<'conn>; impl<'query, Conn, T, U, DB, B> LoadQuery<'query, Conn, U, B> for T where Conn: Connection + LoadConnection, T: AsQuery + RunQueryDsl, T::Query: QueryFragment + QueryId + 'query, T::SqlType: CompatibleType, DB: Backend + QueryMetadata + 'static, U: FromSqlRow<>::SqlType, DB> + 'static, >::SqlType: 'static, { type RowIter<'conn> = LoadIter< U, >::Cursor<'conn, 'query>, >::SqlType, DB, > where Conn: 'conn; fn internal_load(self, conn: &mut Conn) -> QueryResult> { const { // that's required to force evaluating // this constant let _ = T::Query::IS_WINDOW_FUNCTION; } Ok(LoadIter { cursor: conn.load(self.as_query())?, _marker: Default::default(), }) } } /// The `execute` method /// /// This trait should not be relied on directly by most apps. Its behavior is /// provided by [`RunQueryDsl`]. However, you may need a where clause on this trait /// to call `execute` from generic code. /// /// [`RunQueryDsl`]: crate::RunQueryDsl pub trait ExecuteDsl, DB: Backend = ::Backend>: Sized { /// Execute this command fn execute(query: Self, conn: &mut Conn) -> QueryResult; } use crate::result::Error; impl ExecuteDsl for T where Conn: Connection, DB: Backend, T: QueryFragment + QueryId, { fn execute(query: T, conn: &mut Conn) -> Result { conn.execute_returning_count(&query) } } // These types and traits are not part of the public API. // // * CompatibleType as we consider this as "sealed" trait. It shouldn't // be implemented by a third party // * LoadIter as it's an implementation detail mod private { use crate::backend::Backend; use crate::deserialize::FromSqlRow; use crate::expression::select_by::SelectBy; use crate::expression::{Expression, TypedExpressionType}; use crate::sql_types::{SqlType, Untyped}; use crate::{QueryResult, Selectable}; #[allow(missing_debug_implementations)] pub struct LoadIter { pub(super) cursor: C, pub(super) _marker: std::marker::PhantomData<(ST, U, DB)>, } impl<'a, C, U, ST, DB, R> LoadIter where DB: Backend, C: Iterator>, R: crate::row::Row<'a, DB>, U: FromSqlRow, { pub(super) fn map_row(row: Option>) -> Option> { match row? { Ok(row) => Some( U::build_from_row(&row).map_err(crate::result::Error::DeserializationError), ), Err(e) => Some(Err(e)), } } } impl<'a, C, U, ST, DB, R> Iterator for LoadIter where DB: Backend, C: Iterator>, R: crate::row::Row<'a, DB>, U: FromSqlRow, { type Item = QueryResult; fn next(&mut self) -> Option { Self::map_row(self.cursor.next()) } fn size_hint(&self) -> (usize, Option) { self.cursor.size_hint() } fn count(self) -> usize where Self: Sized, { self.cursor.count() } fn last(self) -> Option where Self: Sized, { Self::map_row(self.cursor.last()) } fn nth(&mut self, n: usize) -> Option { Self::map_row(self.cursor.nth(n)) } } impl<'a, C, U, ST, DB, R> ExactSizeIterator for LoadIter where DB: Backend, C: ExactSizeIterator + Iterator>, R: crate::row::Row<'a, DB>, U: FromSqlRow, { fn len(&self) -> usize { self.cursor.len() } } #[cfg_attr( diesel_docsrs, doc(cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")) )] #[diagnostic::on_unimplemented( note = "this is a mismatch between what your query returns and what your type expects the query to return", note = "the fields in your struct need to match the fields returned by your query in count, order and type", note = "consider using `#[diesel(check_for_backend({DB}))]` on either `#[derive(Selectable)]` or `#[derive(QueryableByName)]` \n\ on your struct `{U}` and in your query `.select({U}::as_select())` to get a better error message" )] pub trait CompatibleType { type SqlType; } impl CompatibleType for ST where DB: Backend, ST: SqlType + crate::sql_types::SingleValue, U: FromSqlRow, { type SqlType = ST; } impl CompatibleType for Untyped where U: FromSqlRow, DB: Backend, { type SqlType = Untyped; } impl CompatibleType for SelectBy where DB: Backend, ST: SqlType + TypedExpressionType, U: Selectable, E: Expression, U: FromSqlRow, { type SqlType = ST; } }