1use super::{input, SQLParam};
10use crate::{expr::EngineHook, ColumnType, FunctionTypeResolver, SQLError};
11use uqa_core::memory::{Produced, ProductionControl, ProductionVec};
12
13#[cfg(test)]
14mod tests;
15
16pub fn create_record_parameter_with_control(
18 type_name: &str,
19 fields: &[SQLParam],
20 types: &dyn FunctionTypeResolver,
21 input: &dyn EngineHook,
22 control: &ProductionControl<'_>,
23) -> Result<Produced<SQLParam>, SQLError> {
24 let ty = input::resolve_type(type_name, types, control)?;
25 let mut base = &*ty;
26 while let ColumnType::Domain { base: inner, .. } = base {
27 base = inner;
28 }
29 if matches!(base, ColumnType::Record) {
30 let sources = input::bind_sources(fields, types, control)?;
31 return input::finish(anonymous(fields, &sources, control)?, ty, input, control);
32 }
33 let ColumnType::Composite(reference) = base else {
34 return Err(SQLError::TypeMismatch(
35 "record construction requires a named composite type".into(),
36 ));
37 };
38 let descriptor = crate::expr::composites::descriptor(input.composite_types(), reference.oid)?;
39 let sources = input::bind_sources(fields, types, control)?;
40 if fields.len() != descriptor.attributes.len() {
41 return Err(SQLError::Diagnostic {
42 sqlstate: "42846".into(),
43 message: format!("cannot cast type record to {}", base.display_name()),
44 detail: Some(
45 if fields.len() < descriptor.attributes.len() {
46 "Input has too few columns."
47 } else {
48 "Input has too many columns."
49 }
50 .into(),
51 ),
52 hint: None,
53 });
54 }
55 let value = crate::expr::composites::constructor::construct_current_with_control(
56 &descriptor,
57 input.composite_types(),
58 control,
59 |index| {
60 input::cast_parameter(
61 &fields[index],
62 sources[index].as_ref(),
63 &descriptor.attributes[index].ty,
64 input,
65 control,
66 )
67 },
68 )?;
69 let value = if matches!(*ty, ColumnType::Domain { .. }) {
70 let source = base.catalog_name_with_control(control)?;
71 let target = ty.catalog_name_with_control(control)?;
72 crate::expr::cast_value_with_type_resolution_with_control(
73 &value,
74 Some(&source),
75 &target,
76 Some(input),
77 control,
78 )?
79 } else {
80 value
81 };
82 input::finish(value, ty, input, control)
83}
84
85fn anonymous(
86 fields: &[SQLParam],
87 sources: &[Option<ColumnType>],
88 control: &ProductionControl<'_>,
89) -> Result<Produced<uqa_core::Value>, SQLError> {
90 use crate::catalog::type_metadata::{pg_type_collation_oid, pg_type_modifier, pg_type_oid};
91 let mut values = ProductionVec::new(*control);
92 let mut metadata = ProductionVec::new(*control);
93 values.reserve(fields.len())?;
94 metadata.reserve(fields.len())?;
95 for (field, source) in fields.iter().zip(sources) {
96 values.push_produced(field.to_value_with_control(control)?)?;
97 metadata.push_copy(uqa_core::RecordFieldType {
98 oid: source.as_ref().map_or(705, |ty| pg_type_oid(ty) as u32),
99 type_modifier: source.as_ref().map_or(-1, |ty| pg_type_modifier(ty) as i32),
100 collation_oid: Some(
101 source
102 .as_ref()
103 .map_or(0, |ty| pg_type_collation_oid(ty) as u32),
104 ),
105 })?;
106 }
107 let (row, memory) =
108 uqa_core::RowValue::typed_with_control(values.finish()?, metadata.finish()?, control)?
109 .into_parts();
110 Ok(control.finish(uqa_core::Value::Row(row), memory)?)
111}