Skip to main content

diesel/query_builder/
collected_query.rs

1use super::{AstPass, BindData, MoveableBindCollector, Query, QueryFragment, QueryId};
2use crate::backend::{Backend, DieselReserveSpecialization};
3use crate::result::QueryResult;
4use crate::sql_types::Untyped;
5use alloc::string::String;
6
7#[derive(Debug)]
8#[must_use = "Queries are only executed when calling `load`, `get_result` or similar."]
9/// A SQL query variant with already collected bind data which can be moved
10#[diesel_derives::__diesel_public_if(
11    feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
12)]
13pub struct CollectedQuery<T> {
14    sql: String,
15    safe_to_cache_prepared: bool,
16    bind_data: T,
17}
18
19impl<T> CollectedQuery<T> {
20    /// Builds a [CollectedQuery] with movable bind data
21    pub fn new(sql: String, safe_to_cache_prepared: bool, bind_data: T) -> Self {
22        Self {
23            sql,
24            safe_to_cache_prepared,
25            bind_data,
26        }
27    }
28}
29
30impl<DB, T> QueryFragment<DB> for CollectedQuery<T>
31where
32    T: BindData,
33    DB: Backend + DieselReserveSpecialization,
34    for<'a> <DB as Backend>::BindCollector<'a>: MoveableBindCollector<DB, BindData = T>,
35{
36    fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
37        if !self.safe_to_cache_prepared {
38            pass.unsafe_to_cache_prepared();
39        }
40        pass.push_sql(&self.sql);
41        pass.push_bind_collector_data::<T>(&self.bind_data)
42    }
43}
44
45impl<T> QueryId for CollectedQuery<T> {
46    type QueryId = ();
47
48    const HAS_STATIC_QUERY_ID: bool = false;
49}
50
51impl<T> Query for CollectedQuery<T> {
52    type SqlType = Untyped;
53}