Skip to main content

vlorql_core/function/
builder.rs

1//! Builder for [`FunctionDef`].
2//!
3//! Provides a chainable API that is more readable than constructing
4//! the struct directly.
5
6use std::borrow::Cow;
7
8use crate::schema::DataType;
9
10use super::def::{Dialect, FunctionDef, FunctionKind};
11
12/// Builder for [`FunctionDef`].
13#[derive(Debug, Clone)]
14pub struct FunctionDefBuilder {
15    names: Vec<Cow<'static, str>>,
16    kind: FunctionKind,
17    min_args: usize,
18    max_args: Option<usize>,
19    param_types: Option<Vec<Option<DataType>>>,
20    return_type: Option<DataType>,
21    supports_distinct: bool,
22    supports_order_by: bool,
23    allows_star: bool,
24    dialects: Vec<Dialect>,
25}
26
27impl FunctionDefBuilder {
28    /// Start building a function with the given canonical name.
29    pub fn new(name: &'static str) -> Self {
30        Self {
31            names: vec![Cow::Borrowed(name)],
32            kind: FunctionKind::Scalar,
33            min_args: 0,
34            max_args: None,
35            param_types: None,
36            return_type: None,
37            supports_distinct: false,
38            supports_order_by: false,
39            allows_star: false,
40            dialects: Vec::new(),
41        }
42    }
43
44    /// Add an alias for this function.
45    pub fn alias(mut self, alias: &'static str) -> Self {
46        self.names.push(Cow::Borrowed(alias));
47        self
48    }
49
50    /// Set the function category.
51    pub fn kind(mut self, kind: FunctionKind) -> Self {
52        self.kind = kind;
53        self
54    }
55
56    /// Set the minimum number of arguments.
57    pub fn min_args(mut self, min: usize) -> Self {
58        self.min_args = min;
59        self
60    }
61
62    /// Set the maximum number of arguments (`None` = unlimited).
63    pub fn max_args(mut self, max: Option<usize>) -> Self {
64        self.max_args = max;
65        self
66    }
67
68    /// Set expected parameter types for type-checking.
69    pub fn param_types(mut self, types: &[Option<DataType>]) -> Self {
70        self.param_types = Some(types.to_vec());
71        self
72    }
73
74    /// Set the return type for type inference.
75    pub fn return_type(mut self, ty: DataType) -> Self {
76        self.return_type = Some(ty);
77        self
78    }
79
80    /// Whether `DISTINCT` is allowed (e.g. `COUNT(DISTINCT col)`).
81    pub fn supports_distinct(mut self, yes: bool) -> Self {
82        self.supports_distinct = yes;
83        self
84    }
85
86    /// Whether an `ORDER BY` child clause is allowed.
87    pub fn supports_order_by(mut self, yes: bool) -> Self {
88        self.supports_order_by = yes;
89        self
90    }
91
92    /// Whether `*` is accepted as an argument (e.g. `COUNT(*)`).
93    pub fn allows_star(mut self, yes: bool) -> Self {
94        self.allows_star = yes;
95        self
96    }
97
98    /// Restrict this function to specific SQL dialects.
99    pub fn dialects(mut self, dialects: &[Dialect]) -> Self {
100        self.dialects = dialects.to_vec();
101        self
102    }
103
104    /// Finalise the builder and produce a [`FunctionDef`].
105    pub fn build(self) -> FunctionDef {
106        FunctionDef {
107            names: self.names,
108            kind: self.kind,
109            min_args: self.min_args,
110            max_args: self.max_args,
111            param_types: self.param_types,
112            return_type: self.return_type,
113            supports_distinct: self.supports_distinct,
114            supports_order_by: self.supports_order_by,
115            allows_star: self.allows_star,
116            dialects: self.dialects,
117        }
118    }
119}