Debian Codegraph @

src/query_builder/select_clause.rs

raw
1 use super::from_clause::AsQuerySource;
2 use crate::expression::{Expression, SelectableExpression};
3 use crate::query_builder::*;
4 use crate::query_source::QuerySource;
5
6 #[doc(hidden)]
7 pub struct DefaultSelectClause<QS: AsQuerySource> {
8 default_selection: <QS::QuerySource as QuerySource>::DefaultSelection,
9 }
10
11 impl<QS> std::fmt::Debug for DefaultSelectClause<QS>
12 where
13 QS: AsQuerySource,
14 <QS::QuerySource as QuerySource>::DefaultSelection: std::fmt::Debug,
15 {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 f.debug_struct("DefaultSelectClause")
18 .field("default_selection", &self.default_selection)
19 .finish()
20 }
21 }
22
23 impl<QS> Clone for DefaultSelectClause<QS>
24 where
25 QS: AsQuerySource,
26 <QS::QuerySource as QuerySource>::DefaultSelection: Clone,
27 {
28 fn clone(&self) -> Self {
29 Self {
30 default_selection: self.default_selection.clone(),
31 }
32 }
33 }
34
35 impl<QS> Copy for DefaultSelectClause<QS>
36 where
37 QS: AsQuerySource,
38 <QS::QuerySource as QuerySource>::DefaultSelection: Copy,
39 {
40 }
41
42 impl<QS: AsQuerySource> DefaultSelectClause<QS> {
43 pub(crate) fn new(qs: &QS) -> Self {
44 Self {
45 default_selection: qs.as_query_source().default_selection(),
46 }
47 }
48 }
49
50 impl<QS> QueryId for DefaultSelectClause<QS>
51 where
52 QS: AsQuerySource,
53 <QS::QuerySource as QuerySource>::DefaultSelection: QueryId,
54 {
55 type QueryId = <<QS::QuerySource as QuerySource>::DefaultSelection as QueryId>::QueryId;
56
57 const HAS_STATIC_QUERY_ID: bool =
58 <<QS::QuerySource as QuerySource>::DefaultSelection as QueryId>::HAS_STATIC_QUERY_ID;
59 }
60
61 #[derive(Debug, Clone, Copy, QueryId)]
62 pub struct SelectClause<T>(pub T);
63
64 /// Specialised variant of `Expression` for select clause types
65 ///
66 /// The difference to the normal `Expression` trait is the query source (`QS`)
67 /// generic type parameter. This allows to access the query source in generic code.
68 #[cfg_attr(
69 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
70 cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")
71 )]
72 pub trait SelectClauseExpression<QS> {
73 /// The expression represented by the given select clause
74 type Selection;
75 /// SQL type of the select clause
76 type SelectClauseSqlType;
77 }
78
79 impl<T, QS> SelectClauseExpression<FromClause<QS>> for SelectClause<T>
80 where
81 QS: QuerySource,
82 T: SelectableExpression<QS>,
83 {
84 type Selection = T;
85 type SelectClauseSqlType = T::SqlType;
86 }
87
88 impl<T> SelectClauseExpression<NoFromClause> for SelectClause<T>
89 where
90 T: SelectableExpression<NoFromClause>,
91 {
92 type Selection = T;
93 type SelectClauseSqlType = T::SqlType;
94 }
95
96 impl<QS> SelectClauseExpression<FromClause<QS>> for DefaultSelectClause<FromClause<QS>>
97 where
98 QS: QuerySource,
99 {
100 type Selection = QS::DefaultSelection;
101 type SelectClauseSqlType = <Self::Selection as Expression>::SqlType;
102 }
103
104 impl<T, DB> QueryFragment<DB> for SelectClause<T>
105 where
106 DB: Backend,
107 T: QueryFragment<DB>,
108 {
109 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
110 self.0.walk_ast(pass)
111 }
112 }
113
114 impl<QS, DB> QueryFragment<DB> for DefaultSelectClause<QS>
115 where
116 DB: Backend,
117 QS: AsQuerySource,
118 <QS::QuerySource as QuerySource>::DefaultSelection: QueryFragment<DB>,
119 {
120 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
121 self.default_selection.walk_ast(pass)
122 }
123 }