vlorql_core/function/
builder.rs1use std::borrow::Cow;
7
8use crate::schema::DataType;
9
10use super::def::{Dialect, FunctionDef, FunctionKind};
11
12#[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 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 pub fn alias(mut self, alias: &'static str) -> Self {
46 self.names.push(Cow::Borrowed(alias));
47 self
48 }
49
50 pub fn kind(mut self, kind: FunctionKind) -> Self {
52 self.kind = kind;
53 self
54 }
55
56 pub fn min_args(mut self, min: usize) -> Self {
58 self.min_args = min;
59 self
60 }
61
62 pub fn max_args(mut self, max: Option<usize>) -> Self {
64 self.max_args = max;
65 self
66 }
67
68 pub fn param_types(mut self, types: &[Option<DataType>]) -> Self {
70 self.param_types = Some(types.to_vec());
71 self
72 }
73
74 pub fn return_type(mut self, ty: DataType) -> Self {
76 self.return_type = Some(ty);
77 self
78 }
79
80 pub fn supports_distinct(mut self, yes: bool) -> Self {
82 self.supports_distinct = yes;
83 self
84 }
85
86 pub fn supports_order_by(mut self, yes: bool) -> Self {
88 self.supports_order_by = yes;
89 self
90 }
91
92 pub fn allows_star(mut self, yes: bool) -> Self {
94 self.allows_star = yes;
95 self
96 }
97
98 pub fn dialects(mut self, dialects: &[Dialect]) -> Self {
100 self.dialects = dialects.to_vec();
101 self
102 }
103
104 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}