uqa_sql/assignment/
routines.rs1use crate::{ast::ColumnType, SQLError};
8use uqa_core::{
9 memory::{Produced, ProductionControl},
10 Value,
11};
12
13mod production;
14pub use production::coerce_routine_value_from_with_control;
15pub trait RoutineValueContext: super::AssignmentContext {
16 fn catalog_column_type(&self, name: &str) -> Option<ColumnType>;
17
18 fn catalog_column_type_with_control(
19 &self,
20 name: &str,
21 control: &ProductionControl<'_>,
22 ) -> Result<Option<Produced<ColumnType>>, SQLError> {
23 control.check()?;
24 self.catalog_column_type(name)
25 .map(|ty| ty.retain_external_with_control(control).map_err(Into::into))
26 .transpose()
27 }
28}
29pub fn coerce_routine_value(
30 context: &dyn RoutineValueContext,
31 value: &Value,
32 type_name: &str,
33) -> Result<Value, SQLError> {
34 coerce_routine_value_from(context, value, type_name, None)
35}
36
37pub fn coerce_routine_value_from(
38 context: &dyn RoutineValueContext,
39 value: &Value,
40 type_name: &str,
41 source: Option<&ColumnType>,
42) -> Result<Value, SQLError> {
43 let control = ProductionControl::uncontrolled();
44 coerce_routine_value_from_with_control(
45 context,
46 control.copy_value(value)?,
47 type_name,
48 source,
49 &control,
50 )?
51 .into_uncontrolled()
52 .map_err(|_| SQLError::Internal("ordinary routine coercion owner".into()))
53}
54
55#[derive(Debug)]
57pub struct RoutineValueDescription(RoutineValueKind);
58
59#[derive(Debug)]
60enum RoutineValueKind {
61 Record,
62 Trigger,
63 AnyArray,
64 RefCursor,
65 Void,
66 Catalog(ColumnType),
67}
68
69impl RoutineValueDescription {
70 pub fn resolve(context: &dyn RoutineValueContext, name: &str) -> Option<Self> {
72 Self::resolve_with_control(context, name, &ProductionControl::uncontrolled())
73 .expect("ordinary routine descriptor lookup")
74 .map(|description| {
75 description
76 .into_uncontrolled()
77 .expect("ordinary routine descriptor owner")
78 })
79 }
80
81 pub fn clone_with_control(
83 &self,
84 control: &uqa_core::memory::ProductionControl<'_>,
85 ) -> Result<uqa_core::memory::Produced<Self>, uqa_core::ValueRetentionError> {
86 let kind = match &self.0 {
87 RoutineValueKind::Record => RoutineValueKind::Record,
88 RoutineValueKind::Trigger => RoutineValueKind::Trigger,
89 RoutineValueKind::AnyArray => RoutineValueKind::AnyArray,
90 RoutineValueKind::RefCursor => RoutineValueKind::RefCursor,
91 RoutineValueKind::Void => RoutineValueKind::Void,
92 RoutineValueKind::Catalog(target) => {
93 let (target, memory) = target.clone_with_control(control)?.into_parts();
94 return control.finish(Self(RoutineValueKind::Catalog(target)), memory);
95 }
96 };
97 control.finish(Self(kind), control.empty_reservation())
98 }
99
100 pub fn coerce(
102 &self,
103 context: &dyn RoutineValueContext,
104 value: &Value,
105 source: Option<&ColumnType>,
106 ) -> Result<Value, SQLError> {
107 let control = ProductionControl::uncontrolled();
108 self.coerce_with_control(context, control.copy_value(value)?, source, &control)?
109 .into_uncontrolled()
110 .map_err(|_| SQLError::Internal("ordinary described coercion owner".into()))
111 }
112}
113
114#[cfg(test)]
115mod tests;