1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::collections::HashSet;
use crate::{
    sqlite::{
        schema::{
            table::Table,
            field::Field,
            constraint::{
                Constraint,
                ConstraintType,
            },
        },
        types::to_sql_type,
    },
    graphmigrate::Comparison,
    utils::Tokens,
};
use super::{
    utils::{
        SqliteNodeData,
        SqliteMigrateCtx,
        SqliteNodeDataDispatch,
    },
    Node,
    GraphId,
};

#[derive(Clone)]
pub struct NodeTable_ {
    pub def: Table,
    pub fields: Vec<Field>,
    pub constraints: Vec<Constraint>,
}

impl NodeTable_ {
    pub fn compare(&self, old: &Self, _created: &HashSet<GraphId>) -> Comparison {
        if old.def.id != self.def.id {
            Comparison::Update
        } else {
            Comparison::DoNothing
        }
    }
}

impl SqliteNodeData for NodeTable_ {
    fn update(&self, ctx: &mut SqliteMigrateCtx, old: &Self) {
        if old.def.id != self.def.id {
            let mut stmt = Tokens::new();
            stmt.s("alter table").id(&old.def.id).s("rename to").id(&self.def.id);
            ctx.statements.push(stmt.to_string());
        }
    }
}

impl SqliteNodeDataDispatch for NodeTable_ {
    fn create_coalesce(&mut self, other: Node) -> Option<Node> {
        match other {
            Node::Field(f) if f.def.table == self.def => {
                self.fields.push(f.def.clone());
                None
            },
            Node::Constraint(c) if c.def.table == self.def => {
                self.constraints.push(c.def.clone());
                None
            },
            other => Some(other),
        }
    }

    fn delete_coalesce(&mut self, other: Node) -> Option<Node> {
        match other {
            Node::Field(f) if f.def.table == self.def => None,
            Node::Constraint(e) if e.def.table == self.def => None,
            Node::Index(e) if e.def.table == self.def => None,
            other => Some(other),
        }
    }

    fn create(&self, ctx: &mut SqliteMigrateCtx) {
        let mut stmt = Tokens::new();
        stmt.s("create table").id(&self.def.id).s("(");
        let mut i = 0usize;
        for f in &self.fields {
            if f.id == "rowid" {
                continue;
            }
            if i > 0 {
                stmt.s(",");
            }
            i += 1;
            stmt.id(&f.id).s(to_sql_type(&f.0.type_.type_.type_.type_));
            if !f.type_.type_.opt {
                stmt.s("not null");
            }
        }
        for c in &self.constraints {
            if i > 0 {
                stmt.s(",");
            }
            i += 1;
            stmt.s("constraint").id(&c.id);
            match &c.type_ {
                ConstraintType::PrimaryKey(x) => {
                    stmt.s("primary key (").f(|t| {
                        for (i, field) in x.fields.iter().enumerate() {
                            if i > 0 {
                                t.s(",");
                            }
                            t.id(&field.id);
                        }
                    }).s(")");
                },
                ConstraintType::ForeignKey(x) => {
                    stmt.s("foreign key (").f(|t| {
                        for (i, pair) in x.fields.iter().enumerate() {
                            if i > 0 {
                                t.s(",");
                            }
                            t.id(&pair.0.id);
                        }
                    }).s(") references ").f(|t| {
                        for (i, pair) in x.fields.iter().enumerate() {
                            if i == 0 {
                                t.id(&pair.1.table.id).s("(");
                            } else {
                                t.s(",");
                            }
                            t.id(&pair.1.id);
                        }
                    }).s(")");
                },
            }
        }
        stmt.s(")");
        ctx.statements.push(stmt.to_string());
    }

    fn delete(&self, ctx: &mut SqliteMigrateCtx) {
        ctx.statements.push(Tokens::new().s("drop table").id(&self.def.id).to_string());
    }
}