forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_schema.rs
More file actions
223 lines (198 loc) · 5.85 KB
/
Copy pathprint_schema.rs
File metadata and controls
223 lines (198 loc) · 5.85 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use diesel_infer_schema::*;
use std::error::Error;
use std::fmt::{self, Display, Formatter, Write};
pub enum Filtering {
Whitelist(Vec<TableName>),
Blacklist(Vec<TableName>),
None,
}
impl Filtering {
pub fn should_ignore_table(&self, name: &TableName) -> bool {
use self::Filtering::*;
match *self {
Whitelist(ref names) => !names.contains(name),
Blacklist(ref names) => names.contains(name),
None => false,
}
}
}
pub fn run_print_schema(
database_url: &str,
schema_name: Option<&str>,
filtering: &Filtering,
include_docs: bool,
) -> Result<(), Box<Error>> {
let table_names = load_table_names(database_url, schema_name)?
.into_iter()
.filter(|t| !filtering.should_ignore_table(t))
.collect::<Vec<_>>();
let foreign_keys = load_foreign_key_constraints(database_url, schema_name)?;
let foreign_keys = remove_unsafe_foreign_keys_for_codegen(
database_url,
&foreign_keys,
&table_names,
);
let table_data = table_names.into_iter()
.map(|t| load_table_data(database_url, t))
.collect::<Result<_, Box<Error>>>()?;
let definitions = TableDefinitions {
tables: table_data,
fk_constraints: foreign_keys,
include_docs,
};
if let Some(schema_name) = schema_name {
print!("{}", ModuleDefinition(schema_name, definitions));
} else {
print!("{}", definitions);
}
Ok(())
}
struct ModuleDefinition<'a>(&'a str, TableDefinitions);
impl<'a> Display for ModuleDefinition<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
{
let mut out = PadAdapter::new(f);
writeln!(out, "pub mod {} {{", self.0)?;
write!(out, "{}", self.1)?;
}
writeln!(f, "}}")?;
Ok(())
}
}
struct TableDefinitions {
tables: Vec<TableData>,
fk_constraints: Vec<ForeignKeyConstraint>,
include_docs: bool,
}
impl Display for TableDefinitions {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut is_first = true;
for table in &self.tables {
if is_first {
is_first = false;
} else {
write!(f, "\n")?;
}
writeln!(f, "{}",
TableDefinition {
table,
include_docs: self.include_docs,
}
)?;
}
if !self.fk_constraints.is_empty() {
write!(f, "\n")?;
}
for foreign_key in &self.fk_constraints {
writeln!(f, "{}", Joinable(foreign_key))?;
}
Ok(())
}
}
struct TableDefinition<'a> {
table: &'a TableData,
include_docs: bool,
}
impl<'a> Display for TableDefinition<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "table! {{")?;
{
let mut out = PadAdapter::new(f);
write!(out, "\n")?;
if self.include_docs {
for d in self.table.docs.lines() {
writeln!(out, "///{}{}", if d.is_empty() { "" } else { " " }, d)?;
}
}
write!(out, "{} (", self.table.name)?;
for (i, pk) in self.table.primary_key.iter().enumerate() {
if i != 0 {
write!(out, ", ")?;
}
write!(out, "{}", pk)?;
}
write!(out, ") {}",
ColumnDefinitions {
columns: &self.table.column_data,
include_docs: self.include_docs,
}
)?;
}
write!(f, "}}")?;
Ok(())
}
}
struct ColumnDefinitions<'a> {
columns: &'a [ColumnDefinition],
include_docs: bool,
}
impl<'a> Display for ColumnDefinitions<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
{
let mut out = PadAdapter::new(f);
writeln!(out, "{{")?;
for column in self.columns {
if self.include_docs {
for d in column.docs.lines() {
writeln!(out, "///{}{}", if d.is_empty() { "" } else { " " }, d)?;
}
}
if let Some(ref rust_name) = column.rust_name {
writeln!(out, r#"#[sql_name = {}]"#, column.sql_name)?;
writeln!(out, "{} -> {},", rust_name, column.ty)?;
} else {
writeln!(out, "{} -> {},", column.sql_name, column.ty)?;
}
}
}
writeln!(f, "}}")?;
Ok(())
}
}
struct Joinable<'a>(&'a ForeignKeyConstraint);
impl<'a> Display for Joinable<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"joinable!({} -> {} ({}));",
self.0.child_table,
self.0.parent_table,
self.0.foreign_key,
)
}
}
/// Lifted directly from libcore/fmt/builders.rs
struct PadAdapter<'a, 'b: 'a> {
fmt: &'a mut Formatter<'b>,
on_newline: bool,
}
impl<'a, 'b: 'a> PadAdapter<'a, 'b> {
fn new(fmt: &'a mut Formatter<'b>) -> PadAdapter<'a, 'b> {
PadAdapter {
fmt: fmt,
on_newline: false,
}
}
}
impl<'a, 'b: 'a> Write for PadAdapter<'a, 'b> {
fn write_str(&mut self, mut s: &str) -> fmt::Result {
while !s.is_empty() {
if self.on_newline {
self.fmt.write_str(" ")?;
}
let split = match s.find('\n') {
Some(pos) => {
self.on_newline = true;
pos + 1
}
None => {
self.on_newline = false;
s.len()
}
};
self.fmt.write_str(&s[..split])?;
s = &s[split..];
}
Ok(())
}
}