database_reflection/reflection/
constraint.rs1use crate::metadata::WithMetadata;
2use crate::reflection::column::Column;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::slice::Iter;
6use std::sync::Arc;
7
8#[derive(Clone, Debug, PartialEq)]
9pub enum ConstraintSide {
10 Local,
11 Foreign,
12}
13
14#[derive(Clone, Default, Debug, Serialize, Deserialize)]
15pub struct ConstraintKeyPair {
16 pub local: Arc<Column>,
17 pub foreign: Arc<Column>,
18}
19
20#[derive(Clone, Default, Debug, Serialize, Deserialize)]
21pub struct Constraint {
22 name: Arc<String>,
23 key_pairs: Vec<ConstraintKeyPair>,
24 metadata: HashMap<String, String>,
25}
26
27impl WithMetadata for Constraint {
28 fn get_metadata(&self) -> &HashMap<String, String> {
30 &self.metadata
31 }
32
33 fn get_metadata_mut(&mut self) -> &mut HashMap<String, String> {
35 &mut self.metadata
36 }
37}
38
39impl Constraint {
40 pub fn new(name: impl ToString, local: Arc<Column>, foreign: Arc<Column>) -> Self {
42 Constraint {
43 name: Arc::new(name.to_string()),
44 key_pairs: vec![ConstraintKeyPair { local, foreign }],
45 ..Default::default()
46 }
47 }
48
49 pub fn name(&self) -> Arc<String> {
51 self.name.clone()
52 }
53
54 pub fn local(&self) -> &Column {
56 &self.key_pairs.first().unwrap().local
57 }
58
59 pub fn foreign(&self) -> &Column {
61 &self.key_pairs.first().unwrap().foreign
62 }
63
64 pub fn add_key_pair(&mut self, local: Arc<Column>, foreign: Arc<Column>) -> &mut Constraint {
66 self.key_pairs.push(ConstraintKeyPair { local, foreign });
67
68 self
69 }
70
71 pub fn key_pairs(&self) -> Iter<'_, ConstraintKeyPair> {
73 self.key_pairs.iter()
74 }
75
76 pub fn key_pairs_count(&self) -> usize {
78 self.key_pairs.len()
79 }
80}