use crate::query::condition::FilterValue;
use std::marker::PhantomData;
pub mod aggregate;
pub(crate) mod arithmetic;
pub mod case;
pub(crate) mod compare;
pub(crate) mod literal;
pub(crate) mod node;
#[cfg(feature = "spatial")]
pub mod row_aggregate;
#[cfg(feature = "spatial")]
pub(crate) mod spatial;
pub(crate) mod sql;
pub mod subquery;
pub mod window;
pub mod window_fn;
pub use aggregate::{AggregateExpr, grouping_of};
pub use aggregate::{HypotheticalSetAgg, KindEvidence, MetadataAgg, OrderedSetAgg, ValueAgg};
pub use case::{Case, CaseBuilder};
use node::ExprNode;
#[cfg(feature = "spatial")]
pub use row_aggregate::{BinaryRowAgg, MvtOptions, RowAggregate, RowKindEvidence};
pub use subquery::{Exists, OuterRef, Subquery};
pub use window::{FrameBound, FrameExclude, FrameKind, WindowBuilder, WindowSpec};
pub use window_fn::{
CumeDistWindow, DenseRank, FirstValueWindow, LagWindow, LastValueWindow, LeadWindow,
NthValueWindow, NtileWindow, PercentRankWindow, QualifyCondition, QualifyOp, Rank, RowNumber,
WindowRanking,
};
#[must_use = "expressions are lazy — dropping one silently omits the predicate"]
#[derive(Clone, Debug)]
pub struct Expr<T> {
pub(crate) node: ExprNode,
pub(crate) _phantom: PhantomData<fn() -> T>,
}
impl<T> Expr<T>
where
T: Into<Expr<T>>,
{
pub fn literal(v: T) -> Expr<T> {
v.into()
}
}
impl Expr<i32> {
#[must_use = "expressions are lazy — dropping one silently omits the predicate"]
pub fn current_year() -> Expr<i32> {
Expr::from_node(ExprNode::CurrentYear)
}
}
#[cfg(feature = "spatial")]
impl Expr<f64> {
#[must_use = "expressions are lazy — dropping one silently omits the predicate"]
pub fn area_of<G>(geom: &G) -> Expr<f64>
where
G: crate::geo::GeographyValue,
{
Expr::from_node(ExprNode::Spatial(crate::expr::spatial::SpatialExpr::Area {
geom_ewkb: geom.to_ewkb_bytes(),
}))
}
#[must_use = "expressions are lazy — dropping one silently omits the predicate"]
pub fn area_of_intersection<A, B>(a: &A, b: &B) -> Expr<f64>
where
A: crate::geo::GeographyValue,
B: crate::geo::GeographyValue,
{
Expr::from_node(ExprNode::Spatial(
crate::expr::spatial::SpatialExpr::AreaOfIntersection {
a_ewkb: a.to_ewkb_bytes(),
b_ewkb: b.to_ewkb_bytes(),
},
))
}
}
#[cfg(feature = "spatial")]
impl Expr<crate::geo::Polygon> {
#[must_use = "expressions are lazy — dropping one silently omits the predicate"]
pub fn intersection_of<A, B>(a: &A, b: &B) -> Expr<crate::geo::Polygon>
where
A: crate::geo::GeographyValue,
B: crate::geo::GeographyValue,
{
Expr::from_node(ExprNode::Spatial(
crate::expr::spatial::SpatialExpr::Intersection {
a_ewkb: a.to_ewkb_bytes(),
b_ewkb: b.to_ewkb_bytes(),
},
))
}
}
impl<T> Expr<T> {
pub(crate) fn from_node(node: ExprNode) -> Self {
Expr {
node,
_phantom: PhantomData,
}
}
pub(crate) fn from_literal(v: FilterValue) -> Self {
Expr {
node: ExprNode::Literal(v),
_phantom: PhantomData,
}
}
#[doc(hidden)]
pub fn __raw_sql_fragment(s: &'static str) -> Self {
Expr {
node: ExprNode::RawSql(s),
_phantom: PhantomData,
}
}
}