use crate::query::expr::Expr;
#[cfg(feature = "postgres")]
use crate::Value;
pub fn func<I>(name: &str, args: I) -> Expr
where
I: IntoIterator,
I::Item: Into<Expr>,
{
Expr::func(name, args.into_iter().map(Into::into))
}
pub fn lower(arg: impl Into<Expr>) -> Expr {
Expr::func("lower", [arg.into()])
}
pub fn upper(arg: impl Into<Expr>) -> Expr {
Expr::func("upper", [arg.into()])
}
pub fn length(arg: impl Into<Expr>) -> Expr {
Expr::func("length", [arg.into()])
}
pub fn trim(arg: impl Into<Expr>) -> Expr {
Expr::func("trim", [arg.into()])
}
pub fn abs(arg: impl Into<Expr>) -> Expr {
Expr::func("abs", [arg.into()])
}
pub fn coalesce(first: impl Into<Expr>, second: impl Into<Expr>) -> Expr {
Expr::func("coalesce", [first.into(), second.into()])
}
pub fn round(arg: impl Into<Expr>) -> Expr {
Expr::func("round", [arg.into()])
}
pub fn ceil(arg: impl Into<Expr>) -> Expr {
Expr::func("ceil", [arg.into()])
}
pub fn floor(arg: impl Into<Expr>) -> Expr {
Expr::func("floor", [arg.into()])
}
pub fn substr(arg: impl Into<Expr>, start: impl Into<Expr>) -> Expr {
Expr::func("substr", [arg.into(), start.into()])
}
pub fn substr_len(arg: impl Into<Expr>, start: impl Into<Expr>, len: impl Into<Expr>) -> Expr {
Expr::func("substr", [arg.into(), start.into(), len.into()])
}
pub fn concat<I>(args: I) -> Expr
where
I: IntoIterator,
I::Item: Into<Expr>,
{
Expr::func("concat", args.into_iter().map(Into::into))
}
pub fn nullif(a: impl Into<Expr>, b: impl Into<Expr>) -> Expr {
Expr::func("nullif", [a.into(), b.into()])
}
pub fn greatest<I>(args: I) -> Expr
where
I: IntoIterator,
I::Item: Into<Expr>,
{
Expr::func("greatest", args.into_iter().map(Into::into))
}
pub fn least<I>(args: I) -> Expr
where
I: IntoIterator,
I::Item: Into<Expr>,
{
Expr::func("least", args.into_iter().map(Into::into))
}
pub fn random_value() -> Expr {
Expr::func("random", [] as [Expr; 0])
}
pub fn replace(column: impl Into<Expr>, from: &str, to: &str) -> Expr {
Expr::func("replace", [
column.into(),
Expr::value(crate::value::Value::Text(from.to_string())),
Expr::value(crate::value::Value::Text(to.to_string())),
])
}
pub fn position(substring: &str, column: impl Into<Expr>) -> Expr {
Expr::func("position", [
Expr::value(crate::value::Value::Text(substring.to_string())),
column.into(),
])
}
pub fn current_timestamp() -> Expr {
Expr::raw("CURRENT_TIMESTAMP")
}
pub fn current_date() -> Expr {
Expr::raw("CURRENT_DATE")
}
pub fn current_time() -> Expr {
Expr::raw("CURRENT_TIME")
}
pub fn now() -> Expr {
Expr::func("NOW", [] as [Expr; 0])
}
pub fn extract(field: &str, source: impl Into<Expr>) -> Expr {
Expr::Extract {
field: field.to_string(),
source: Box::new(source.into()),
}
}
#[cfg(feature = "postgres")]
pub fn date_trunc(field: &str, source: impl Into<Expr>) -> Expr {
Expr::func("date_trunc", [
Expr::value(crate::value::Value::Text(field.to_string())),
source.into(),
])
}
#[cfg(feature = "postgres")]
pub fn age(end: impl Into<Expr>, start: impl Into<Expr>) -> Expr {
Expr::func("AGE", [end.into(), start.into()])
}
#[cfg(feature = "postgres")]
pub fn to_char(source: impl Into<Expr>, format: &str) -> Expr {
Expr::func("TO_CHAR", [
source.into(),
Expr::value(crate::value::Value::Text(format.to_string())),
])
}
#[cfg(feature = "postgres")]
pub fn at_time_zone(zone: &str, expr: impl Into<Expr>) -> Expr {
Expr::func("timezone", [
Expr::value(crate::value::Value::Text(zone.to_string())),
expr.into(),
])
}
pub fn row_number() -> Expr {
Expr::func("ROW_NUMBER", [] as [Expr; 0])
}
pub fn rank() -> Expr {
Expr::func("RANK", [] as [Expr; 0])
}
pub fn dense_rank() -> Expr {
Expr::func("DENSE_RANK", [] as [Expr; 0])
}
pub fn ntile(n: i64) -> Expr {
Expr::func("NTILE", [Expr::value(crate::value::Value::Int(n))])
}
pub fn lag(expr: impl Into<Expr>) -> Expr {
Expr::func("LAG", [expr.into()])
}
pub fn lag_offset(expr: impl Into<Expr>, offset: i64) -> Expr {
Expr::func("LAG", [expr.into(), Expr::value(crate::value::Value::Int(offset))])
}
pub fn lag_default(expr: impl Into<Expr>, offset: i64, default: impl Into<Expr>) -> Expr {
Expr::func("LAG", [
expr.into(),
Expr::value(crate::value::Value::Int(offset)),
default.into(),
])
}
pub fn lead(expr: impl Into<Expr>) -> Expr {
Expr::func("LEAD", [expr.into()])
}
pub fn lead_offset(expr: impl Into<Expr>, offset: i64) -> Expr {
Expr::func("LEAD", [expr.into(), Expr::value(crate::value::Value::Int(offset))])
}
pub fn lead_default(expr: impl Into<Expr>, offset: i64, default: impl Into<Expr>) -> Expr {
Expr::func("LEAD", [
expr.into(),
Expr::value(crate::value::Value::Int(offset)),
default.into(),
])
}
pub fn first_value(expr: impl Into<Expr>) -> Expr {
Expr::func("FIRST_VALUE", [expr.into()])
}
pub fn last_value(expr: impl Into<Expr>) -> Expr {
Expr::func("LAST_VALUE", [expr.into()])
}
pub fn nth_value(expr: impl Into<Expr>, n: i64) -> Expr {
Expr::func("NTH_VALUE", [expr.into(), Expr::value(crate::value::Value::Int(n))])
}
pub fn percent_rank() -> Expr {
Expr::func("PERCENT_RANK", [] as [Expr; 0])
}
pub fn cume_dist() -> Expr {
Expr::func("CUME_DIST", [] as [Expr; 0])
}
#[cfg(feature = "postgres")]
pub fn regex_match(column: impl Into<Expr>, pattern: &str) -> Expr {
Expr::func("regexp_like", [column.into(), Expr::value(crate::value::Value::Text(pattern.to_string()))])
}
#[cfg(feature = "postgres")]
pub fn regex_replace(column: impl Into<Expr>, pattern: &str, replacement: &str) -> Expr {
Expr::func("regexp_replace", [
column.into(),
Expr::value(crate::value::Value::Text(pattern.to_string())),
Expr::value(crate::value::Value::Text(replacement.to_string())),
])
}
#[cfg(feature = "postgres")]
pub fn split_part(column: impl Into<Expr>, delimiter: &str, field: i64) -> Expr {
Expr::func("split_part", [
column.into(),
Expr::value(crate::value::Value::Text(delimiter.to_string())),
Expr::value(crate::value::Value::Int(field)),
])
}
#[cfg(feature = "postgres")]
pub fn left(column: impl Into<Expr>, n: i64) -> Expr {
Expr::func("left", [column.into(), Expr::value(crate::value::Value::Int(n))])
}
#[cfg(feature = "postgres")]
pub fn right(column: impl Into<Expr>, n: i64) -> Expr {
Expr::func("right", [column.into(), Expr::value(crate::value::Value::Int(n))])
}
#[cfg(feature = "postgres")]
pub fn repeat(column: impl Into<Expr>, n: i64) -> Expr {
Expr::func("repeat", [column.into(), Expr::value(crate::value::Value::Int(n))])
}
#[cfg(feature = "postgres")]
pub fn reverse(column: impl Into<Expr>) -> Expr {
Expr::func("reverse", [column.into()])
}
#[cfg(feature = "postgres")]
pub fn string_aggregation(column: impl Into<Expr>, delimiter: &str) -> Expr {
Expr::aggregate(crate::query::expr::AggFunc::StringAggregation, [
column.into(),
Expr::value(crate::value::Value::Text(delimiter.to_string())),
])
}
#[cfg(feature = "postgres")]
pub fn array_aggregation(column: impl Into<Expr>) -> Expr {
Expr::aggregate(crate::query::expr::AggFunc::ArrayAggregation, [column.into()])
}
#[cfg(feature = "postgres")]
pub fn json_aggregation(column: impl Into<Expr>) -> Expr {
Expr::aggregate(crate::query::expr::AggFunc::JsonAggregation, [column.into()])
}
#[cfg(feature = "postgres")]
pub fn jsonb_aggregation(column: impl Into<Expr>) -> Expr {
Expr::aggregate(crate::query::expr::AggFunc::JsonbAggregation, [column.into()])
}
#[cfg(feature = "postgres")]
pub fn bool_and(column: impl Into<Expr>) -> Expr {
Expr::aggregate(crate::query::expr::AggFunc::BoolAnd, [column.into()])
}
#[cfg(feature = "postgres")]
pub fn bool_or(column: impl Into<Expr>) -> Expr {
Expr::aggregate(crate::query::expr::AggFunc::BoolOr, [column.into()])
}
#[cfg(feature = "postgres")]
pub fn to_tsvector(config: &str, text: impl Into<Expr>) -> Expr {
Expr::func("to_tsvector", [Expr::value(Value::Text(config.to_string())), text.into()])
}
#[cfg(feature = "postgres")]
pub fn to_tsvector_simple(text: impl Into<Expr>) -> Expr {
to_tsvector("simple", text)
}
#[cfg(feature = "postgres")]
pub fn to_tsquery(config: &str, query_text: impl Into<Expr>) -> Expr {
Expr::func("to_tsquery", [Expr::value(Value::Text(config.to_string())), query_text.into()])
}
#[cfg(feature = "postgres")]
pub fn plainto_tsquery(config: &str, query_text: impl Into<Expr>) -> Expr {
Expr::func("plainto_tsquery", [Expr::value(Value::Text(config.to_string())), query_text.into()])
}
#[cfg(feature = "postgres")]
pub fn phraseto_tsquery(config: &str, query_text: impl Into<Expr>) -> Expr {
Expr::func("phraseto_tsquery", [Expr::value(Value::Text(config.to_string())), query_text.into()])
}
#[cfg(feature = "postgres")]
pub fn ts_rank(vector: impl Into<Expr>, query: impl Into<Expr>) -> Expr {
Expr::func("ts_rank", [vector.into(), query.into()])
}
#[cfg(feature = "postgres")]
pub fn ts_rank_cd(vector: impl Into<Expr>, query: impl Into<Expr>) -> Expr {
Expr::func("ts_rank_cd", [vector.into(), query.into()])
}
#[cfg(feature = "postgres")]
pub fn ts_headline(
config: &str,
text: impl Into<Expr>,
query: impl Into<Expr>,
) -> Expr {
Expr::func(
"ts_headline",
[
Expr::value(Value::Text(config.to_string())),
text.into(),
query.into(),
],
)
}
#[cfg(feature = "postgres")]
pub fn tsquery(query_text: &str) -> Expr {
Expr::func("tsquery", [Expr::value(Value::Text(query_text.to_string()))])
}
#[cfg(feature = "mysql")]
pub fn group_concat(arg: impl Into<Expr>) -> Expr {
Expr::func("GROUP_CONCAT", [arg.into()])
}