uqa_sql/semantics/rules/action_binding/
names.rs1use crate::SQLError;
10use uqa_core::memory::{BudgetedVec, OwnedMap, OwnedSet, Produced, ProductionControl};
11
12pub(super) type Names = OwnedSet<Produced<String>>;
13pub(super) type Named<T> = OwnedMap<Produced<String>, T>;
14
15pub(super) fn folded_cmp(name: &str, stored: &str) -> std::cmp::Ordering {
16 name.bytes()
17 .map(|byte| byte.to_ascii_lowercase())
18 .cmp(stored.bytes())
19}
20pub(super) fn folded(
21 name: &str,
22 control: &ProductionControl<'_>,
23) -> Result<Produced<String>, SQLError> {
24 let (mut text, memory) = control.copy_text(name)?.into_parts();
25 text.make_ascii_lowercase();
26 Ok(control.finish(text, memory)?)
27}
28pub(super) fn insert_name(
29 names: &mut Names,
30 name: &str,
31 lowercase: bool,
32 control: &ProductionControl<'_>,
33) -> Result<(), SQLError> {
34 control.check()?;
35 if if lowercase {
36 names.contains_by(|stored| folded_cmp(name, stored))
37 } else {
38 names.contains(name)
39 } {
40 return Ok(());
41 }
42 let name = if lowercase {
43 folded(name, control)?
44 } else {
45 control.copy_text(name)?
46 };
47 match control.budget() {
48 Some(budget) => {
49 names.insert_budgeted(name, budget)?;
50 }
51 None => {
52 names.insert(name);
53 }
54 }
55 Ok(())
56}
57pub(super) fn insert<T>(
58 names: &mut Named<T>,
59 name: &str,
60 value: T,
61 control: &ProductionControl<'_>,
62) -> Result<(), SQLError> {
63 let name = folded(name, control)?;
64 if let Some(previous) = names.get_mut(name.as_str()) {
65 *previous = value;
66 return Ok(());
67 }
68 match control.budget() {
69 Some(budget) => {
70 names.insert_budgeted(name, value, budget)?;
71 }
72 None => {
73 names.insert(name, value);
74 }
75 }
76 Ok(())
77}
78
79enum Buffer {
80 Ordinary(Vec<Produced<String>>),
81 Controlled(BudgetedVec<Produced<String>>),
82}
83pub(super) struct Columns(Buffer);
84impl Columns {
85 pub fn new(control: &ProductionControl<'_>) -> Self {
86 Self(control.budget().map_or_else(
87 || Buffer::Ordinary(Vec::new()),
88 |budget| Buffer::Controlled(BudgetedVec::new(budget)),
89 ))
90 }
91 pub fn copy(&self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
92 Self::from_names(self.iter().map(|name| name.as_str()), control)
93 }
94 pub fn from_names<'a>(
95 names: impl IntoIterator<Item = &'a str>,
96 control: &ProductionControl<'_>,
97 ) -> Result<Self, SQLError> {
98 let mut output = Self::new(control);
99 output.extend(names, control)?;
100 Ok(output)
101 }
102 pub fn from_produced(
103 names: Produced<Vec<String>>,
104 control: &ProductionControl<'_>,
105 ) -> Result<Self, SQLError> {
106 let mut output = Self::new(control);
107 let (names, mut memory) = names.into_parts();
108 for name in names {
109 let lease = memory.as_mut().map(|memory| memory.split(name.capacity()));
110 output.push_produced(control.finish(name, lease)?, control)?;
111 }
112 Ok(output)
113 }
114 pub fn push_produced(
115 &mut self,
116 name: Produced<String>,
117 control: &ProductionControl<'_>,
118 ) -> Result<(), SQLError> {
119 control.check()?;
120 match &mut self.0 {
121 Buffer::Ordinary(names) => names.push(name),
122 Buffer::Controlled(names) => names.push(name)?,
123 }
124 Ok(())
125 }
126 pub fn push(&mut self, name: &str, control: &ProductionControl<'_>) -> Result<(), SQLError> {
127 self.push_produced(control.copy_text(name)?, control)
128 }
129 pub fn extend<'a>(
130 &mut self,
131 names: impl IntoIterator<Item = &'a str>,
132 control: &ProductionControl<'_>,
133 ) -> Result<(), SQLError> {
134 for name in names {
135 self.push(name, control)?;
136 }
137 Ok(())
138 }
139 pub fn rename(
140 &mut self,
141 aliases: &[String],
142 control: &ProductionControl<'_>,
143 ) -> Result<(), SQLError> {
144 if self.is_empty() {
145 return self.extend(aliases.iter().map(String::as_str), control);
146 }
147 let columns = match &mut self.0 {
148 Buffer::Ordinary(names) => names.as_mut_slice(),
149 Buffer::Controlled(names) => &mut **names,
150 };
151 for (column, alias) in columns.iter_mut().zip(aliases) {
152 *column = control.copy_text(alias)?;
153 }
154 Ok(())
155 }
156}
157impl std::ops::Deref for Columns {
158 type Target = [Produced<String>];
159 fn deref(&self) -> &Self::Target {
160 match &self.0 {
161 Buffer::Ordinary(names) => names,
162 Buffer::Controlled(names) => names,
163 }
164 }
165}
166
167pub trait ActionColumns {
169 fn contains(&self, name: &str) -> bool;
170 fn contains_folded(&self, name: &str) -> bool;
171}
172impl ActionColumns for std::collections::BTreeSet<String> {
173 fn contains(&self, name: &str) -> bool {
174 Self::contains(self, name)
175 }
176 fn contains_folded(&self, name: &str) -> bool {
177 self.iter().any(|column| column.eq_ignore_ascii_case(name))
178 }
179}
180impl ActionColumns for Names {
181 fn contains(&self, name: &str) -> bool {
182 Self::contains(self, name)
183 }
184 fn contains_folded(&self, name: &str) -> bool {
185 self.iter().any(|column| column.eq_ignore_ascii_case(name))
186 }
187}
188
189impl ActionColumns for Vec<String> {
190 fn contains(&self, name: &str) -> bool {
191 self.iter().any(|column| column == name)
192 }
193 fn contains_folded(&self, name: &str) -> bool {
194 self.iter().any(|column| column.eq_ignore_ascii_case(name))
195 }
196}