blib/lib/UR/Namespace/Command/Update/SchemaDiagram.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 168 SCAR 60.4
CC
20
Cov
0%
execute
306.0
_get_related_items
20.0
sub_command_sort_position
2.0
CRAP 420.0
1  
2 package UR::Namespace::Command::Update::SchemaDiagram;
3  
4  
5  
6 use strict;
7 use warnings;
8 use UR;
9 our $VERSION = "0.46"; # UR $VERSION;
10  
11 UR::Object::Type->define(
12 class_name => __PACKAGE__,
13 is => 'UR::Namespace::Command::Base',
14 has => [
15 data_source => {type => 'String', doc => 'Which datasource to use', is_optional => 1},
16 depth => { type => 'Integer', doc => 'Max distance of related tables to include. Default is 1. 0 means show only the named tables, -1 means to include everything', is_optional => 1},
17 file => { type => 'String', doc => 'Pathname of the Umlet (.uxf) file' },
18 show_columns => { type => 'Boolean', is_optional => 1, default => 1, doc => 'Include column names in the diagram' },
19 initial_name => {
20 is_many => 1,
21 is_optional => 1,
22 shell_args_position => 1
23 }
24 ],
25 );
26  
27 sub sub_command_sort_position { 3 };
28  
29  
30 sub help_brief {
31 "Update an Umlet diagram based on the current schema"
32 }
33  
34 sub help_detail {
35 return <<EOS;
36 Creates a new Umlet diagram, or updates an existing diagram. Bare arguments
37 are taken as table names to include in the diagram. Other tables may be
38 included in the diagram based on their distance from the names tables
39 and the --depth parameter.
40  
41 If an existing file is being updated, the position of existing elements
42 will not change.
43  
44 EOS
45 }
46  
47 # The max X coord to use when placing boxes. After this, move down a line and go back to the left
48 use constant MAX_X_AUTO_POSITION => 1000;
49  
50 # FIXME This execute() and the one from ur update class-diagram should be combined since they share
51 # most of the code
52 sub execute {
53 my $self = shift;
54  
55 my $params = shift;
56
57 #$DB::single = 1;
58 my $namespace = $self->namespace_name;
59 eval "use $namespace";
60 if ($@) {
61 $self->error_message("Failed to load module for $namespace: $@");
62 return;
63 }
64  
65  
66 # FIXME this is a workaround for a bug. If you try to get Table objects filtered by namespace,
67 # you have to have already instantiated the namespace's data source objects into the object cache
68 # first
69 map { $_->_singleton_object } $namespace->get_data_sources;
70  
71 my @initial_name_list;
72 if ($params->{'depth'} == -1) {
73 # They wanted them all... Ignore whatever is on the command line
74 @initial_name_list = map { $_->table_name}
75 UR::DataSource::RDBMS::Table->get(namespace => $namespace);
76 } else {
77 @initial_name_list = $self->initial_name;
78 }
79  
80 my $diagram;
81 if (-f $params->{'file'}) {
82 $params->{'depth'} = 0 unless (exists $params->{'depth'}); # Default is just update what's there
83 $diagram = UR::Object::Umlet::Diagram->create_from_file($params->{'file'});
84 push @initial_name_list, map { $_->subject_id } UR::Object::Umlet::Class->get(diagram_name => $diagram->name);
85 } else {
86 $params->{'depth'} = 1 unless exists($params->{'depth'});
87 $diagram = UR::Object::Umlet::Diagram->create(name => $params->{'file'});
88 }
89  
90  
91 # FIXME this can get removed when attribute defaults work correctly
92 unless (exists $params->{'show_attributes'}) {
93 $self->show_columns(1);
94 }
95
96 my @involved_tables = map { UR::DataSource::RDBMS::Table->get(table_name => $_, namespace => $namespace) }
97 @initial_name_list;
98 #foreach my $table_name ( @initial_name_list ) {
99 # # FIXME namespace dosen't work here either
100 # push @involved_tables, UR::DataSource::RDBMS::Table->get(namespace => $namespace, table_name => $table_name);
101 #}
102  
103 #$DB::single = 1;
104 push @involved_tables ,$self->_get_related_items( names => \@initial_name_list,
105 depth => $params->{'depth'},
106 namespace => $namespace,
107 item_class => 'UR::DataSource::RDBMS::Table',
108 item_param => 'table_name',
109 related_class => 'UR::DataSource::RDBMS::FkConstraint',
110 related_param => 'r_table_name',
111 );
112 my %involved_table_names = map { $_->table_name => 1 } @involved_tables;
113  
114 # Figure out the initial placement
115 # The initial placement, and how much to move over for the next box
116 my($x_coord, $y_coord, $x_inc, $y_inc) = (20,20,40,40);
117 my @objs = sort { $b->y <=> $a->y or $b->x <=> $a->x } UR::Object::Umlet::Class->get();
118 if (@objs) {
119 my $maxobj = $objs[0];
120 $x_coord = $maxobj->x + $maxobj->width + $x_inc;
121 $y_coord = $maxobj->y + $maxobj->height + $y_inc;
122 }
123
124  
125 # First, place all the tables' boxes
126 my @all_boxes = UR::Object::Umlet::Class->get( diagram_name => $diagram->name );
127 foreach my $table ( @involved_tables ) {
128 my $umlet_table = UR::Object::Umlet::Class->get(diagram_name => $diagram->name,
129 subject_id => $table->table_name);
130 my $created = 0;
131 unless ($umlet_table) {
132 $created = 1;
133 $umlet_table = UR::Object::Umlet::Class->create( diagram_name => $diagram->name,
134 subject_id => $table->table_name,
135 label => $table->table_name,
136 x => $x_coord,
137 y => $y_coord,
138 );
139
140 if ($self->show_columns) {
141 my $attributes = $umlet_table->attributes || [];
142 my %attributes_already_in_diagram = map { $_->{'name'} => 1 } @{ $attributes };
143 my %pk_properties = map { $_ => 1 } $table->primary_key_constraint_column_names;
144
145 my $line_count = scalar @$attributes;
146 foreach my $column_name ( $table->column_names ) {
147 next if $attributes_already_in_diagram{$column_name};
148 $line_count++;
149 my $column = UR::DataSource::RDBMS::TableColumn->get(table_name => $table->table_name,
150 column_name => $column_name,
151 namespace => $namespace);
152 push @$attributes, { is_id => $pk_properties{$column_name} ? '+' : ' ',
153 name => $column_name,
154 type => $column->data_type,
155 line => $line_count,
156 };
157 }
158 $umlet_table->attributes($attributes);
159 }
160  
161 # Make sure this box dosen't overlap other boxes
162 while(my $overlapped = $umlet_table->is_overlapping(@all_boxes) ) {
163 if ($umlet_table->x > MAX_X_AUTO_POSITION) {
164 $umlet_table->x(20);
165 $umlet_table->y( $umlet_table->y + $y_inc);
166 } else {
167 $umlet_table->x( $overlapped->x + $overlapped->width + $x_inc );
168 }
169 }
170  
171 push @all_boxes, $umlet_table;
172 }
173  
174 if ($created) {
175 $x_coord = $umlet_table->x + $umlet_table->width + $x_inc;
176 if ($x_coord > MAX_X_AUTO_POSITION) {
177 $x_coord = 20;
178 $y_coord += $y_inc;
179 }
180 }
181 }
182  
183 # Next, connect the tables together
184 foreach my $table ( @involved_tables ) {
185 foreach my $fk ( UR::DataSource::RDBMS::FkConstraint->get(table_name => $table->table_name, namespace => $namespace) ) {
186  
187 next unless ($involved_table_names{$fk->r_table_name});
188  
189 my $umlet_relation = UR::Object::Umlet::Relation->get( #diagram_name => $diagram->name,
190 from_entity_name => $fk->table_name,
191 to_entity_name => $fk->r_table_name,
192 );
193 unless ($umlet_relation) {
194 my @fk_column_names = $fk->column_name_map();
195 my $label = join("\n", map { $_->[0] . " -> " . $_->[1] } @fk_column_names);
196 $umlet_relation = UR::Object::Umlet::Relation->create( diagram_name => $diagram->name,
197 relation_type => '&lt;-',
198 label => $label,
199 from_entity_name => $fk->table_name,
200 to_entity_name => $fk->r_table_name,
201 );
202 $umlet_relation->connect_entities();
203 }
204 }
205 }
206  
207 $diagram->save_to_file($params->{'file'});
208  
209 1;
210 }
211  
212  
213 sub _get_related_items {
214 my($self, %params) = @_;
215  
216 return unless (@{$params{'names'}});
217 return unless $params{'depth'};
218  
219 my $item_class = $params{'item_class'};
220 my $item_param = $params{'item_param'};
221  
222 my $related_class = $params{'related_class'};
223 my $related_param = $params{'related_param'};
224  
225 # Get everything linked to the named things
226 my @related_names = map { $_->$related_param } $related_class->get($item_param => $params{'names'}, namespace => $params{'namespace'});
227 push @related_names, map { $_->$item_param } $related_class->get($related_param => $params{'names'}, namespace => $params{'namespace'});
228 return unless @related_names;
229  
230 my @objs = $item_class->get($item_param => \@related_names, namespace => $params{'namespace'});
231  
232 # make a recursive call to get the related objects by name
233 return ( @objs, $self->_get_related_items( %params, names => \@related_names, depth => --$params{'depth'}) );
234 }
235
236  
237  
238  
239 1;
240