1use super::{input, SQLParam};
10use crate::{
11 expr::{EngineHook, EngineValueCatalog, RangeBoundsInput, RangeValueDescriptor},
12 ColumnType, FunctionTypeResolver, SQLError,
13};
14use uqa_core::{
15 memory::{Produced, ProductionControl, ProductionVec},
16 Value,
17};
18
19#[cfg(test)]
20mod tests;
21
22pub enum RangeParameterBounds<'a> {
24 Empty,
25 Bounds {
26 lower: Option<&'a SQLParam>,
27 upper: Option<&'a SQLParam>,
28 lower_inclusive: bool,
29 upper_inclusive: bool,
30 },
31}
32
33pub fn create_range_parameter_with_control(
35 type_name: &str,
36 bounds: RangeParameterBounds<'_>,
37 types: &dyn FunctionTypeResolver,
38 input: &dyn EngineHook,
39 control: &ProductionControl<'_>,
40) -> Result<Produced<SQLParam>, SQLError> {
41 let ty = input::resolve_type(type_name, types, control)?;
42 let base = base_type(&ty);
43 if !matches!(base, ColumnType::Range(_) | ColumnType::DeclaredRange(_)) {
44 return Err(SQLError::TypeMismatch(
45 "range construction requires a range type".into(),
46 ));
47 }
48 let catalog = EngineValueCatalog(input);
49 let descriptor =
50 RangeValueDescriptor::from_type_with_control(base, Some(&catalog), control)?
51 .ok_or_else(|| SQLError::Internal("range construction has no descriptor".into()))?;
52 let bounds = match bounds {
53 RangeParameterBounds::Empty => RangeBoundsInput::Empty,
54 RangeParameterBounds::Bounds {
55 lower,
56 upper,
57 lower_inclusive,
58 upper_inclusive,
59 } => {
60 let source = |parameter: Option<&SQLParam>| {
61 parameter
62 .map(|parameter| {
63 super::bind_parameter_type_with_control(parameter, types, control)
64 })
65 .transpose()
66 .map(Option::flatten)
67 };
68 let lower_source = source(lower)?;
69 let upper_source = source(upper)?;
70 let bound = |parameter: Option<&SQLParam>, source: Option<&ColumnType>| {
71 let Some(parameter) = parameter else {
72 return Ok(control.finish(None, control.empty_reservation())?);
73 };
74 let (value, memory) =
75 input::cast_parameter(parameter, source, &descriptor.endpoint, input, control)?
76 .into_parts();
77 Ok::<_, SQLError>(control.finish(
78 if matches!(value, Value::Null) {
79 None
80 } else {
81 Some(value)
82 },
83 memory,
84 )?)
85 };
86 RangeBoundsInput::Bounds {
87 lower: bound(lower, lower_source.as_deref())?,
88 upper: bound(upper, upper_source.as_deref())?,
89 lower_inclusive,
90 upper_inclusive,
91 }
92 }
93 };
94 let value = match base {
95 ColumnType::Range(subtype) => {
96 crate::expr::range_value_from_bounds_with_control(*subtype, bounds, control)?
97 }
98 ColumnType::DeclaredRange(reference) => {
99 input.construct_range(reference.oid, bounds, control)?
100 }
101 _ => unreachable!("checked range declaration"),
102 };
103 finish(value, ty, input, control)
104}
105
106pub fn create_multirange_parameter_with_control(
108 type_name: &str,
109 members: &[SQLParam],
110 types: &dyn FunctionTypeResolver,
111 input: &dyn EngineHook,
112 control: &ProductionControl<'_>,
113) -> Result<Produced<SQLParam>, SQLError> {
114 let ty = input::resolve_type(type_name, types, control)?;
115 let base = base_type(&ty);
116 if !matches!(
117 base,
118 ColumnType::Multirange(_) | ColumnType::DeclaredMultirange(_)
119 ) {
120 return Err(SQLError::TypeMismatch(
121 "multirange construction requires a multirange type".into(),
122 ));
123 }
124 let catalog = EngineValueCatalog(input);
125 let descriptor = RangeValueDescriptor::from_type_with_control(base, Some(&catalog), control)?
126 .ok_or_else(|| {
127 SQLError::Internal("multirange construction has no descriptor".into())
128 })?;
129 let range_type = descriptor.range_type_with_control(Some(&catalog), control)?;
130 let sources = input::bind_sources(members, types, control)?;
131 let mut values = ProductionVec::new(*control);
132 values.reserve(members.len())?;
133 for (member, source) in members.iter().zip(sources.iter()) {
134 let value = input::cast_parameter(member, source.as_ref(), &range_type, input, control)?;
135 if matches!(*value, Value::Null) {
136 return Err(SQLError::Routine {
137 sqlstate: "22004".into(),
138 message: "multirange values cannot contain null members".into(),
139 });
140 }
141 values.push_produced(value)?;
142 }
143 let value = match base {
144 ColumnType::Multirange(subtype) => {
145 crate::expr::multirange_value_from_members_with_control(*subtype, &values, control)?
146 }
147 ColumnType::DeclaredMultirange(reference) => {
148 input.construct_multirange(reference.oid, &values, control)?
149 }
150 _ => unreachable!("checked multirange declaration"),
151 };
152 finish(value, ty, input, control)
153}
154
155fn base_type(mut ty: &ColumnType) -> &ColumnType {
156 while let ColumnType::Domain { base, .. } = ty {
157 ty = base;
158 }
159 ty
160}
161
162fn finish(
163 value: Produced<Value>,
164 ty: Produced<ColumnType>,
165 input: &dyn EngineHook,
166 control: &ProductionControl<'_>,
167) -> Result<Produced<SQLParam>, SQLError> {
168 let value = if matches!(*ty, ColumnType::Domain { .. }) {
169 let source = base_type(&ty).catalog_name_with_control(control)?;
170 let target = ty.catalog_name_with_control(control)?;
171 crate::expr::cast_value_with_type_resolution_with_control(
172 &value,
173 Some(&source),
174 &target,
175 Some(input),
176 control,
177 )?
178 } else {
179 value
180 };
181 input::finish(value, ty, input, control)
182}