uqa_sql/semantics/
bound_columns.rs1use crate::{RowSchema, SQLError};
10
11pub fn bound_source_schema(
12 schema: &RowSchema,
13 bound: Option<&[String]>,
14) -> Result<RowSchema, SQLError> {
15 bound_source_schema_with_control(
16 schema,
17 bound,
18 &uqa_core::memory::ProductionControl::uncontrolled(),
19 )
20}
21
22pub fn bound_source_schema_with_control(
23 schema: &RowSchema,
24 bound: Option<&[String]>,
25 control: &uqa_core::memory::ProductionControl<'_>,
26) -> Result<RowSchema, SQLError> {
27 control.check()?;
28 let Some(bound) = bound else {
29 return Ok(schema.clone());
30 };
31 for name in bound {
32 control.check()?;
33 if !schema.columns().contains(name) {
34 return Err(SQLError::UnknownColumn(
35 control.copy_text(name)?.into_parts().0,
36 ));
37 }
38 }
39 RowSchema::select_with_control(
40 schema,
41 bound.iter().map(|name| (name.as_str(), name.as_str())),
42 control,
43 )
44}
45
46pub fn bound_source_column_names(
47 current: Vec<String>,
48 bound: Option<&[String]>,
49) -> Result<Vec<String>, SQLError> {
50 let Some(bound) = bound else {
51 return Ok(current);
52 };
53 bound_source_column_names_with_control(
54 current.iter().map(String::as_str),
55 Some(bound),
56 &uqa_core::memory::ProductionControl::uncontrolled(),
57 )?
58 .into_uncontrolled()
59 .map_err(|_| unreachable!("ordinary source column selection"))
60}
61
62pub fn bound_source_column_names_with_control<'a>(
64 current: impl IntoIterator<Item = &'a str>,
65 bound: Option<&[String]>,
66 control: &uqa_core::memory::ProductionControl<'_>,
67) -> Result<uqa_core::memory::Produced<Vec<String>>, SQLError> {
68 use uqa_core::memory::ProductionVec;
69 let mut output = ProductionVec::new(*control);
70 let Some(bound) = bound else {
71 for name in current {
72 output.push_produced(control.copy_text(name)?)?;
73 }
74 return Ok(output.finish()?);
75 };
76 let mut declared = ProductionVec::new(*control);
77 for name in current {
78 declared.push_copy(name)?;
79 }
80 for name in bound {
81 control.check()?;
82 if !declared.contains(&name.as_str()) {
83 return Err(SQLError::UnknownColumn(name.clone()));
84 }
85 }
86 for name in bound {
87 output.push_produced(control.copy_text(name)?)?;
88 }
89 Ok(output.finish()?)
90}