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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Loader for postgresql.

// This is not production code, so while we should get rid of these lints, its not
// worth the time it would take, for no return value.
#![allow(clippy::unwrap_used)]
#![allow(clippy::indexing_slicing)]

use std::{
    cell::RefCell,
    collections::HashMap,
    convert::{TryFrom, TryInto},
};

use anyhow::Result;
use itertools::Itertools;
use postgres::{tls::NoTls, Client, Row};

use crate::{
    opts,
    schema::{Index, Relation, Schema, Table, TableColumn},
};

/// Struct that manages the loading and implements `Loader` trait.
pub struct Conn {
    /// Postgres client
    pg_client: RefCell<Client>,
    /// Schema name
    schema: String,
    /// Options
    opts: opts::Cli,
}

/// Check if a column is a primary key
fn is_primary_key(table: &str, column: &str, indexes: &[Index]) -> bool {
    indexes
        .iter()
        .any(|idx| idx.table == table && idx.fields.contains(&column.to_string()) && idx.primary)
}

impl Conn {
    /// Make a new postgres connection
    pub(crate) fn new(opts: &opts::Cli) -> Result<Conn> {
        let pg_client = postgres::Config::new()
            .user(&opts.pg_opts.username)
            .password(&opts.pg_opts.password)
            .dbname(&opts.pg_opts.database)
            .host(&opts.pg_opts.hostname)
            .connect(NoTls)?;

        let pg_client = RefCell::new(pg_client);
        let schema = opts.pg_opts.schema.clone();
        Ok(Conn {
            pg_client,
            schema,
            opts: opts.clone(),
        })
    }

    /// Do we include this table name?
    fn include_table(&self, name: &String) -> bool {
        match &self.opts.include_tables {
            Some(inc) => inc.contains(name),
            None => true,
        }
    }

    /// Do we exclude this table name?
    fn exclude_table(&self, name: &String) -> bool {
        match &self.opts.exclude_tables {
            Some(inc) => inc.contains(name),
            None => false,
        }
    }

    /// Load the schema
    pub(crate) fn load(&self) -> Result<Schema> {
        let mut client = self.pg_client.borrow_mut();
        let tables_rows = client.query(tables_query(), &[&self.schema])?;
        let relations_rows = client.query(relations_query(), &[&self.schema])?;
        let index_rows = client.query(index_query(), &[])?;

        let mut partial_tables: HashMap<String, Vec<String>> = HashMap::new();

        let indexes: Vec<_> = index_rows
            .into_iter()
            .filter(|row| {
                let row_name: String = row.get(0);
                self.include_table(&row_name) && !self.exclude_table(&row_name)
            })
            .map(|row| {
                let idx: Index = row.try_into().unwrap();
                idx
            })
            .collect();

        let tables: Vec<_> = tables_rows
            .into_iter()
            .group_by(|row| row.get(0))
            .into_iter()
            .filter(|(name, _rows)| self.include_table(name) && !self.exclude_table(name))
            .map(|(name, rows)| {
                let fields: Vec<_> = rows
                    .into_iter()
                    .map(|row| {
                        let mut field: TableColumn = row.try_into().unwrap();
                        field.primary_key = is_primary_key(&name, &field.column, &indexes);

                        let desc = match field.description {
                            Some(desc) => {
                                match self.opts.column_description_wrap {
                                    Some(wrap) => Some(textwrap::fill(&desc, wrap)),
                                    None => Some(desc),
                                }
                            },
                            None => None,
                        };
                        field.description = desc;

                        field
                    })
                    .collect();

                let desc = match &fields[0].table_description {
                    Some(desc) => {
                        match self.opts.table_description_wrap {
                            Some(wrap) => Some(textwrap::fill(desc, wrap)),
                            None => Some(desc).cloned(),
                        }
                    },
                    None => None,
                };

                Table {
                    name,
                    description: desc,
                    fields,
                }
            })
            .collect();

        let relations: Vec<_> = relations_rows
            .into_iter()
            .map(|row| {
                let relation: Relation = row.try_into().unwrap();
                relation
            })
            .filter(|relation| {
                if self.include_table(&relation.on_table)
                    && !self.exclude_table(&relation.on_table)
                    && !self.exclude_table(&relation.to_table)
                {
                    if !self.include_table(&relation.to_table) {
                        match partial_tables.get_mut(&relation.to_table) {
                            Some(value) => {
                                if !value.contains(&relation.to_field) {
                                    value.push(relation.to_field.clone());
                                }
                            },
                            None => {
                                partial_tables.insert(relation.to_table.clone(), vec![relation
                                    .to_field
                                    .clone()]);
                            },
                        }
                    }
                    true
                } else {
                    false
                }
            })
            .collect();

        Ok(Schema {
            tables,
            relations,
            partial_tables,
        })
    }
}

impl TryFrom<Row> for Index {
    type Error = String;

    fn try_from(row: Row) -> std::result::Result<Self, String> {
        let all_fields: String = row.get(4);
        let braces: &[_] = &['{', '}'];

        let fields: Vec<_> = all_fields
            .trim_matches(braces)
            .split(',')
            .map(std::string::ToString::to_string)
            .collect();

        Ok(Self {
            table: row.get(0),
            // name: row.get(1),
            primary: row.get(2),
            // unique: row.get(3),
            fields,
        })
    }
}

impl TryFrom<Row> for Relation {
    type Error = String;

    fn try_from(row: Row) -> std::result::Result<Self, String> {
        let fields: HashMap<String, String> = row
            .columns()
            .iter()
            .enumerate()
            .map(|(i, c)| (c.name().to_string(), row.get(i)))
            .collect();

        Ok(Self {
            on_table: fetch_field(&fields, "on_table")?,
            on_field: fetch_field(&fields, "on_field")?,
            to_table: fetch_field(&fields, "to_table")?,
            to_field: fetch_field(&fields, "to_field")?,
        })
    }
}

impl TryFrom<Row> for TableColumn {
    type Error = String;

    fn try_from(row: Row) -> std::result::Result<Self, String> {
        Ok(Self {
            column: row.get(1),
            data_type: row.get(2),
            index: row.get(3),
            default: row.get(4),
            nullable: row.get(5),
            max_chars: row.get(6),
            description: row.get(7),
            table_description: row.get(8),
            primary_key: false,
        })
    }
}

/// Fetch a field from a hashmap
fn fetch_field(map: &HashMap<String, String>, key: &str) -> std::result::Result<String, String> {
    map.get(key)
        .cloned()
        .ok_or(format!("could not find field {key}"))
}

/// Query all tables and columns
fn tables_query() -> &'static str {
    "
    select table_name, column_name, data_type, ordinal_position, column_default, is_nullable, character_maximum_length, col_description(table_name::regclass, ordinal_position), obj_description(table_name::regclass)
      from information_schema.columns
     where table_schema = $1
     order by table_name, ordinal_position
    "
}

/// Query all relationships
fn relations_query() -> &'static str {
    "
    select *
      from (
        select ns.nspname AS schemaname,
               cl.relname AS on_table,
               attr.attname AS on_field,
               clf.relname AS to_table,
               attrf.attname AS to_field
          from pg_constraint con
                 join pg_class cl
                     on con.conrelid = cl.oid
                 join pg_namespace ns
                     on cl.relnamespace = ns.oid
                 join pg_class clf
                     on con.confrelid = clf.oid
                 join pg_attribute attr
                     on attr.attnum = ANY(con.conkey) and
                 attr.attrelid = con.conrelid
                 join pg_attribute attrf
                     on attrf.attnum = ANY(con.confkey) and
                 attrf.attrelid = con.confrelid
      ) as fk
     where fk.schemaname = $1
    "
}

/// Query all indexes
fn index_query() -> &'static str {
    "
SELECT
    CAST(idx.indrelid::regclass as varchar) as table_name,
    i.relname as index_name,
    idx.indisprimary as primary_key,
    idx.indisunique as unique,
    CAST(
        ARRAY(
            SELECT pg_get_indexdef(idx.indexrelid, k + 1, true)
            FROM generate_subscripts(idx.indkey, 1) as k
            ORDER BY k
        ) as varchar
    ) as columns
FROM   pg_index as idx
JOIN   pg_class as i
ON     i.oid = idx.indexrelid
JOIN   pg_am as am
ON     i.relam = am.oid
JOIN   pg_namespace as ns
ON     ns.oid = i.relnamespace
AND    ns.nspname = ANY(current_schemas(false))
ORDER BY idx.indrelid
"
}