use std::borrow::Cow;
use crate::schema::DataType;
use super::def::{Dialect, FunctionDef, FunctionKind};
#[derive(Debug, Clone)]
pub struct FunctionDefBuilder {
names: Vec<Cow<'static, str>>,
kind: FunctionKind,
min_args: usize,
max_args: Option<usize>,
param_types: Option<Vec<Option<DataType>>>,
return_type: Option<DataType>,
supports_distinct: bool,
supports_order_by: bool,
allows_star: bool,
dialects: Vec<Dialect>,
}
impl FunctionDefBuilder {
pub fn new(name: &'static str) -> Self {
Self {
names: vec![Cow::Borrowed(name)],
kind: FunctionKind::Scalar,
min_args: 0,
max_args: None,
param_types: None,
return_type: None,
supports_distinct: false,
supports_order_by: false,
allows_star: false,
dialects: Vec::new(),
}
}
pub fn alias(mut self, alias: &'static str) -> Self {
self.names.push(Cow::Borrowed(alias));
self
}
pub fn kind(mut self, kind: FunctionKind) -> Self {
self.kind = kind;
self
}
pub fn min_args(mut self, min: usize) -> Self {
self.min_args = min;
self
}
pub fn max_args(mut self, max: Option<usize>) -> Self {
self.max_args = max;
self
}
pub fn param_types(mut self, types: &[Option<DataType>]) -> Self {
self.param_types = Some(types.to_vec());
self
}
pub fn return_type(mut self, ty: DataType) -> Self {
self.return_type = Some(ty);
self
}
pub fn supports_distinct(mut self, yes: bool) -> Self {
self.supports_distinct = yes;
self
}
pub fn supports_order_by(mut self, yes: bool) -> Self {
self.supports_order_by = yes;
self
}
pub fn allows_star(mut self, yes: bool) -> Self {
self.allows_star = yes;
self
}
pub fn dialects(mut self, dialects: &[Dialect]) -> Self {
self.dialects = dialects.to_vec();
self
}
pub fn build(self) -> FunctionDef {
FunctionDef {
names: self.names,
kind: self.kind,
min_args: self.min_args,
max_args: self.max_args,
param_types: self.param_types,
return_type: self.return_type,
supports_distinct: self.supports_distinct,
supports_order_by: self.supports_order_by,
allows_star: self.allows_star,
dialects: self.dialects,
}
}
}