Debian Codegraph @

src/query_builder/collected_query.rs

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