1use self::private::SqlOrdAggregate;
2#[cfg(doc)]
3use super::aggregate_expressions::{AggregateExpressionMethods, WindowExpressionMethods};
4use crate::expression::functions::declare_sql_function;
5
6#[doc =
" Represents a SQL `MAX` function. This function can only take types which are"]
#[doc = " ordered."]
#[doc = ""]
#[doc = " ## Window Function Usage"]
#[doc = ""]
#[doc =
" This function can be used as window function. See [`WindowExpressionMethods`] for details"]
#[doc = ""]
#[doc = " ## Aggregate Function Expression"]
#[doc = ""]
#[doc =
" This function can be used as aggregate expression. See [`AggregateExpressionMethods`] for details."]
#[doc = ""]
#[doc = " # Examples"]
#[doc = ""]
#[doc = " ## Normal function usage"]
#[doc = ""]
#[doc = " ```rust"]
#[doc = " # include!(\"../../doctest_setup.rs\");"]
#[doc = " # use diesel::dsl::*;"]
#[doc = " #"]
#[doc = " # fn main() {"]
#[doc = " # use schema::animals::dsl::*;"]
#[doc = " # let connection = &mut establish_connection();"]
#[doc =
" assert_eq!(Ok(Some(8)), animals.select(max(legs)).first(connection));"]
#[doc = " # }"]
#[doc = " ```"]
#[doc = ""]
#[doc = " ## Window function"]
#[doc = ""]
#[doc = " ```rust"]
#[doc = " # include!(\"../../doctest_setup.rs\");"]
#[doc = " # use diesel::dsl::*;"]
#[doc = " #"]
#[doc = " # fn main() {"]
#[doc = " # use schema::animals::dsl::*;"]
#[doc = " # let connection = &mut establish_connection();"]
#[doc = " let res = animals"]
#[doc = " .select((name, max(legs).partition_by(id)))"]
#[doc = " .load::<(Option<String>, Option<i32>)>(connection);"]
#[doc = ""]
#[doc = " assert_eq!("]
#[doc = " Ok(vec![(Some(\"Jack\".into()), Some(4)), (None, Some(8))]),"]
#[doc = " res"]
#[doc = " );"]
#[doc = " # }"]
#[doc = " ```"]
#[doc = ""]
#[doc = ""]
#[doc = " ## Aggregate function expression"]
#[doc = ""]
#[doc = " ```rust"]
#[doc = " # include!(\"../../doctest_setup.rs\");"]
#[doc = " # use diesel::dsl::*;"]
#[doc = " #"]
#[doc = " # fn main() {"]
#[doc = " # use schema::animals::dsl::*;"]
#[doc = " # let connection = &mut establish_connection();"]
#[doc = " # #[cfg(not(feature = \"mysql\"))]"]
#[doc = " assert_eq!("]
#[doc = " Ok(Some(4)),"]
#[doc = " animals"]
#[doc = " .select(max(legs).aggregate_filter(legs.lt(8)))"]
#[doc = " .first(connection)"]
#[doc = " );"]
#[doc = " # }"]
#[doc = " ```"]
#[allow(non_camel_case_types)]
pub fn max<ST: SqlOrdAggregate, expr>(expr: expr) -> max<ST, expr> where
expr: diesel::expression::AsExpression<ST> {
max_utils::max {
expr: expr.as_expression(),
ST: ::std::marker::PhantomData,
}
}
#[allow(non_camel_case_types, non_snake_case)]
#[doc = "The return type of [`max()`](fn@max)"]
pub type max<ST, expr> =
max_utils::max<ST,
<expr as diesel::expression::AsExpression<ST>>::Expression>;
#[doc(hidden)]
#[allow(non_camel_case_types, non_snake_case, unused_imports)]
pub(crate) mod max_utils {
use diesel::{self, QueryResult};
use diesel::expression::{
AsExpression, Expression, SelectableExpression, AppearsOnTable,
ValidGrouping,
};
use diesel::query_builder::{QueryFragment, AstPass};
use diesel::sql_types::*;
use diesel::internal::sql_functions::*;
use super::*;
pub struct max<ST, expr> {
pub(in super) expr: expr,
pub(in super) ST: ::std::marker::PhantomData<ST>,
}
const _: () =
{
use diesel;
use diesel::internal::derives::numeric_ops as ops;
use diesel::expression::{Expression, AsExpression};
use diesel::sql_types::ops::{Add, Sub, Mul, Div};
use diesel::sql_types::{SqlType, SingleValue};
impl<ST, expr, __Rhs> ::std::ops::Add<__Rhs> for max<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Add,
<<Self as Expression>::SqlType as Add>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Add>::Rhs> {
type Output = ops::Add<Self, __Rhs::Expression>;
fn add(self, rhs: __Rhs) -> Self::Output {
ops::Add::new(self, rhs.as_expression())
}
}
impl<ST, expr, __Rhs> ::std::ops::Sub<__Rhs> for max<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Sub,
<<Self as Expression>::SqlType as Sub>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Sub>::Rhs> {
type Output = ops::Sub<Self, __Rhs::Expression>;
fn sub(self, rhs: __Rhs) -> Self::Output {
ops::Sub::new(self, rhs.as_expression())
}
}
impl<ST, expr, __Rhs> ::std::ops::Mul<__Rhs> for max<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Mul,
<<Self as Expression>::SqlType as Mul>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Mul>::Rhs> {
type Output = ops::Mul<Self, __Rhs::Expression>;
fn mul(self, rhs: __Rhs) -> Self::Output {
ops::Mul::new(self, rhs.as_expression())
}
}
impl<ST, expr, __Rhs> ::std::ops::Div<__Rhs> for max<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Div,
<<Self as Expression>::SqlType as Div>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Div>::Rhs> {
type Output = ops::Div<Self, __Rhs::Expression>;
fn div(self, rhs: __Rhs) -> Self::Output {
ops::Div::new(self, rhs.as_expression())
}
}
};
#[automatically_derived]
impl<ST: ::core::fmt::Debug, expr: ::core::fmt::Debug> ::core::fmt::Debug
for max<ST, expr> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "max",
"expr", &self.expr, "ST", &&self.ST)
}
}
#[automatically_derived]
impl<ST: ::core::clone::Clone, expr: ::core::clone::Clone>
::core::clone::Clone for max<ST, expr> {
#[inline]
fn clone(&self) -> max<ST, expr> {
max {
expr: ::core::clone::Clone::clone(&self.expr),
ST: ::core::clone::Clone::clone(&self.ST),
}
}
}
#[automatically_derived]
impl<ST: ::core::marker::Copy, expr: ::core::marker::Copy>
::core::marker::Copy for max<ST, expr> {
}
const _: () =
{
use diesel;
#[allow(non_camel_case_types)]
impl<ST: diesel::query_builder::QueryId,
expr: diesel::query_builder::QueryId>
diesel::query_builder::QueryId for max<ST, expr> {
type QueryId =
max<<ST as diesel::query_builder::QueryId>::QueryId,
<expr as diesel::query_builder::QueryId>::QueryId>;
const HAS_STATIC_QUERY_ID: bool =
<ST as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID
&&
<expr as
diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID &&
true;
const IS_WINDOW_FUNCTION: bool =
<ST as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION
||
<expr as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION
|| false;
}
};
#[doc = "The return type of [`max()`](fn@max)"]
pub type HelperType<ST, expr> =
max<ST, <expr as AsExpression<ST>>::Expression>;
impl<ST: SqlOrdAggregate, expr> Expression for max<ST, expr> where
(expr): Expression {
type SqlType = ST::Ret;
}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
SelectableExpression<__DieselInternal> for max<ST, expr> where
expr: SelectableExpression<__DieselInternal>,
Self: AppearsOnTable<__DieselInternal> {}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
AppearsOnTable<__DieselInternal> for max<ST, expr> where
expr: AppearsOnTable<__DieselInternal>, Self: Expression {}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
FunctionFragment<__DieselInternal> for max<ST, expr> where
__DieselInternal: diesel::backend::Backend,
expr: QueryFragment<__DieselInternal> {
const FUNCTION_NAME: &'static str = "max";
#[allow(unused_assignments)]
fn walk_arguments<'__b>(&'__b self,
mut out: AstPass<'_, '__b, __DieselInternal>) -> QueryResult<()> {
let mut needs_comma = false;
if !self.expr.is_noop(out.backend())? {
if needs_comma { out.push_sql(", "); }
self.expr.walk_ast(out.reborrow())?;
needs_comma = true;
}
Ok(())
}
}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
QueryFragment<__DieselInternal> for max<ST, expr> where
__DieselInternal: diesel::backend::Backend,
expr: QueryFragment<__DieselInternal> {
fn walk_ast<'__b>(&'__b self,
mut out: AstPass<'_, '__b, __DieselInternal>) -> QueryResult<()> {
out.push_sql(<Self as
FunctionFragment<__DieselInternal>>::FUNCTION_NAME);
out.push_sql("(");
self.walk_arguments(out.reborrow())?;
out.push_sql(")");
Ok(())
}
}
impl<ST: SqlOrdAggregate, expr, __P, __O, __F, __DieselInternal>
WindowFunctionFragment<max<ST, expr>, __DieselInternal> for
OverClause<__P, __O, __F> where
__DieselInternal: diesel::backend::Backend {}
impl<ST: SqlOrdAggregate, expr> IsWindowFunction for max<ST, expr> {
type ArgTypes = (expr,);
}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
ValidGrouping<__DieselInternal> for max<ST, expr> {
type IsAggregate = diesel::expression::is_aggregate::Yes;
}
impl<ST: SqlOrdAggregate, expr> IsAggregateFunction for max<ST, expr> {}
}
#[doc =
" Represents a SQL `MIN` function. This function can only take types which are"]
#[doc = " ordered."]
#[doc = ""]
#[doc = " ## Window Function Usage"]
#[doc = ""]
#[doc =
" This function can be used as window function. See [`WindowExpressionMethods`] for details"]
#[doc = ""]
#[doc = " ## Aggregate Function Expression"]
#[doc = ""]
#[doc =
" This function can be used as aggregate expression. See [`AggregateExpressionMethods`] for details."]
#[doc = ""]
#[doc = " # Examples"]
#[doc = ""]
#[doc = " ## Normal function usage"]
#[doc = ""]
#[doc = " ```rust"]
#[doc = " # include!(\"../../doctest_setup.rs\");"]
#[doc = " # use diesel::dsl::*;"]
#[doc = " #"]
#[doc = " # fn main() {"]
#[doc = " # use schema::animals::dsl::*;"]
#[doc = " # let connection = &mut establish_connection();"]
#[doc =
" assert_eq!(Ok(Some(4)), animals.select(min(legs)).first(connection));"]
#[doc = " # }"]
#[doc = " ```"]
#[doc = ""]
#[doc = " ## Window function"]
#[doc = ""]
#[doc = " ```rust"]
#[doc = " # include!(\"../../doctest_setup.rs\");"]
#[doc = " # use diesel::dsl::*;"]
#[doc = " #"]
#[doc = " # fn main() {"]
#[doc = " # use schema::animals::dsl::*;"]
#[doc = " # let connection = &mut establish_connection();"]
#[doc = " let res = animals"]
#[doc = " .select((name, min(legs).partition_by(id)))"]
#[doc = " .load::<(Option<String>, Option<i32>)>(connection);"]
#[doc = ""]
#[doc = " assert_eq!("]
#[doc = " Ok(vec![(Some(\"Jack\".into()), Some(4)), (None, Some(8))]),"]
#[doc = " res"]
#[doc = " );"]
#[doc = " # }"]
#[doc = " ```"]
#[doc = ""]
#[doc = " ## Aggregate function expression"]
#[doc = ""]
#[doc = " ```rust"]
#[doc = " # include!(\"../../doctest_setup.rs\");"]
#[doc = " # use diesel::dsl::*;"]
#[doc = " #"]
#[doc = " # fn main() {"]
#[doc = " # use schema::animals::dsl::*;"]
#[doc = " # let connection = &mut establish_connection();"]
#[doc = " # #[cfg(not(feature = \"mysql\"))]"]
#[doc = " assert_eq!("]
#[doc = " Ok(Some(8)),"]
#[doc = " animals"]
#[doc = " .select(min(legs).aggregate_filter(legs.gt(4)))"]
#[doc = " .first(connection)"]
#[doc = " );"]
#[doc = " # }"]
#[doc = " ```"]
#[allow(non_camel_case_types)]
pub fn min<ST: SqlOrdAggregate, expr>(expr: expr) -> min<ST, expr> where
expr: diesel::expression::AsExpression<ST> {
min_utils::min {
expr: expr.as_expression(),
ST: ::std::marker::PhantomData,
}
}
#[allow(non_camel_case_types, non_snake_case)]
#[doc = "The return type of [`min()`](fn@min)"]
pub type min<ST, expr> =
min_utils::min<ST,
<expr as diesel::expression::AsExpression<ST>>::Expression>;
#[doc(hidden)]
#[allow(non_camel_case_types, non_snake_case, unused_imports)]
pub(crate) mod min_utils {
use diesel::{self, QueryResult};
use diesel::expression::{
AsExpression, Expression, SelectableExpression, AppearsOnTable,
ValidGrouping,
};
use diesel::query_builder::{QueryFragment, AstPass};
use diesel::sql_types::*;
use diesel::internal::sql_functions::*;
use super::*;
pub struct min<ST, expr> {
pub(in super) expr: expr,
pub(in super) ST: ::std::marker::PhantomData<ST>,
}
const _: () =
{
use diesel;
use diesel::internal::derives::numeric_ops as ops;
use diesel::expression::{Expression, AsExpression};
use diesel::sql_types::ops::{Add, Sub, Mul, Div};
use diesel::sql_types::{SqlType, SingleValue};
impl<ST, expr, __Rhs> ::std::ops::Add<__Rhs> for min<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Add,
<<Self as Expression>::SqlType as Add>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Add>::Rhs> {
type Output = ops::Add<Self, __Rhs::Expression>;
fn add(self, rhs: __Rhs) -> Self::Output {
ops::Add::new(self, rhs.as_expression())
}
}
impl<ST, expr, __Rhs> ::std::ops::Sub<__Rhs> for min<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Sub,
<<Self as Expression>::SqlType as Sub>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Sub>::Rhs> {
type Output = ops::Sub<Self, __Rhs::Expression>;
fn sub(self, rhs: __Rhs) -> Self::Output {
ops::Sub::new(self, rhs.as_expression())
}
}
impl<ST, expr, __Rhs> ::std::ops::Mul<__Rhs> for min<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Mul,
<<Self as Expression>::SqlType as Mul>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Mul>::Rhs> {
type Output = ops::Mul<Self, __Rhs::Expression>;
fn mul(self, rhs: __Rhs) -> Self::Output {
ops::Mul::new(self, rhs.as_expression())
}
}
impl<ST, expr, __Rhs> ::std::ops::Div<__Rhs> for min<ST, expr>
where Self: Expression, Self: Expression,
<Self as Expression>::SqlType: Div,
<<Self as Expression>::SqlType as Div>::Rhs: SqlType +
SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as
Div>::Rhs> {
type Output = ops::Div<Self, __Rhs::Expression>;
fn div(self, rhs: __Rhs) -> Self::Output {
ops::Div::new(self, rhs.as_expression())
}
}
};
#[automatically_derived]
impl<ST: ::core::fmt::Debug, expr: ::core::fmt::Debug> ::core::fmt::Debug
for min<ST, expr> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "min",
"expr", &self.expr, "ST", &&self.ST)
}
}
#[automatically_derived]
impl<ST: ::core::clone::Clone, expr: ::core::clone::Clone>
::core::clone::Clone for min<ST, expr> {
#[inline]
fn clone(&self) -> min<ST, expr> {
min {
expr: ::core::clone::Clone::clone(&self.expr),
ST: ::core::clone::Clone::clone(&self.ST),
}
}
}
#[automatically_derived]
impl<ST: ::core::marker::Copy, expr: ::core::marker::Copy>
::core::marker::Copy for min<ST, expr> {
}
const _: () =
{
use diesel;
#[allow(non_camel_case_types)]
impl<ST: diesel::query_builder::QueryId,
expr: diesel::query_builder::QueryId>
diesel::query_builder::QueryId for min<ST, expr> {
type QueryId =
min<<ST as diesel::query_builder::QueryId>::QueryId,
<expr as diesel::query_builder::QueryId>::QueryId>;
const HAS_STATIC_QUERY_ID: bool =
<ST as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID
&&
<expr as
diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID &&
true;
const IS_WINDOW_FUNCTION: bool =
<ST as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION
||
<expr as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION
|| false;
}
};
#[doc = "The return type of [`min()`](fn@min)"]
pub type HelperType<ST, expr> =
min<ST, <expr as AsExpression<ST>>::Expression>;
impl<ST: SqlOrdAggregate, expr> Expression for min<ST, expr> where
(expr): Expression {
type SqlType = ST::Ret;
}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
SelectableExpression<__DieselInternal> for min<ST, expr> where
expr: SelectableExpression<__DieselInternal>,
Self: AppearsOnTable<__DieselInternal> {}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
AppearsOnTable<__DieselInternal> for min<ST, expr> where
expr: AppearsOnTable<__DieselInternal>, Self: Expression {}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
FunctionFragment<__DieselInternal> for min<ST, expr> where
__DieselInternal: diesel::backend::Backend,
expr: QueryFragment<__DieselInternal> {
const FUNCTION_NAME: &'static str = "min";
#[allow(unused_assignments)]
fn walk_arguments<'__b>(&'__b self,
mut out: AstPass<'_, '__b, __DieselInternal>) -> QueryResult<()> {
let mut needs_comma = false;
if !self.expr.is_noop(out.backend())? {
if needs_comma { out.push_sql(", "); }
self.expr.walk_ast(out.reborrow())?;
needs_comma = true;
}
Ok(())
}
}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
QueryFragment<__DieselInternal> for min<ST, expr> where
__DieselInternal: diesel::backend::Backend,
expr: QueryFragment<__DieselInternal> {
fn walk_ast<'__b>(&'__b self,
mut out: AstPass<'_, '__b, __DieselInternal>) -> QueryResult<()> {
out.push_sql(<Self as
FunctionFragment<__DieselInternal>>::FUNCTION_NAME);
out.push_sql("(");
self.walk_arguments(out.reborrow())?;
out.push_sql(")");
Ok(())
}
}
impl<ST: SqlOrdAggregate, expr, __P, __O, __F, __DieselInternal>
WindowFunctionFragment<min<ST, expr>, __DieselInternal> for
OverClause<__P, __O, __F> where
__DieselInternal: diesel::backend::Backend {}
impl<ST: SqlOrdAggregate, expr> IsWindowFunction for min<ST, expr> {
type ArgTypes = (expr,);
}
impl<ST: SqlOrdAggregate, expr, __DieselInternal>
ValidGrouping<__DieselInternal> for min<ST, expr> {
type IsAggregate = diesel::expression::is_aggregate::Yes;
}
impl<ST: SqlOrdAggregate, expr> IsAggregateFunction for min<ST, expr> {}
}#[declare_sql_function]
7extern "SQL" {
8 #[aggregate]
74 #[window]
75 fn max<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
76
77 #[aggregate]
142 #[window]
143 fn min<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
144}
145
146mod private {
147 use crate::sql_types::{IntoNullable, SingleValue, SqlOrd, SqlType};
148 pub trait SqlOrdAggregate: SingleValue {
149 type Ret: SqlType + SingleValue;
150 }
151
152 impl<T> SqlOrdAggregate for T
153 where
154 T: SqlOrd + IntoNullable + SingleValue,
155 T::Nullable: SqlType + SingleValue,
156 {
157 type Ret = T::Nullable;
158 }
159}