1414def _get_tier (table_name ):
1515 try :
1616 return next (tier for tier in user_relation_classes
17- if re .fullmatch (tier .tier_regexp , table_name ))
17+ if re .fullmatch (tier .tier_regexp , table_name . split ( '`' )[ - 2 ] ))
1818 except StopIteration :
1919 return None
2020
@@ -84,6 +84,25 @@ def from_sequence(cls, sequence):
8484 """
8585 return functools .reduce (lambda x , y : x + y , map (ERD , sequence ))
8686
87+ def add_parts (self ):
88+ """
89+ Adds to the diagram the part tables of tables already included in the diagram
90+ :return:
91+ """
92+ def is_part (part , master ):
93+ """
94+ :param part: `database`.`table_name`
95+ :param master: `database`.`table_name`
96+ :return: True if part is part of master,
97+ """
98+ part = [s .strip ('`' ) for s in part .split ('.' )]
99+ master = [s .strip ('`' ) for s in master .split ('.' )]
100+ return master [0 ] == part [0 ] and master [1 ] + '__' == part [1 ][:len (master [1 ])+ 2 ]
101+
102+ self = ERD (self ) # copy
103+ self .nodes_to_show .update (n for n in self .nodes () if any (is_part (n , m ) for m in self .nodes_to_show ))
104+ return self
105+
87106 def __add__ (self , arg ):
88107 """
89108 :param arg: either another ERD or a positive integer.
@@ -93,11 +112,14 @@ def __add__(self, arg):
93112 try :
94113 self .nodes_to_show .update (arg .nodes_to_show )
95114 except AttributeError :
96- for i in range (arg ):
97- new = nx .algorithms .boundary .node_boundary (self , self .nodes_to_show )
98- if not new :
99- break
100- self .nodes_to_show .update (new )
115+ try :
116+ self .nodes_to_show .add (arg .full_table_name )
117+ except AttributeError :
118+ for i in range (arg ):
119+ new = nx .algorithms .boundary .node_boundary (self , self .nodes_to_show )
120+ if not new :
121+ break
122+ self .nodes_to_show .update (new )
101123 return self
102124
103125 def __sub__ (self , arg ):
@@ -109,11 +131,14 @@ def __sub__(self, arg):
109131 try :
110132 self .nodes_to_show .difference_update (arg .nodes_to_show )
111133 except AttributeError :
112- for i in range (arg ):
113- new = nx .algorithms .boundary .node_boundary (nx .DiGraph (self ).reverse (), self .nodes_to_show )
114- if not new :
115- break
116- self .nodes_to_show .update (new )
134+ try :
135+ self .nodes_to_show .remove (arg .full_table_name )
136+ except AttributeError :
137+ for i in range (arg ):
138+ new = nx .algorithms .boundary .node_boundary (nx .DiGraph (self ).reverse (), self .nodes_to_show )
139+ if not new :
140+ break
141+ self .nodes_to_show .update (new )
117142 return self
118143
119144 def __mul__ (self , arg ):
@@ -131,7 +156,7 @@ def _make_graph(self, context):
131156 Make the self.graph - a graph object ready for drawing
132157 """
133158 graph = nx .DiGraph (self ).subgraph (self .nodes_to_show )
134- nx .set_node_attributes (graph , 'node_type' , {n : _get_tier (n . split ( '`' )[ - 2 ] ) for n in graph })
159+ nx .set_node_attributes (graph , 'node_type' , {n : _get_tier (n ) for n in graph })
135160 # relabel nodes to class names
136161 mapping = {node : lookup_class_name (node , context ) for node in graph .nodes ()}
137162 new_names = [mapping .values ()]
0 commit comments