1 package SQL::Translator::Producer::GraphViz;
3 # -------------------------------------------------------------------
4 # $Id: GraphViz.pm,v 1.1 2003-04-24 16:15:13 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
26 use SQL::Translator::Utils qw(debug);
28 use vars qw[ $VERSION $DEBUG ];
29 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
30 $DEBUG = 0 unless defined $DEBUG;
32 use constant VALID_LAYOUT => {
38 use constant VALID_NODE_SHAPE => {
54 use constant VALID_OUTPUT => {
81 my $args = $t->producer_args;
82 local $DEBUG = $t->debug;
83 debug("Data =\n", Dumper( $data ));
84 debug("Producer args =\n", Dumper( $args ));
86 my $out_file = $args->{'out_file'};
87 my $layout = $args->{'layout'};
88 my $node_shape = $args->{'node_shape'};
89 my $output_type = $args->{'output_type'};
90 my $add_color = $args->{'add_color'};
91 my $natural_join = $args->{'natural_join'};
92 my $join_pk_only = $args->{'join_pk_only'};
93 my $skip_fields = $args->{'skip_fields'};
94 my %skip = map { s/^\s+|\s+$//g; $_, 1 }
95 split ( /,/, $skip_fields );
96 $natural_join ||= $join_pk_only;
98 die "Invalid layout '$layout'" unless VALID_LAYOUT->{ $layout };
99 die "Invalid output type: '$output_type'"
100 unless VALID_OUTPUT->{ $output_type };
101 die "Invalid node shape'$node_shape'"
102 unless VALID_NODE_SHAPE->{ $node_shape };
105 # Create GraphViz and see if we can produce the output type.
107 my $gv = GraphViz->new(
108 directed => $natural_join ? 0 : 1,
111 bgcolor => $add_color ? 'lightgoldenrodyellow' : 'white',
113 shape => $node_shape,
117 ) or die "Can't create GraphViz object\n";
119 my %nj_registry; # for locations of fields for natural joins
120 my @fk_registry; # for locations of fields for foreign keys
123 # If necessary, pre-process fields to find foreign keys.
125 if ( $natural_join ) {
126 my ( %common_keys, %pk );
127 for my $table ( values %$data ) {
129 @{ $table->{'indices'} || [] },
130 @{ $table->{'constraints'} || [] },
132 my @fields = @{ $index->{'fields'} || [] } or next;
133 if ( $index->{'type'} eq 'primary_key' ) {
134 $pk{ $_ } = 1 for @fields;
138 for my $field ( values %{ $table->{'fields'} } ) {
139 push @{ $common_keys{ $field->{'name'} } },
140 $table->{'table_name'};
144 for my $field ( keys %common_keys ) {
145 my @tables = @{ $common_keys{ $field } };
146 next unless scalar @tables > 1;
147 for my $table ( @tables ) {
148 next if $join_pk_only and !defined $pk{ $field };
149 $data->{ $table }{'fields'}{ $field }{'is_fk'} = 1;
154 for my $table ( values %$data ) {
155 for my $field ( values %{ $table->{'fields'} } ) {
157 grep { $_->{'type'} eq 'foreign_key' }
158 @{ $field->{'constraints'} }
160 my $ref_table = $constraint->{'reference_table'} or next;
161 my @ref_fields = @{ $constraint->{'reference_fields'}||[] };
163 unless ( @ref_fields ) {
165 values %{ $data->{ $ref_table }{'fields'} }
168 grep { $_->{'type'} eq 'primary_key' }
169 @{ $field->{'constraints'} }
171 push @ref_fields, @{ $pk->{'fields'} };
175 $constraint->{'reference_fields'} = [ @ref_fields ];
178 for my $ref_field (@{$constraint->{'reference_fields'}}) {
179 $data->{$ref_table}{'fields'}{$ref_field}{'is_fk'} = 1;
188 sort { $a->[0] <=> $b->[0] }
189 map { [ $_->{'order'}, $_ ] }
192 my $table_name = $table->{'table_name'};
193 $gv->add_node( $table_name );
195 debug("Processing table '$table_name'");
199 sort { $a->[0] <=> $b->[0] }
200 map { [ $_->{'order'}, $_ ] }
201 values %{ $table->{'fields'} };
203 debug("Fields = ", join(', ', map { $_->{'name'} } @fields));
207 @{ $table->{'indices'} || [] },
208 @{ $table->{'constraints'} || [] },
210 my @fields = @{ $index->{'fields'} || [] } or next;
211 if ( $index->{'type'} eq 'primary_key' ) {
212 $pk{ $_ } = 1 for @fields;
214 elsif ( $index->{'type'} eq 'unique' ) {
215 $unique{ $_ } = 1 for @fields;
219 debug("Primary keys = ", join(', ', sort keys %pk));
220 debug("Unique = ", join(', ', sort keys %unique));
222 for my $f ( @fields ) {
223 my $name = $f->{'name'} or next;
224 my $is_pk = $pk{ $name };
225 my $is_unique = $unique{ $name };
228 # Decide if we should skip this field.
230 if ( $natural_join ) {
231 next unless $is_pk || $f->{'is_fk'};
234 next unless $is_pk ||
235 grep { $_->{'type'} eq 'foreign_key' }
236 @{ $f->{'constraints'} }
240 my $constraints = $f->{'constraints'};
242 if ( $natural_join && !$skip{ $name } ) {
243 push @{ $nj_registry{ $name } }, $table_name;
245 elsif ( @{ $constraints || [] } ) {
246 for my $constraint ( @$constraints ) {
247 next unless $constraint->{'type'} eq 'foreign_key';
249 @{ $constraint->{'reference_fields'} || [] }
251 my $fk_table = $constraint->{'reference_table'};
252 next unless defined $data->{ $fk_table };
253 push @fk_registry, [ $table_name, $fk_table ];
261 # Make the connections.
264 if ( $natural_join ) {
265 for my $field_name ( keys %nj_registry ) {
266 my @table_names = @{ $nj_registry{ $field_name } || [] } or next;
267 next if scalar @table_names == 1;
268 push @table_bunches, [ @table_names ];
272 @table_bunches = @fk_registry;
276 for my $bunch ( @table_bunches ) {
277 my @tables = @$bunch;
279 for my $i ( 0 .. $#tables ) {
280 my $table1 = $tables[ $i ];
281 for my $j ( 0 .. $#tables ) {
282 my $table2 = $tables[ $j ];
283 next if $table1 eq $table2;
284 next if $done{ $table1 }{ $table2 };
285 $gv->add_edge( $table1, $table2 );
286 $done{ $table1 }{ $table2 } = 1;
287 $done{ $table2 }{ $table1 } = 1;
295 my $output_method = "as_$output_type";
297 open my $fh, ">$out_file" or die "Can't write '$out_file': $!\n";
298 print $fh $gv->$output_method;
302 return $gv->$output_method;
312 SQL::Translator::Producer::GraphViz - GraphViz producer for SQL::Translator
316 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>