use crate::sql::{SQL, Token};
use crate::traits::SQLParam;
use crate::types::{Double, Numeric};
use super::{Expr, NullOr, Nullability, SQLExpr, Scalar};
pub fn abs<'a, V, E>(expr: E) -> SQLExpr<'a, V, E::SQLType, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("ABS", expr.into_sql()))
}
pub fn round<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("ROUND", expr.into_sql()))
}
pub fn round_to<'a, V, E, P>(expr: E, precision: P) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
P: Expr<'a, V>,
{
SQLExpr::new(SQL::func(
"ROUND",
expr.into_sql()
.push(Token::COMMA)
.append(precision.into_sql()),
))
}
pub fn ceil<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("CEIL", expr.into_sql()))
}
pub fn floor<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("FLOOR", expr.into_sql()))
}
pub fn trunc<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("TRUNC", expr.into_sql()))
}
pub fn sqrt<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("SQRT", expr.into_sql()))
}
pub fn power<'a, V, E1, E2>(
base: E1,
exponent: E2,
) -> SQLExpr<'a, V, Double, <E1::Nullable as NullOr<E2::Nullable>>::Output, Scalar>
where
V: SQLParam + 'a,
E1: Expr<'a, V>,
E1::SQLType: Numeric,
E2: Expr<'a, V>,
E2::SQLType: Numeric,
E1::Nullable: NullOr<E2::Nullable>,
E2::Nullable: Nullability,
{
SQLExpr::new(SQL::func(
"POWER",
base.into_sql()
.push(Token::COMMA)
.append(exponent.into_sql()),
))
}
pub fn exp<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("EXP", expr.into_sql()))
}
pub fn ln<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("LN", expr.into_sql()))
}
pub fn log10<'a, V, E>(expr: E) -> SQLExpr<'a, V, Double, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("LOG10", expr.into_sql()))
}
pub fn log<'a, V, E1, E2>(
base: E1,
value: E2,
) -> SQLExpr<'a, V, Double, <E1::Nullable as NullOr<E2::Nullable>>::Output, Scalar>
where
V: SQLParam + 'a,
E1: Expr<'a, V>,
E1::SQLType: Numeric,
E2: Expr<'a, V>,
E2::SQLType: Numeric,
E1::Nullable: NullOr<E2::Nullable>,
E2::Nullable: Nullability,
{
SQLExpr::new(SQL::func(
"LOG",
base.into_sql().push(Token::COMMA).append(value.into_sql()),
))
}
pub fn sign<'a, V, E>(expr: E) -> SQLExpr<'a, V, E::SQLType, E::Nullable, Scalar>
where
V: SQLParam + 'a,
E: Expr<'a, V>,
E::SQLType: Numeric,
{
SQLExpr::new(SQL::func("SIGN", expr.into_sql()))
}
#[allow(clippy::type_complexity)]
pub fn mod_<'a, V, E1, E2>(
dividend: E1,
divisor: E2,
) -> SQLExpr<'a, V, E1::SQLType, <E1::Nullable as NullOr<E2::Nullable>>::Output, Scalar>
where
V: SQLParam + 'a,
E1: Expr<'a, V>,
E1::SQLType: Numeric,
E2: Expr<'a, V>,
E2::SQLType: Numeric,
E1::Nullable: NullOr<E2::Nullable>,
E2::Nullable: Nullability,
{
SQLExpr::new(
dividend
.into_sql()
.push(Token::REM)
.append(divisor.into_sql()),
)
}