good_ormning/sqlite/schema/
constraint.rs1use std::{
2 rc::Rc,
3 ops::Deref,
4 fmt::Display,
5};
6use super::{
7 table::{
8 Table,
9 },
10 field::Field,
11};
12
13#[derive(Clone, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
14pub struct SchemaConstraintId(pub String);
15
16impl Display for SchemaConstraintId {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 Display::fmt(&self.0, f)
19 }
20}
21
22#[derive(Clone, PartialEq)]
23pub struct PrimaryKeyDef {
24 pub fields: Vec<Field>,
25}
26
27#[derive(Clone, PartialEq)]
28pub struct ForeignKeyDef {
29 pub fields: Vec<(Field, Field)>,
30}
31
32#[derive(Clone, PartialEq)]
33pub enum ConstraintType {
34 PrimaryKey(PrimaryKeyDef),
35 ForeignKey(ForeignKeyDef),
36}
37
38pub struct Constraint_ {
39 pub table: Table,
40 pub schema_id: SchemaConstraintId,
41 pub id: String,
42 pub type_: ConstraintType,
43}
44
45#[derive(Clone)]
46pub struct Constraint(pub Rc<Constraint_>);
47
48impl Display for Constraint {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 Display::fmt(
51 &format!("{}.{} ({}.{})", self.0.table.id, self.0.id, self.0.table.schema_id, self.0.schema_id),
52 f,
53 )
54 }
55}
56
57impl PartialEq for Constraint {
58 fn eq(&self, other: &Self) -> bool {
59 self.table == other.table && self.schema_id == other.schema_id
60 }
61}
62
63impl Eq for Constraint { }
64
65impl Deref for Constraint {
66 type Target = Constraint_;
67
68 fn deref(&self) -> &Self::Target {
69 self.0.as_ref()
70 }
71}