| 1 |
use crate::backend::DieselReserveSpecialization; |
| 2 |
use crate::query_builder::*; |
| 3 |
use std::marker::PhantomData; |
| 4 |
|
| 5 |
#[doc(hidden)] // used by the table macro |
| 6 |
pub trait StaticQueryFragment { |
| 7 |
type Component: 'static; |
| 8 |
const STATIC_COMPONENT: &'static Self::Component; |
| 9 |
} |
| 10 |
|
| 11 |
#[derive(Debug, Copy, Clone)] |
| 12 |
#[doc(hidden)] // used by the table macro |
| 13 |
pub struct StaticQueryFragmentInstance<T>(PhantomData<T>); |
| 14 |
|
| 15 |
impl<T> StaticQueryFragmentInstance<T> { |
| 16 |
#[doc(hidden)] // used by the table macro |
| 17 |
pub const fn new() -> Self { |
| 18 |
Self(PhantomData) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
impl<T, DB> QueryFragment<DB> for StaticQueryFragmentInstance<T> |
| 23 |
where |
| 24 |
DB: Backend + DieselReserveSpecialization, |
| 25 |
T: StaticQueryFragment, |
| 26 |
T::Component: QueryFragment<DB>, |
| 27 |
{ |
| 28 |
fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { |
| 29 |
T::STATIC_COMPONENT.walk_ast(pass) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
#[derive(Debug, Copy, Clone)] |
| 34 |
#[doc(hidden)] // used by the table macro |
| 35 |
pub struct Identifier<'a>(pub &'a str); |
| 36 |
|
| 37 |
impl<DB: Backend> QueryFragment<DB> for Identifier<'_> { |
| 38 |
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { |
| 39 |
out.push_identifier(self.0) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
pub trait MiddleFragment<DB: Backend> { |
| 44 |
fn push_sql(&self, pass: AstPass<'_, '_, DB>); |
| 45 |
} |
| 46 |
|
| 47 |
impl<DB: Backend> MiddleFragment<DB> for &str { |
| 48 |
fn push_sql(&self, mut pass: AstPass<'_, '_, DB>) { |
| 49 |
pass.push_sql(self); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Copy, Clone)] |
| 54 |
#[doc(hidden)] // used by the table macro |
| 55 |
pub struct InfixNode<T, U, M> { |
| 56 |
lhs: T, |
| 57 |
rhs: U, |
| 58 |
middle: M, |
| 59 |
} |
| 60 |
|
| 61 |
impl<T, U, M> InfixNode<T, U, M> { |
| 62 |
#[doc(hidden)] // used by the table macro |
| 63 |
pub const fn new(lhs: T, rhs: U, middle: M) -> Self { |
| 64 |
InfixNode { lhs, rhs, middle } |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl<T, U, DB, M> QueryFragment<DB> for InfixNode<T, U, M> |
| 69 |
where |
| 70 |
DB: Backend + DieselReserveSpecialization, |
| 71 |
T: QueryFragment<DB>, |
| 72 |
U: QueryFragment<DB>, |
| 73 |
M: MiddleFragment<DB>, |
| 74 |
{ |
| 75 |
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { |
| 76 |
self.lhs.walk_ast(out.reborrow())?; |
| 77 |
self.middle.push_sql(out.reborrow()); |
| 78 |
self.rhs.walk_ast(out.reborrow())?; |
| 79 |
Ok(()) |
| 80 |
} |
| 81 |
} |