| 1 |
use crate::expression::{Expression, ValidGrouping}; |
| 2 |
use crate::pg::Pg; |
| 3 |
use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement}; |
| 4 |
use crate::query_source::QuerySource; |
| 5 |
use crate::result::QueryResult; |
| 6 |
use crate::{JoinTo, SelectableExpression, Table}; |
| 7 |
|
| 8 |
/// Represents a query with an `ONLY` clause. |
| 9 |
#[derive(Debug, Clone, Copy, Default)] |
| 10 |
pub struct Only<S> { |
| 11 |
pub(crate) source: S, |
| 12 |
} |
| 13 |
|
| 14 |
impl<S> QueryId for Only<S> |
| 15 |
where |
| 16 |
Self: 'static, |
| 17 |
S: QueryId, |
| 18 |
{ |
| 19 |
type QueryId = Self; |
| 20 |
const HAS_STATIC_QUERY_ID: bool = <S as QueryId>::HAS_STATIC_QUERY_ID; |
| 21 |
} |
| 22 |
|
| 23 |
impl<S> QuerySource for Only<S> |
| 24 |
where |
| 25 |
S: Table + Clone, |
| 26 |
<S as QuerySource>::DefaultSelection: ValidGrouping<()> + SelectableExpression<Only<S>>, |
| 27 |
{ |
| 28 |
type FromClause = Self; |
| 29 |
type DefaultSelection = <S as QuerySource>::DefaultSelection; |
| 30 |
|
| 31 |
fn from_clause(&self) -> Self::FromClause { |
| 32 |
self.clone() |
| 33 |
} |
| 34 |
|
| 35 |
fn default_selection(&self) -> Self::DefaultSelection { |
| 36 |
self.source.default_selection() |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl<S> QueryFragment<Pg> for Only<S> |
| 41 |
where |
| 42 |
S: QueryFragment<Pg>, |
| 43 |
{ |
| 44 |
fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, Pg>) -> QueryResult<()> { |
| 45 |
pass.push_sql(" ONLY "); |
| 46 |
self.source.walk_ast(pass.reborrow())?; |
| 47 |
Ok(()) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl<S> AsQuery for Only<S> |
| 52 |
where |
| 53 |
S: Table + Clone, |
| 54 |
<S as QuerySource>::DefaultSelection: ValidGrouping<()> + SelectableExpression<Only<S>>, |
| 55 |
{ |
| 56 |
type SqlType = <<Self as QuerySource>::DefaultSelection as Expression>::SqlType; |
| 57 |
type Query = SelectStatement<FromClause<Self>>; |
| 58 |
|
| 59 |
fn as_query(self) -> Self::Query { |
| 60 |
SelectStatement::simple(self) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl<S, T> JoinTo<T> for Only<S> |
| 65 |
where |
| 66 |
S: JoinTo<T>, |
| 67 |
T: Table, |
| 68 |
S: Table, |
| 69 |
{ |
| 70 |
type FromClause = <S as JoinTo<T>>::FromClause; |
| 71 |
type OnClause = <S as JoinTo<T>>::OnClause; |
| 72 |
|
| 73 |
fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause) { |
| 74 |
<S as JoinTo<T>>::join_target(rhs) |
| 75 |
} |
| 76 |
} |
| 77 |
impl<S> Table for Only<S> |
| 78 |
where |
| 79 |
S: Table + Clone + AsQuery, |
| 80 |
|
| 81 |
<S as Table>::PrimaryKey: SelectableExpression<Only<S>>, |
| 82 |
<S as Table>::AllColumns: SelectableExpression<Only<S>>, |
| 83 |
<S as QuerySource>::DefaultSelection: ValidGrouping<()> + SelectableExpression<Only<S>>, |
| 84 |
{ |
| 85 |
type PrimaryKey = <S as Table>::PrimaryKey; |
| 86 |
type AllColumns = <S as Table>::AllColumns; |
| 87 |
|
| 88 |
fn primary_key(&self) -> Self::PrimaryKey { |
| 89 |
self.source.primary_key() |
| 90 |
} |
| 91 |
|
| 92 |
fn all_columns() -> Self::AllColumns { |
| 93 |
S::all_columns() |
| 94 |
} |
| 95 |
} |