File size: 4,717 Bytes
ae01f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################

from pgadmin.browser.server_groups.servers.databases.schemas.tables.utils \
    import BaseTableView
from pgadmin.browser.server_groups.servers.databases.schemas.utils \
    import get_schemas
from pgadmin.browser.server_groups.servers.databases.schemas.utils \
    import DataTypeReader
from pgadmin.utils.preferences import Preferences


class ERDTableView(BaseTableView, DataTypeReader):
    def __init__(self):
        super().__init__(cmd='erd')

    @BaseTableView.check_precondition
    def sql(self, conn_id=None, did=None, sid=None, data={}, with_drop=False):
        return BaseTableView.get_sql(self, did, None, None, data, None,
                                     add_not_exists_clause=True,
                                     with_drop=with_drop)

    @BaseTableView.check_precondition
    def get_types(self, conn_id=None, did=None, sid=None):
        condition = self.get_types_condition_sql(False)
        return DataTypeReader.get_types(self, self.conn, condition, True)

    @BaseTableView.check_precondition
    def fetch_all_tables(self, did=None, sid=None, scid=None):
        all_tables = []
        schemas = {'rows': []}
        if scid is None:
            status, schemas = get_schemas(self.conn, show_system_objects=False)
            if not status:
                return status, schemas
        else:
            schemas['rows'].append({'oid': scid})

        for row in schemas['rows']:
            status, res = \
                BaseTableView.fetch_tables(self, sid, did, row['oid'],
                                           with_serial_cols=True)
            if not status:
                return status, res

            all_tables.extend(res.values())

        return True, all_tables

    @BaseTableView.check_precondition
    def traverse_related_tables(self, did=None, sid=None, scid=None,
                                tid=None, related={}, maxdepth=0, currdepth=0):

        status, res = \
            BaseTableView.fetch_tables(self, sid, did, scid, tid=tid,
                                       with_serial_cols=True)

        if not status:
            return status, res

        related[tid] = res
        # Max depth limit reached
        if currdepth == maxdepth:
            new_fks = []
            for fk in related[tid].pop('foreign_key', []):
                if fk['confrelid'] in related:
                    new_fks.append(fk)

            related[tid]['foreign_key'] = new_fks
            return True, None

        status, depending_res = BaseTableView.get_fk_ref_tables(
            self, tid)

        if not status:
            return status, depending_res

        for fk in [*res.get('foreign_key', []), *depending_res]:
            if fk['confrelid'] in related:
                continue
            status, res = self.traverse_related_tables(
                did=did, sid=sid, scid=fk['refnspoid'], tid=fk['confrelid'],
                related=related, maxdepth=maxdepth, currdepth=currdepth + 1)
            if not status:
                return status, res

        return True, None


class ERDHelper:
    def __init__(self, conn_id, sid, did):
        self.conn_id = conn_id
        self.did = did
        self.sid = sid
        self.table_view = ERDTableView()
        self.link_view = None

    def get_types(self):
        return self.table_view.get_types(
            conn_id=self.conn_id, did=self.did, sid=self.sid)

    def get_table_sql(self, data, with_drop=False):
        SQL, _ = self.table_view.sql(
            conn_id=self.conn_id, did=self.did, sid=self.sid,
            data=data, with_drop=with_drop)
        return SQL

    def get_all_tables(self, scid, tid):
        if tid is None and scid is None:
            status, res = self.table_view.fetch_all_tables(
                did=self.did, sid=self.sid)
        elif tid is None:
            status, res = self.table_view.fetch_all_tables(
                did=self.did, sid=self.sid, scid=scid)
        else:
            prefs = Preferences.module('erd')
            table_relation_depth = prefs.preference('table_relation_depth')
            related = {}
            status, res = self.table_view.traverse_related_tables(
                did=self.did, sid=self.sid, scid=scid, tid=tid,
                related=related, maxdepth=table_relation_depth.get()
            )
            if status:
                res = list(related.values())
        return status, res