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
use crate::postgres::{def::*, query::TableConstraintsQueryResult};
use crate::Name;
pub struct TableConstraintsQueryResultParser {
curr: Option<TableConstraintsQueryResult>,
results: Box<dyn Iterator<Item = TableConstraintsQueryResult>>,
}
pub fn parse_table_constraint_query_results(
results: Box<dyn Iterator<Item = TableConstraintsQueryResult>>,
) -> impl Iterator<Item = Constraint> {
TableConstraintsQueryResultParser {
curr: None,
results,
}
}
impl Iterator for TableConstraintsQueryResultParser {
type Item = Constraint;
fn next(&mut self) -> Option<Self::Item> {
let result = if let Some(result) = self.curr.take() {
result
} else {
self.results.next()?
};
let constraint_name = result.constraint_name;
match result.constraint_type.as_str() {
"CHECK" => {
Some(Constraint::Check(Check {
name: constraint_name,
expr: result.check_clause.unwrap(),
no_inherit: false,
}))
}
"FOREIGN KEY" => {
let mut columns = Vec::new();
let mut foreign_columns = Vec::new();
columns.push(result.column_name.unwrap());
let table = result.referential_key_table_name.unwrap();
foreign_columns.push(result.referential_key_column_name.unwrap());
let on_update =
ForeignKeyAction::from_str(&result.update_rule.clone().unwrap_or_default());
let on_delete =
ForeignKeyAction::from_str(&result.delete_rule.clone().unwrap_or_default());
for result in self.results.by_ref() {
if result.constraint_name != constraint_name {
self.curr = Some(result);
return Some(Constraint::References(References {
name: constraint_name,
columns,
table,
foreign_columns,
on_update,
on_delete,
}));
}
if result.column_name.is_some() && result.referential_key_column_name.is_some()
{
columns.push(result.column_name.unwrap());
foreign_columns.push(result.referential_key_column_name.unwrap());
}
}
Some(Constraint::References(References {
name: constraint_name,
columns,
table,
foreign_columns,
on_update,
on_delete,
}))
}
"PRIMARY KEY" => {
let mut columns = vec![result.column_name.unwrap()];
for result in self.results.by_ref() {
if result.constraint_name != constraint_name {
self.curr = Some(result);
return Some(Constraint::PrimaryKey(PrimaryKey {
name: constraint_name,
columns,
}));
}
columns.push(result.column_name.unwrap());
}
Some(Constraint::PrimaryKey(PrimaryKey {
name: constraint_name,
columns,
}))
}
"UNIQUE" => {
let mut columns = vec![result.column_name.unwrap()];
for result in self.results.by_ref() {
if result.constraint_name != constraint_name {
self.curr = Some(result);
return Some(Constraint::Unique(Unique {
name: constraint_name,
columns,
}));
}
columns.push(result.column_name.unwrap());
}
Some(Constraint::Unique(Unique {
name: constraint_name,
columns,
}))
}
_ => {
None
}
}
}
}