use super::{FnDescriptor, FnVolatility, ImplStrategy, NamedDefault, Param, PylonFnDef, PylonType, SqlLanguage};
use ImplStrategy::{SqlBuiltin as B, SqlExpression as E, SqlOperator as O, TranspilerIntrinsic as I};
use PylonType::{
Any, AnyOrderable, AnyPoint, Array, BigInt, Bool, Box2D, Box3D, Bytes, DateDuration, Datetime, Decimal, Duration,
Float32, Float64, Geography, Geometry, Int16, Int32, Int64, Json, LocalDate, LocalDatetime, LocalTime, Multirange,
Optional, Range, RelativeDuration, Set, Str, Tuple, Uuid, Vector,
};
mod cal_ns;
mod crypto_ns;
mod math_ns;
mod pgvector_ns;
mod postgis_ns;
mod std_ns;
mod sys_ns;
fn arr(t: PylonType) -> PylonType {
Array(Box::new(t))
}
fn set_of(t: PylonType) -> PylonType {
Set(Box::new(t))
}
fn opt(t: PylonType) -> PylonType {
Optional(Box::new(t))
}
fn ro(t: PylonType) -> PylonType {
Range(Box::new(t))
}
fn mr(t: PylonType) -> PylonType {
Multirange(Box::new(t))
}
fn tup(ts: Vec<PylonType>) -> PylonType {
Tuple(ts)
}
fn p(name: &'static str, ty: PylonType) -> Param {
Param {
name,
ty,
variadic: false,
named_only: None,
keyword: None,
}
}
fn pv(name: &'static str, ty: PylonType) -> Param {
Param {
name,
ty,
variadic: true,
named_only: None,
keyword: None,
}
}
fn pn_as(name: &'static str, keyword: &'static str, ty: PylonType, default: NamedDefault) -> Param {
Param {
keyword: Some(keyword),
..pn(name, ty, default)
}
}
fn pn(name: &'static str, ty: PylonType, default: NamedDefault) -> Param {
Param {
name,
ty,
variadic: false,
named_only: Some(default),
keyword: None,
}
}
fn default_volatility(impl_: &ImplStrategy) -> FnVolatility {
match impl_ {
ImplStrategy::PylonFunction(def) => def.volatility,
_ => FnVolatility::Immutable,
}
}
fn f(ns: &'static str, name: &'static str, params: Vec<Param>, ret: PylonType, impl_: ImplStrategy) -> FnDescriptor {
FnDescriptor {
namespace: ns,
name,
params,
return_type: ret,
volatility: default_volatility(&impl_),
impl_strategy: impl_,
cast_target: false,
}
}
fn fc(ns: &'static str, name: &'static str, params: Vec<Param>, ret: PylonType, impl_: ImplStrategy) -> FnDescriptor {
FnDescriptor {
namespace: ns,
name,
params,
return_type: ret,
volatility: default_volatility(&impl_),
impl_strategy: impl_,
cast_target: true,
}
}
impl FnDescriptor {
fn vol(mut self, v: FnVolatility) -> Self {
self.volatility = v;
self
}
}
fn sql(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::Sql,
volatility: FnVolatility::Immutable,
strict: true,
returns_override: None,
body,
})
}
fn sql_nullable(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::Sql,
volatility: FnVolatility::Immutable,
strict: false,
returns_override: None,
body,
})
}
fn sql_returns(name: &'static str, returns: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::Sql,
volatility: FnVolatility::Immutable,
strict: true,
returns_override: Some(returns),
body,
})
}
fn plpgsql(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::PlPgSql,
volatility: FnVolatility::Immutable,
strict: true,
returns_override: None,
body,
})
}
fn plpgsql_nullable(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::PlPgSql,
volatility: FnVolatility::Immutable,
strict: false,
returns_override: None,
body,
})
}
fn plpgsql_stable_nullable(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::PlPgSql,
volatility: FnVolatility::Stable,
strict: false,
returns_override: Some("anyarray"),
body,
})
}
fn plpgsql_stable_nullable_bool(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::PlPgSql,
volatility: FnVolatility::Stable,
strict: false,
returns_override: Some("boolean"),
body,
})
}
fn plpgsql_stable_nullable_elem(name: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::PlPgSql,
volatility: FnVolatility::Stable,
strict: false,
returns_override: Some("anyelement"),
body,
})
}
fn plpgsql_stable_returns(name: &'static str, returns: &'static str, body: &'static str) -> ImplStrategy {
ImplStrategy::PylonFunction(PylonFnDef {
name,
language: SqlLanguage::PlPgSql,
volatility: FnVolatility::Stable,
strict: true,
returns_override: Some(returns),
body,
})
}
pub(super) fn build() -> Vec<FnDescriptor> {
let mut v = Vec::new();
v.extend(std_ns::build());
v.extend(math_ns::build());
v.extend(cal_ns::build());
v.extend(sys_ns::build());
v.extend(pgvector_ns::build());
v.extend(crypto_ns::build());
v.extend(postgis_ns::build());
v
}