Creates a new `FunctionalDependencies` object from the given constraints.
(
constraints: Option<&Constraints>,
n_field: usize,
)
| 197 | |
| 198 | /// Creates a new `FunctionalDependencies` object from the given constraints. |
| 199 | pub fn new_from_constraints( |
| 200 | constraints: Option<&Constraints>, |
| 201 | n_field: usize, |
| 202 | ) -> Self { |
| 203 | if let Some(Constraints { inner: constraints }) = constraints { |
| 204 | // Construct dependency objects based on each individual constraint: |
| 205 | let dependencies = constraints |
| 206 | .iter() |
| 207 | .map(|constraint| { |
| 208 | // All the field indices are associated with the whole table |
| 209 | // since we are dealing with table level constraints: |
| 210 | let dependency = match constraint { |
| 211 | Constraint::PrimaryKey(indices) => FunctionalDependence::new( |
| 212 | indices.to_vec(), |
| 213 | (0..n_field).collect::<Vec<_>>(), |
| 214 | false, |
| 215 | ), |
| 216 | Constraint::Unique(indices) => FunctionalDependence::new( |
| 217 | indices.to_vec(), |
| 218 | (0..n_field).collect::<Vec<_>>(), |
| 219 | true, |
| 220 | ), |
| 221 | }; |
| 222 | // As primary keys are guaranteed to be unique, set the |
| 223 | // functional dependency mode to `Dependency::Single`: |
| 224 | dependency.with_mode(Dependency::Single) |
| 225 | }) |
| 226 | .collect::<Vec<_>>(); |
| 227 | Self::new(dependencies) |
| 228 | } else { |
| 229 | // There is no constraint, return an empty object: |
| 230 | Self::empty() |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | pub fn with_dependency(mut self, mode: Dependency) -> Self { |
| 235 | self.deps.iter_mut().for_each(|item| item.mode = mode); |