1use super::{input, SQLParam};
10use crate::{expr::EngineHook, ColumnType, FunctionTypeResolver, SQLError};
11use uqa_core::{
12 memory::{Produced, ProductionControl},
13 ArrayValue, Value,
14};
15
16#[allow(clippy::too_many_arguments)]
18pub fn create_array_parameter_with_control(
19 type_name: &str,
20 dimensions: &[usize],
21 lower_bounds: &[i32],
22 elements: &[SQLParam],
23 types: &dyn FunctionTypeResolver,
24 input: &dyn EngineHook,
25 control: &ProductionControl<'_>,
26) -> Result<Produced<SQLParam>, SQLError> {
27 let ty = input::resolve_type(type_name, types, control)?;
28 let mut base = &*ty;
29 while let ColumnType::Domain { base: inner, .. } = base {
30 base = inner;
31 }
32 let element = base.array_element_type().ok_or_else(|| {
33 SQLError::TypeMismatch("array construction requires an array type".into())
34 })?;
35 let sources = input::bind_sources(elements, types, control)?;
36 let array = ArrayValue::from_row_major_with_control(
37 dimensions,
38 lower_bounds,
39 elements.len(),
40 control,
41 |index| {
42 input::cast_parameter(
43 &elements[index],
44 sources[index].as_ref(),
45 element,
46 input,
47 control,
48 )
49 },
50 )?
51 .ok_or_else(|| SQLError::Routine {
52 sqlstate: "22023".into(),
53 message: "invalid array dimensions, bounds or element count".into(),
54 })?;
55 let (array, memory) = array.into_parts();
56 let array = array.with_element_type_oid(Some(
57 crate::catalog::type_metadata::pg_type_oid(element) as u32,
58 ));
59 let value = control.finish(Value::Array(array), memory)?;
60 let value = if matches!(*ty, ColumnType::Domain { .. }) {
61 let source = base.catalog_name_with_control(control)?;
62 let target = ty.catalog_name_with_control(control)?;
63 crate::expr::cast_value_with_type_resolution_with_control(
64 &value,
65 Some(&source),
66 &target,
67 Some(input),
68 control,
69 )?
70 } else {
71 value
72 };
73 input::finish(value, ty, input, control)
74}