File: SchemaDiagram.pm

package info (click to toggle)
libur-perl 0.470%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,184 kB
  • sloc: perl: 61,813; javascript: 255; xml: 108; sh: 13; makefile: 9
file content (240 lines) | stat: -rw-r--r-- 10,416 bytes parent folder | download | duplicates (3)
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

package UR::Namespace::Command::Update::SchemaDiagram;



use strict;
use warnings;
use UR;
our $VERSION = "0.47"; # UR $VERSION;

UR::Object::Type->define(
    class_name => __PACKAGE__,
    is => 'UR::Namespace::Command::Base',
    has => [ 
        data_source => {type => 'String', doc => 'Which datasource to use', is_optional => 1},
        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},
        file => { type => 'String', doc => 'Pathname of the Umlet (.uxf) file' },
        show_columns => { type => 'Boolean', is_optional => 1, default => 1, doc => 'Include column names in the diagram' },
        initial_name => {
            is_many => 1,
            is_optional => 1,
            shell_args_position => 1
        }
    ],
);

sub sub_command_sort_position { 3 };


sub help_brief {
    "Update an Umlet diagram based on the current schema"
}

sub help_detail {
    return <<EOS;
Creates a new Umlet diagram, or updates an existing diagram.  Bare arguments
are taken as table names to include in the diagram.  Other tables may be
included in the diagram based on their distance from the names tables
and the --depth parameter.

If an existing file is being updated, the position of existing elements 
will not change.

EOS
}

# The max X coord to use when placing boxes.  After this, move down a line and go back to the left
use constant MAX_X_AUTO_POSITION => 1000;

# FIXME   This execute() and the one from ur update class-diagram should be combined since they share
# most of the code
sub execute {
    my $self = shift;

    my $params = shift;
    
#$DB::single = 1;
    my $namespace = $self->namespace_name;
    eval "use $namespace";
    if ($@) {
        $self->error_message("Failed to load module for $namespace: $@");
        return;
    }


    # FIXME this is a workaround for a bug.  If you try to get Table objects filtered by namespace,
    # you have to have already instantiated the namespace's data source objects into the object cache 
    # first
    map { $_->_singleton_object } $namespace->get_data_sources;

    my @initial_name_list;
    if ($params->{'depth'} == -1) {
        # They wanted them all...  Ignore whatever is on the command line
        @initial_name_list = map { $_->table_name}
                                 UR::DataSource::RDBMS::Table->get(namespace => $namespace);
    } else {
        @initial_name_list = $self->initial_name;
    }

    my $diagram;
    if (-f $params->{'file'}) {
        $params->{'depth'} = 0 unless (exists $params->{'depth'});  # Default is just update what's there
        $diagram = UR::Object::Umlet::Diagram->create_from_file($params->{'file'});
        push @initial_name_list, map { $_->subject_id } UR::Object::Umlet::Class->get(diagram_name => $diagram->name);
    } else {
        $params->{'depth'} = 1 unless exists($params->{'depth'});
        $diagram = UR::Object::Umlet::Diagram->create(name => $params->{'file'});
    }


    # FIXME this can get removed when attribute defaults work correctly
    unless (exists $params->{'show_attributes'}) {
        $self->show_columns(1);
    }
        
    my @involved_tables = map { UR::DataSource::RDBMS::Table->get(table_name => $_, namespace => $namespace) }
                            @initial_name_list;
    #foreach my $table_name ( @initial_name_list ) {
    #    # FIXME namespace dosen't work here either
    #    push @involved_tables, UR::DataSource::RDBMS::Table->get(namespace => $namespace, table_name => $table_name);
    #}

#$DB::single = 1;
    push @involved_tables ,$self->_get_related_items( names => \@initial_name_list,
                                                      depth => $params->{'depth'},
                                                      namespace => $namespace,
                                                      item_class => 'UR::DataSource::RDBMS::Table',
                                                      item_param => 'table_name',
                                                      related_class => 'UR::DataSource::RDBMS::FkConstraint',
                                                      related_param => 'r_table_name',
                                                    );
    my %involved_table_names = map { $_->table_name => 1 } @involved_tables;

    # Figure out the initial placement
    # The initial placement, and how much to move over for the next box
    my($x_coord, $y_coord, $x_inc, $y_inc) = (20,20,40,40);
    my @objs = sort { $b->y <=> $a->y or $b->x <=> $a->x } UR::Object::Umlet::Class->get();
    if (@objs) {
        my $maxobj = $objs[0];
        $x_coord = $maxobj->x + $maxobj->width + $x_inc;
        $y_coord = $maxobj->y + $maxobj->height + $y_inc;
    }
    

    # First, place all the tables' boxes
    my @all_boxes = UR::Object::Umlet::Class->get( diagram_name => $diagram->name );
    foreach my $table ( @involved_tables ) {
        my $umlet_table = UR::Object::Umlet::Class->get(diagram_name => $diagram->name,
                                                        subject_id => $table->table_name);
        my $created = 0;
        unless ($umlet_table) {
            $created = 1;
            $umlet_table = UR::Object::Umlet::Class->create( diagram_name => $diagram->name,
                                                             subject_id => $table->table_name,
                                                             label => $table->table_name,
                                                             x => $x_coord,
                                                             y => $y_coord,
                                                           );
                                                                        
            if ($self->show_columns) {
                my $attributes = $umlet_table->attributes || [];
                my %attributes_already_in_diagram = map { $_->{'name'} => 1 } @{ $attributes };
                my %pk_properties = map { $_ => 1 } $table->primary_key_constraint_column_names;
    
                my $line_count = scalar @$attributes;
                foreach my $column_name ( $table->column_names ) {
                    next if $attributes_already_in_diagram{$column_name};
                    $line_count++;
                    my $column = UR::DataSource::RDBMS::TableColumn->get(table_name => $table->table_name,
                                                                         column_name => $column_name,
                                                                         namespace => $namespace);
                    push @$attributes, { is_id => $pk_properties{$column_name} ? '+' : ' ',
                                         name => $column_name,
                                         type => $column->data_type,
                                         line => $line_count,
                                       };
                }
                $umlet_table->attributes($attributes);
            }

            # Make sure this box dosen't overlap other boxes
            while(my $overlapped = $umlet_table->is_overlapping(@all_boxes) ) {
                    if ($umlet_table->x > MAX_X_AUTO_POSITION) {
                        $umlet_table->x(20);
                        $umlet_table->y( $umlet_table->y + $y_inc);
                    } else {
                        $umlet_table->x( $overlapped->x + $overlapped->width + $x_inc );
                    }
            }

            push @all_boxes, $umlet_table;
        }

        if ($created) {
            $x_coord = $umlet_table->x + $umlet_table->width + $x_inc;
            if ($x_coord > MAX_X_AUTO_POSITION) {
                $x_coord = 20;
                $y_coord += $y_inc;
            }
        }
    }

    # Next, connect the tables together
    foreach my $table ( @involved_tables ) {
        foreach my $fk ( UR::DataSource::RDBMS::FkConstraint->get(table_name => $table->table_name, namespace => $namespace) )  {

            next unless ($involved_table_names{$fk->r_table_name});

            my $umlet_relation = UR::Object::Umlet::Relation->get( #diagram_name => $diagram->name,
                                                                   from_entity_name => $fk->table_name,
                                                                   to_entity_name => $fk->r_table_name,
                                                                 );
            unless ($umlet_relation) {
                my @fk_column_names = $fk->column_name_map();
                my $label = join("\n", map { $_->[0] . " -> " . $_->[1] } @fk_column_names);
                $umlet_relation = UR::Object::Umlet::Relation->create( diagram_name => $diagram->name,
                                                                       relation_type => '&lt;-',
                                                                       label => $label,
                                                                       from_entity_name => $fk->table_name,
                                                                       to_entity_name => $fk->r_table_name,
                                                                     );
                 $umlet_relation->connect_entities();
            }
        }
    }

    $diagram->save_to_file($params->{'file'});

    1;
}


sub _get_related_items {
my($self, %params) = @_;

    return unless (@{$params{'names'}});
    return unless $params{'depth'};

    my $item_class = $params{'item_class'};
    my $item_param = $params{'item_param'};

    my $related_class = $params{'related_class'};
    my $related_param = $params{'related_param'};

    # Get everything linked to the named things
    my @related_names = map { $_->$related_param } $related_class->get($item_param => $params{'names'}, namespace => $params{'namespace'});
    push @related_names, map { $_->$item_param } $related_class->get($related_param => $params{'names'}, namespace => $params{'namespace'});
    return unless @related_names;

    my @objs = $item_class->get($item_param => \@related_names, namespace => $params{'namespace'});

    # make a recursive call to get the related objects by name
    return ( @objs, $self->_get_related_items( %params, names => \@related_names, depth => --$params{'depth'}) );
}
    



1;