| 1 |
//! Within this module, types commonly use the following abbreviations: |
| 2 |
//! |
| 3 |
//! F: From Clause |
| 4 |
//! S: Select Clause |
| 5 |
//! D: Distinct Clause |
| 6 |
//! W: Where Clause |
| 7 |
//! O: Order By Clause |
| 8 |
//! L: Limit Clause |
| 9 |
//! Of: Offset Clause |
| 10 |
//! G: Group By Clause |
| 11 |
//! H: Having clause |
| 12 |
//! LC: For Update Clause |
| 13 |
|
| 14 |
pub(crate) mod boxed; |
| 15 |
mod dsl_impls; |
| 16 |
#[diesel_derives::__diesel_public_if( |
| 17 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 18 |
)] |
| 19 |
pub(crate) use self::boxed::BoxedSelectStatement; |
| 20 |
|
| 21 |
use super::distinct_clause::NoDistinctClause; |
| 22 |
use super::from_clause::AsQuerySource; |
| 23 |
use super::from_clause::FromClause; |
| 24 |
use super::group_by_clause::*; |
| 25 |
use super::limit_clause::NoLimitClause; |
| 26 |
use super::locking_clause::NoLockingClause; |
| 27 |
use super::offset_clause::NoOffsetClause; |
| 28 |
use super::order_clause::NoOrderClause; |
| 29 |
use super::select_clause::*; |
| 30 |
use super::where_clause::*; |
| 31 |
use super::NoFromClause; |
| 32 |
use super::{AstPass, Query, QueryFragment}; |
| 33 |
use crate::backend::{sql_dialect, Backend}; |
| 34 |
use crate::expression::subselect::ValidSubselect; |
| 35 |
use crate::expression::*; |
| 36 |
use crate::query_builder::having_clause::NoHavingClause; |
| 37 |
use crate::query_builder::limit_offset_clause::LimitOffsetClause; |
| 38 |
use crate::query_builder::{QueryId, SelectQuery}; |
| 39 |
use crate::query_dsl::order_dsl::ValidOrderingForDistinct; |
| 40 |
use crate::query_source::joins::{AppendSelection, Inner, Join}; |
| 41 |
use crate::query_source::*; |
| 42 |
use crate::result::QueryResult; |
| 43 |
|
| 44 |
/// This type represents a select query |
| 45 |
/// |
| 46 |
/// Using this type directly is only meaningful for custom backends |
| 47 |
/// that need to provide a custom [`QueryFragment`] implementation |
| 48 |
#[diesel_derives::__diesel_public_if( |
| 49 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes", |
| 50 |
public_fields( |
| 51 |
select, |
| 52 |
from, |
| 53 |
distinct, |
| 54 |
where_clause, |
| 55 |
order, |
| 56 |
limit_offset, |
| 57 |
group_by, |
| 58 |
having, |
| 59 |
locking |
| 60 |
) |
| 61 |
)] |
| 62 |
#[derive(Debug, Clone, Copy, QueryId)] |
| 63 |
#[must_use = "Queries are only executed when calling `load`, `get_result` or similar."] |
| 64 |
pub struct SelectStatement< |
| 65 |
From, |
| 66 |
Select = DefaultSelectClause<From>, |
| 67 |
Distinct = NoDistinctClause, |
| 68 |
Where = NoWhereClause, |
| 69 |
Order = NoOrderClause, |
| 70 |
LimitOffset = LimitOffsetClause<NoLimitClause, NoOffsetClause>, |
| 71 |
GroupBy = NoGroupByClause, |
| 72 |
Having = NoHavingClause, |
| 73 |
Locking = NoLockingClause, |
| 74 |
> { |
| 75 |
/// The select clause of the query |
| 76 |
pub(crate) select: Select, |
| 77 |
/// The from clause of the query |
| 78 |
pub(crate) from: From, |
| 79 |
/// The distinct clause of the query |
| 80 |
pub(crate) distinct: Distinct, |
| 81 |
/// The where clause of the query |
| 82 |
pub(crate) where_clause: Where, |
| 83 |
/// The order clause of the query |
| 84 |
pub(crate) order: Order, |
| 85 |
/// The combined limit/offset clause of the query |
| 86 |
pub(crate) limit_offset: LimitOffset, |
| 87 |
/// The group by clause of the query |
| 88 |
pub(crate) group_by: GroupBy, |
| 89 |
/// The having clause of the query |
| 90 |
pub(crate) having: Having, |
| 91 |
/// The locking clause of the query |
| 92 |
pub(crate) locking: Locking, |
| 93 |
} |
| 94 |
|
| 95 |
/// Semi-Private trait for containing get-functions for all `SelectStatement` fields |
| 96 |
// |
| 97 |
// This is used by `#[derive(MultiConnection)]` |
| 98 |
pub trait SelectStatementAccessor { |
| 99 |
/// The type of the select clause |
| 100 |
type Select; |
| 101 |
/// The type of the from clause |
| 102 |
type From; |
| 103 |
/// The type of the distinct clause |
| 104 |
type Distinct; |
| 105 |
/// The type of the where clause |
| 106 |
type Where; |
| 107 |
/// The type of the order clause |
| 108 |
type Order; |
| 109 |
/// The type of the limit offset clause |
| 110 |
type LimitOffset; |
| 111 |
/// The type of the group by clause |
| 112 |
type GroupBy; |
| 113 |
/// The type of the having clause |
| 114 |
type Having; |
| 115 |
/// The type of the locking clause |
| 116 |
type Locking; |
| 117 |
|
| 118 |
/// Access the select clause |
| 119 |
fn select_clause(&self) -> &Self::Select; |
| 120 |
/// Access the from clause |
| 121 |
#[allow(clippy::wrong_self_convention)] // obviously wrong, as `from` refers to the clause name |
| 122 |
fn from_clause(&self) -> &Self::From; |
| 123 |
/// Access the distinct clause |
| 124 |
fn distinct_clause(&self) -> &Self::Distinct; |
| 125 |
/// Access the where clause |
| 126 |
fn where_clause(&self) -> &Self::Where; |
| 127 |
/// Access the order clause |
| 128 |
fn order_clause(&self) -> &Self::Order; |
| 129 |
/// Access the limit_offset clause |
| 130 |
fn limit_offset_clause(&self) -> &Self::LimitOffset; |
| 131 |
/// Access the group by clause |
| 132 |
fn group_by_clause(&self) -> &Self::GroupBy; |
| 133 |
/// Access the having clause |
| 134 |
fn having_clause(&self) -> &Self::Having; |
| 135 |
/// Access the locking clause |
| 136 |
fn locking_clause(&self) -> &Self::Locking; |
| 137 |
} |
| 138 |
|
| 139 |
impl<F, S, D, W, O, LOf, G, H, LC> SelectStatementAccessor |
| 140 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 141 |
{ |
| 142 |
type Select = S; |
| 143 |
type From = F; |
| 144 |
type Distinct = D; |
| 145 |
type Where = W; |
| 146 |
type Order = O; |
| 147 |
type LimitOffset = LOf; |
| 148 |
type GroupBy = G; |
| 149 |
type Having = H; |
| 150 |
type Locking = LC; |
| 151 |
|
| 152 |
fn select_clause(&self) -> &Self::Select { |
| 153 |
&self.select |
| 154 |
} |
| 155 |
|
| 156 |
fn from_clause(&self) -> &Self::From { |
| 157 |
&self.from |
| 158 |
} |
| 159 |
|
| 160 |
fn distinct_clause(&self) -> &Self::Distinct { |
| 161 |
&self.distinct |
| 162 |
} |
| 163 |
|
| 164 |
fn where_clause(&self) -> &Self::Where { |
| 165 |
&self.where_clause |
| 166 |
} |
| 167 |
|
| 168 |
fn order_clause(&self) -> &Self::Order { |
| 169 |
&self.order |
| 170 |
} |
| 171 |
|
| 172 |
fn limit_offset_clause(&self) -> &Self::LimitOffset { |
| 173 |
&self.limit_offset |
| 174 |
} |
| 175 |
|
| 176 |
fn group_by_clause(&self) -> &Self::GroupBy { |
| 177 |
&self.group_by |
| 178 |
} |
| 179 |
|
| 180 |
fn having_clause(&self) -> &Self::Having { |
| 181 |
&self.having |
| 182 |
} |
| 183 |
|
| 184 |
fn locking_clause(&self) -> &Self::Locking { |
| 185 |
&self.locking |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl<F, S, D, W, O, LOf, G, H, LC> SelectStatement<F, S, D, W, O, LOf, G, H, LC> { |
| 190 |
#[allow(clippy::too_many_arguments)] |
| 191 |
pub(crate) fn new( |
| 192 |
select: S, |
| 193 |
from: F, |
| 194 |
distinct: D, |
| 195 |
where_clause: W, |
| 196 |
order: O, |
| 197 |
limit_offset: LOf, |
| 198 |
group_by: G, |
| 199 |
having: H, |
| 200 |
locking: LC, |
| 201 |
) -> Self { |
| 202 |
SelectStatement { |
| 203 |
select, |
| 204 |
from, |
| 205 |
distinct, |
| 206 |
where_clause, |
| 207 |
order, |
| 208 |
limit_offset, |
| 209 |
group_by, |
| 210 |
having, |
| 211 |
locking, |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
impl<F: QuerySource> SelectStatement<FromClause<F>> { |
| 217 |
// This is used by the `table!` macro |
| 218 |
#[doc(hidden)] |
| 219 |
pub fn simple(from: F) -> Self { |
| 220 |
let from = FromClause::new(from); |
| 221 |
SelectStatement::new( |
| 222 |
DefaultSelectClause::new(&from), |
| 223 |
from, |
| 224 |
NoDistinctClause, |
| 225 |
NoWhereClause, |
| 226 |
NoOrderClause, |
| 227 |
LimitOffsetClause { |
| 228 |
limit_clause: NoLimitClause, |
| 229 |
offset_clause: NoOffsetClause, |
| 230 |
}, |
| 231 |
NoGroupByClause, |
| 232 |
NoHavingClause, |
| 233 |
NoLockingClause, |
| 234 |
) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
impl<F, S, D, W, O, LOf, G, H, LC> Query for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 239 |
where |
| 240 |
G: ValidGroupByClause, |
| 241 |
S: SelectClauseExpression<F>, |
| 242 |
S::Selection: ValidGrouping<G::Expressions>, |
| 243 |
W: ValidWhereClause<F>, |
| 244 |
{ |
| 245 |
type SqlType = S::SelectClauseSqlType; |
| 246 |
} |
| 247 |
|
| 248 |
impl<F, S, D, W, O, LOf, G, H, LC> SelectQuery for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 249 |
where |
| 250 |
S: SelectClauseExpression<F>, |
| 251 |
O: ValidOrderingForDistinct<D>, |
| 252 |
{ |
| 253 |
type SqlType = S::SelectClauseSqlType; |
| 254 |
} |
| 255 |
|
| 256 |
impl<F, S, D, W, O, LOf, G, H, LC, DB> QueryFragment<DB> |
| 257 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 258 |
where |
| 259 |
DB: Backend, |
| 260 |
Self: QueryFragment<DB, DB::SelectStatementSyntax>, |
| 261 |
{ |
| 262 |
fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { |
| 263 |
<Self as QueryFragment<DB, DB::SelectStatementSyntax>>::walk_ast(self, pass) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl<F, S, D, W, O, LOf, G, H, LC, DB> |
| 268 |
QueryFragment<DB, sql_dialect::select_statement_syntax::AnsiSqlSelectStatement> |
| 269 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 270 |
where |
| 271 |
DB: Backend< |
| 272 |
SelectStatementSyntax = sql_dialect::select_statement_syntax::AnsiSqlSelectStatement, |
| 273 |
>, |
| 274 |
S: QueryFragment<DB>, |
| 275 |
F: QueryFragment<DB>, |
| 276 |
D: QueryFragment<DB>, |
| 277 |
W: QueryFragment<DB>, |
| 278 |
O: QueryFragment<DB>, |
| 279 |
LOf: QueryFragment<DB>, |
| 280 |
G: QueryFragment<DB>, |
| 281 |
H: QueryFragment<DB>, |
| 282 |
LC: QueryFragment<DB>, |
| 283 |
{ |
| 284 |
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { |
| 285 |
out.push_sql("SELECT "); |
| 286 |
self.distinct.walk_ast(out.reborrow())?; |
| 287 |
self.select.walk_ast(out.reborrow())?; |
| 288 |
self.from.walk_ast(out.reborrow())?; |
| 289 |
self.where_clause.walk_ast(out.reborrow())?; |
| 290 |
self.group_by.walk_ast(out.reborrow())?; |
| 291 |
self.having.walk_ast(out.reborrow())?; |
| 292 |
self.order.walk_ast(out.reborrow())?; |
| 293 |
self.limit_offset.walk_ast(out.reborrow())?; |
| 294 |
self.locking.walk_ast(out.reborrow())?; |
| 295 |
Ok(()) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
impl<S, F, D, W, O, LOf, G, H, LC, QS> ValidSubselect<QS> |
| 300 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC> |
| 301 |
where |
| 302 |
Self: SelectQuery, |
| 303 |
F: QuerySource, |
| 304 |
QS: QuerySource, |
| 305 |
Join<F, QS, Inner>: QuerySource, |
| 306 |
W: ValidWhereClause<FromClause<Join<F, QS, Inner>>>, |
| 307 |
{ |
| 308 |
} |
| 309 |
|
| 310 |
impl<S, D, W, O, LOf, G, H, LC> ValidSubselect<NoFromClause> |
| 311 |
for SelectStatement<NoFromClause, S, D, W, O, LOf, G, H, LC> |
| 312 |
where |
| 313 |
Self: SelectQuery, |
| 314 |
W: ValidWhereClause<NoFromClause>, |
| 315 |
{ |
| 316 |
} |
| 317 |
|
| 318 |
impl<S, F, D, W, O, LOf, G, H, LC> ValidSubselect<NoFromClause> |
| 319 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC> |
| 320 |
where |
| 321 |
Self: SelectQuery, |
| 322 |
F: QuerySource, |
| 323 |
W: ValidWhereClause<FromClause<F>>, |
| 324 |
{ |
| 325 |
} |
| 326 |
|
| 327 |
impl<S, D, W, O, LOf, G, H, LC, QS> ValidSubselect<QS> |
| 328 |
for SelectStatement<NoFromClause, S, D, W, O, LOf, G, H, LC> |
| 329 |
where |
| 330 |
Self: SelectQuery, |
| 331 |
QS: QuerySource, |
| 332 |
W: ValidWhereClause<NoFromClause>, |
| 333 |
{ |
| 334 |
} |
| 335 |
|
| 336 |
/// Allow `SelectStatement<From>` to act as if it were `From` as long as |
| 337 |
/// no other query methods have been called on it |
| 338 |
impl<From, T> AppearsInFromClause<T> for SelectStatement<From> |
| 339 |
where |
| 340 |
From: AsQuerySource, |
| 341 |
From::QuerySource: AppearsInFromClause<T> + QuerySource, |
| 342 |
{ |
| 343 |
type Count = <From::QuerySource as AppearsInFromClause<T>>::Count; |
| 344 |
} |
| 345 |
|
| 346 |
impl<From> QuerySource for SelectStatement<From> |
| 347 |
where |
| 348 |
From: AsQuerySource, |
| 349 |
<From::QuerySource as QuerySource>::DefaultSelection: SelectableExpression<Self>, |
| 350 |
{ |
| 351 |
type FromClause = <From::QuerySource as QuerySource>::FromClause; |
| 352 |
type DefaultSelection = <From::QuerySource as QuerySource>::DefaultSelection; |
| 353 |
|
| 354 |
fn from_clause(&self) -> <From::QuerySource as QuerySource>::FromClause { |
| 355 |
self.from.as_query_source().from_clause() |
| 356 |
} |
| 357 |
|
| 358 |
fn default_selection(&self) -> Self::DefaultSelection { |
| 359 |
self.from.as_query_source().default_selection() |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
impl<From, Selection> AppendSelection<Selection> for SelectStatement<From> |
| 364 |
where |
| 365 |
From: AsQuerySource, |
| 366 |
From::QuerySource: AppendSelection<Selection>, |
| 367 |
{ |
| 368 |
type Output = <From::QuerySource as AppendSelection<Selection>>::Output; |
| 369 |
|
| 370 |
fn append_selection(&self, selection: Selection) -> Self::Output { |
| 371 |
self.from.as_query_source().append_selection(selection) |
| 372 |
} |
| 373 |
} |