« blib/lib/SQL/Translator/Parser/DB2/Grammar.pm
↑ Summary
blib/lib/SQL/Translator/Producer/HTML.pm »
| 1 | package SQL::Translator::Producer::GraphViz; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SQL::Translator::Producer::GraphViz - GraphViz producer for SQL::Translator | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use SQL::Translator; | ||
| 12 | |||
| 13 | my $trans = SQL::Translator->new( | ||
| 14 | from => 'MySQL', # or your db of choice | ||
| 15 | to => 'GraphViz', | ||
| 16 | producer_args => { | ||
| 17 | out_file => 'schema.png', | ||
| 18 | bgcolor => 'lightgoldenrodyellow', | ||
| 19 | show_constraints => 1, | ||
| 20 | show_datatypes => 1, | ||
| 21 | show_sizes => 1 | ||
| 22 | } | ||
| 23 | ) or die SQL::Translator->error; | ||
| 24 | |||
| 25 | $trans->translate or die $trans->error; | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Creates a graph of a schema using the amazing graphviz | ||
| 30 | (see http://www.graphviz.org/) application (via | ||
| 31 | the L<GraphViz> module). It's nifty--you should try it! | ||
| 32 | |||
| 33 | =head1 PRODUCER ARGS | ||
| 34 | |||
| 35 | All L<GraphViz> constructor attributes are accepted and passed | ||
| 36 | through to L<GraphViz/new>. The following defaults are assumed | ||
| 37 | for some attributes: | ||
| 38 | |||
| 39 | layout => 'dot', | ||
| 40 | overlap => 'false', | ||
| 41 | |||
| 42 | node => { | ||
| 43 | shape => 'record', | ||
| 44 | style => 'filled', | ||
| 45 | fillcolor => 'white', | ||
| 46 | }, | ||
| 47 | |||
| 48 | # in inches | ||
| 49 | width => 8.5, | ||
| 50 | height => 11, | ||
| 51 | |||
| 52 | See the documentation of L<GraphViz/new> for more info on these | ||
| 53 | and other attributes. | ||
| 54 | |||
| 55 | In addition this producer accepts the following arguments: | ||
| 56 | |||
| 57 | =over 4 | ||
| 58 | |||
| 59 | =item * skip_tables | ||
| 60 | |||
| 61 | An arrayref or a comma-separated list of table names that should be | ||
| 62 | skipped. Note that a skipped table node may still appear if another | ||
| 63 | table has foreign key constraints pointing to the skipped table. If | ||
| 64 | this happens no table field/index information will be included. | ||
| 65 | |||
| 66 | =item * skip_tables_like | ||
| 67 | |||
| 68 | An arrayref or a comma-separated list of regular expressions matching | ||
| 69 | table names that should be skipped. | ||
| 70 | |||
| 71 | =item * cluster | ||
| 72 | |||
| 73 | Clustering of tables allows you to group and box tables according to | ||
| 74 | function or domain or whatever criteria you choose. The syntax for | ||
| 75 | clustering tables is: | ||
| 76 | |||
| 77 | cluster => 'cluster1=table1,table2;cluster2=table3,table4' | ||
| 78 | |||
| 79 | Or pass it as an arrayref like so: | ||
| 80 | |||
| 81 | cluster => [ 'cluster1=table1,table2', 'cluster2=table3,table4' ] | ||
| 82 | |||
| 83 | Or like so: | ||
| 84 | |||
| 85 | cluster => [ | ||
| 86 | { name => 'cluster1', tables => [ 'table1', 'table2' ] }, | ||
| 87 | { name => 'cluster2', tables => [ 'table3', 'table4' ] }, | ||
| 88 | ] | ||
| 89 | |||
| 90 | =item * out_file | ||
| 91 | |||
| 92 | The name of the file where the resulting GraphViz output will be | ||
| 93 | written. Alternatively an open filehandle can be supplied. If | ||
| 94 | undefined (the default) - the result is returned as a string. | ||
| 95 | |||
| 96 | =item * output_type (DEFAULT: 'png') | ||
| 97 | |||
| 98 | This determines which | ||
| 99 | L<output method|GraphViz/as_canon, as_text, as_gif etc. methods> | ||
| 100 | will be invoked to generate the graph: C<png> translates to | ||
| 101 | C<as_png>, C<ps> to C<as_ps> and so on. | ||
| 102 | |||
| 103 | =item * fontname | ||
| 104 | |||
| 105 | This sets the global font name (or full path to font file) for | ||
| 106 | node, edge, and graph labels | ||
| 107 | |||
| 108 | =item * fontsize | ||
| 109 | |||
| 110 | This sets the global font size for node and edge labels (note that | ||
| 111 | arbitrarily large sizes may be ignored due to page size or graph size | ||
| 112 | constraints) | ||
| 113 | |||
| 114 | =item * show_fields (DEFAULT: true) | ||
| 115 | |||
| 116 | If set to a true value, the names of the columns in a table will | ||
| 117 | be displayed in each table's node | ||
| 118 | |||
| 119 | =item * show_fk_only | ||
| 120 | |||
| 121 | If set to a true value, only columns which are foreign keys | ||
| 122 | will be displayed in each table's node | ||
| 123 | |||
| 124 | =item * show_datatypes | ||
| 125 | |||
| 126 | If set to a true value, the datatype of each column will be | ||
| 127 | displayed next to each column's name; this option will have no | ||
| 128 | effect if the value of C<show_fields> is set to false | ||
| 129 | |||
| 130 | =item * friendly_ints | ||
| 131 | |||
| 132 | If set to a true value, each integer type field will be displayed | ||
| 133 | as a tinyint, smallint, integer or bigint depending on the field's | ||
| 134 | associated size parameter. This only applies for the C<integer> | ||
| 135 | type (and not the C<int> type, which is always assumed to be a | ||
| 136 | 32-bit integer); this option will have no effect if the value of | ||
| 137 | C<show_fields> is set to false | ||
| 138 | |||
| 139 | =item * friendly_ints_extended | ||
| 140 | |||
| 141 | If set to a true value, the friendly ints displayed will take into | ||
| 142 | account the non-standard types, 'tinyint' and 'mediumint' (which, | ||
| 143 | as far as I am aware, is only implemented in MySQL) | ||
| 144 | |||
| 145 | =item * show_sizes | ||
| 146 | |||
| 147 | If set to a true value, the size (in bytes) of each CHAR and | ||
| 148 | VARCHAR column will be displayed in parentheses next to the | ||
| 149 | column's name; this option will have no effect if the value of | ||
| 150 | C<show_fields> is set to false | ||
| 151 | |||
| 152 | =item * show_constraints | ||
| 153 | |||
| 154 | If set to a true value, a field's constraints (i.e., its | ||
| 155 | primary-key-ness, its foreign-key-ness and/or its uniqueness) | ||
| 156 | will appear as a comma-separated list in brackets next to the | ||
| 157 | field's name; this option will have no effect if the value of | ||
| 158 | C<show_fields> is set to false | ||
| 159 | |||
| 160 | =item * show_indexes | ||
| 161 | |||
| 162 | If set to a true value, each record will also show the indexes | ||
| 163 | set on each table. It describes the index types along with | ||
| 164 | which columns are included in the index. | ||
| 165 | |||
| 166 | =item * show_index_names (DEFAULT: true) | ||
| 167 | |||
| 168 | If C<show_indexes> is set to a true value, then the value of this | ||
| 169 | parameter determines whether or not to print names of indexes. | ||
| 170 | if C<show_index_names> is false, then a list of indexed columns | ||
| 171 | will appear below the field list. Otherwise, it will be a list | ||
| 172 | prefixed with the name of each index. | ||
| 173 | |||
| 174 | =item * natural_join | ||
| 175 | |||
| 176 | If set to a true value, L<SQL::Translator::Schema/make_natural_joins> | ||
| 177 | will be called before generating the graph. | ||
| 178 | |||
| 179 | =item * join_pk_only | ||
| 180 | |||
| 181 | The value of this option will be passed as the value of the | ||
| 182 | like-named argument to L<SQL::Translator::Schema/make_natural_joins>; | ||
| 183 | implies C<< natural_join => 1 >> | ||
| 184 | |||
| 185 | =item * skip_fields | ||
| 186 | |||
| 187 | The value of this option will be passed as the value of the | ||
| 188 | like-named argument to L<SQL::Translator::Schema/make_natural_joins>; | ||
| 189 | implies C<< natural_join => 1 >> | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | =head2 DEPRECATED ARGS | ||
| 194 | |||
| 195 | =over 4 | ||
| 196 | |||
| 197 | =item * node_shape | ||
| 198 | |||
| 199 | Deprecated, use node => { shape => ... } instead | ||
| 200 | |||
| 201 | =item * add_color | ||
| 202 | |||
| 203 | Deprecated, use bgcolor => 'lightgoldenrodyellow' instead | ||
| 204 | |||
| 205 | If set to a true value, the graphic will have a background | ||
| 206 | color of 'lightgoldenrodyellow'; otherwise the default | ||
| 207 | white background will be used | ||
| 208 | |||
| 209 | =item * nodeattrs | ||
| 210 | |||
| 211 | Deprecated, use node => { ... } instead | ||
| 212 | |||
| 213 | =item * edgeattrs | ||
| 214 | |||
| 215 | Deprecated, use edge => { ... } instead | ||
| 216 | |||
| 217 | =item * graphattrs | ||
| 218 | |||
| 219 | Deprecated, use graph => { ... } instead | ||
| 220 | |||
| 221 | =back | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | use warnings; | ||
| 226 | use strict; | ||
| 227 | use GraphViz; | ||
| 228 | use SQL::Translator::Schema::Constants; | ||
| 229 | use SQL::Translator::Utils qw(debug); | ||
| 230 | use Scalar::Util qw/openhandle/; | ||
| 231 | |||
| 232 | our $DEBUG; | ||
| 233 | our $VERSION = '1.66'; | ||
| 234 | $DEBUG = 0 unless defined $DEBUG; | ||
| 235 | |||
| 236 | sub produce { | ||
| 237 | my $t = shift; | ||
| 238 | my $schema = $t->schema; | ||
| 239 | my $args = $t->producer_args; | ||
| 240 | local $DEBUG = $t->debug; | ||
| 241 | |||
| 242 | # translate legacy {node|edge|graph}attrs to just {node|edge|graph} | ||
| 243 | for my $argtype (qw/node edge graph/) { | ||
| 244 | my $old_arg = $argtype . 'attrs'; | ||
| 245 | |||
| 246 | my %arglist = (map { %{ $_ || {} } } (delete $args->{$old_arg}, delete $args->{$argtype})); | ||
| 247 | |||
| 248 | $args->{$argtype} = \%arglist if keys %arglist; | ||
| 249 | } | ||
| 250 | |||
| 251 | # explode font settings | ||
| 252 | for (qw/fontsize fontname/) { | ||
| 253 | if (defined $args->{$_}) { | ||
| 254 | $args->{node}{$_} ||= $args->{$_}; | ||
| 255 | $args->{edge}{$_} ||= $args->{$_}; | ||
| 256 | $args->{graph}{$_} ||= $args->{$_}; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | # legacy add_color setting, trumped by bgcolor if set | ||
| 261 | $args->{bgcolor} ||= 'lightgoldenrodyellow' if $args->{add_color}; | ||
| 262 | |||
| 263 | # legacy node_shape setting, defaults to 'record', trumped by {node}{shape} | ||
| 264 | $args->{node}{shape} ||= ($args->{node_shape} || 'record'); | ||
| 265 | |||
| 266 | # maintain defaults | ||
| 267 | $args->{layout} ||= 'dot'; | ||
| 268 | $args->{output_type} ||= 'png'; | ||
| 269 | $args->{overlap} ||= 'false'; | ||
| 270 | $args->{node}{style} ||= 'filled'; | ||
| 271 | $args->{node}{fillcolor} ||= 'white'; | ||
| 272 | |||
| 273 | $args->{show_fields} = 1 if not exists $args->{show_fields}; | ||
| 274 | $args->{show_index_names} = 1 if not exists $args->{show_index_names}; | ||
| 275 | $args->{width} = 8.5 if not defined $args->{width}; | ||
| 276 | $args->{height} = 11 if not defined $args->{height}; | ||
| 277 | for ($args->{height}, $args->{width}) { | ||
| 278 | $_ = 0 unless $_ =~ /^\d+(?:.\d+)?$/; | ||
| 279 | $_ = 0 if $_ < 0; | ||
| 280 | } | ||
| 281 | |||
| 282 | # so split won't warn | ||
| 283 | $args->{$_} ||= '' for qw/skip_fields skip_tables skip_tables_like cluster/; | ||
| 284 | |||
| 285 | my %skip_fields = map { s/^\s+|\s+$//g; length $_ ? ($_, 1) : () } | ||
| 286 | split(/,/, $args->{skip_fields}); | ||
| 287 | |||
| 288 | my %skip_tables = map { $_, 1 } ( | ||
| 289 | ref $args->{skip_tables} eq 'ARRAY' | ||
| 290 | ? @{ $args->{skip_tables} } | ||
| 291 | : split(/\s*,\s*/, $args->{skip_tables}) | ||
| 292 | ); | ||
| 293 | |||
| 294 | my @skip_tables_like = map {qr/$_/} ( | ||
| 295 | ref $args->{skip_tables_like} eq 'ARRAY' | ||
| 296 | ? @{ $args->{skip_tables_like} } | ||
| 297 | : split(/\s*,\s*/, $args->{skip_tables_like}) | ||
| 298 | ); | ||
| 299 | |||
| 300 | # join_pk_only/skip_fields implies natural_join | ||
| 301 | $args->{natural_join} = 1 | ||
| 302 | if ($args->{join_pk_only} or scalar keys %skip_fields); | ||
| 303 | |||
| 304 | # usually we do not want direction when using natural join | ||
| 305 | $args->{directed} = ($args->{natural_join} ? 0 : 1) | ||
| 306 | if not exists $args->{directed}; | ||
| 307 | |||
| 308 | $schema->make_natural_joins( | ||
| 309 | join_pk_only => $args->{join_pk_only}, | ||
| 310 | skip_fields => $args->{skip_fields}, | ||
| 311 | ) if $args->{natural_join}; | ||
| 312 | |||
| 313 | my %cluster; | ||
| 314 | if (defined $args->{'cluster'}) { | ||
| 315 | my @clusters; | ||
| 316 | if (ref $args->{'cluster'} eq 'ARRAY') { | ||
| 317 | @clusters = @{ $args->{'cluster'} }; | ||
| 318 | } else { | ||
| 319 | @clusters = split /\s*;\s*/, $args->{'cluster'}; | ||
| 320 | } | ||
| 321 | |||
| 322 | for my $c (@clusters) { | ||
| 323 | my ($cluster_name, @cluster_tables); | ||
| 324 | if (ref $c eq 'HASH') { | ||
| 325 | $cluster_name = $c->{'name'} || $c->{'cluster_name'}; | ||
| 326 | @cluster_tables = @{ $c->{'tables'} || [] }; | ||
| 327 | } else { | ||
| 328 | my ($name, $tables) = split /\s*=\s*/, $c; | ||
| 329 | $cluster_name = $name; | ||
| 330 | @cluster_tables = split /\s*,\s*/, $tables; | ||
| 331 | } | ||
| 332 | |||
| 333 | for my $table (@cluster_tables) { | ||
| 334 | $cluster{$table} = $cluster_name; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | # | ||
| 340 | # Create a blank GraphViz object and see if we can produce the output type. | ||
| 341 | # | ||
| 342 | my $gv = GraphViz->new(%$args) | ||
| 343 | or die sprintf("Can't create GraphViz object: %s\n", $@ || 'reason unknown'); | ||
| 344 | |||
| 345 | my $output_method = "as_$args->{output_type}"; | ||
| 346 | |||
| 347 | # the generators are AUTOLOADed so can't use ->can ($output_method) | ||
| 348 | eval { $gv->$output_method }; | ||
| 349 | die "Invalid output type: '$args->{output_type}'" if $@; | ||
| 350 | |||
| 351 | # | ||
| 352 | # Process tables definitions, create nodes | ||
| 353 | # | ||
| 354 | my %nj_registry; # for locations of fields for natural joins | ||
| 355 | my @fk_registry; # for locations of fields for foreign keys | ||
| 356 | |||
| 357 | TABLE: | ||
| 358 | for my $table ($schema->get_tables) { | ||
| 359 | |||
| 360 | my $table_name = $table->name; | ||
| 361 | if (@skip_tables_like or keys %skip_tables) { | ||
| 362 | next TABLE if $skip_tables{$table_name}; | ||
| 363 | for my $regex (@skip_tables_like) { | ||
| 364 | next TABLE if $table_name =~ $regex; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | my @fields = $table->get_fields; | ||
| 369 | if ($args->{show_fk_only}) { | ||
| 370 | @fields = grep { $_->is_foreign_key } @fields; | ||
| 371 | } | ||
| 372 | |||
| 373 | my $field_str = ''; | ||
| 374 | if ($args->{show_fields}) { | ||
| 375 | my @fmt_fields; | ||
| 376 | for my $field (@fields) { | ||
| 377 | |||
| 378 | my $field_info; | ||
| 379 | if ($args->{show_datatypes}) { | ||
| 380 | |||
| 381 | my $field_type = $field->data_type; | ||
| 382 | my $size = $field->size; | ||
| 383 | |||
| 384 | if ( $args->{friendly_ints} | ||
| 385 | && $size | ||
| 386 | && (lc($field_type) eq 'integer')) { | ||
| 387 | # Automatically translate to int2, int4, int8 | ||
| 388 | # Type (Bits) Max. Signed/Unsigned Length | ||
| 389 | # tinyint* (8) 128 3 | ||
| 390 | # 255 3 | ||
| 391 | # smallint (16) 32767 5 | ||
| 392 | # 65535 5 | ||
| 393 | # mediumint* (24) 8388607 7 | ||
| 394 | # 16777215 8 | ||
| 395 | # int (32) 2147483647 10 | ||
| 396 | # 4294967295 11 | ||
| 397 | # bigint (64) 9223372036854775807 19 | ||
| 398 | # 18446744073709551615 20 | ||
| 399 | # | ||
| 400 | # * tinyint and mediumint are nonstandard extensions which are | ||
| 401 | # only available under MySQL (to my knowledge) | ||
| 402 | if ($size <= 3 and $args->{friendly_ints_extended}) { | ||
| 403 | $field_type = 'tinyint'; | ||
| 404 | } elsif ($size <= 5) { | ||
| 405 | $field_type = 'smallint'; | ||
| 406 | } elsif ($size <= 8 and $args->{friendly_ints_extended}) { | ||
| 407 | $field_type = 'mediumint'; | ||
| 408 | } elsif ($size <= 11) { | ||
| 409 | $field_type = 'integer'; | ||
| 410 | } else { | ||
| 411 | $field_type = 'bigint'; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | $field_info = $field_type; | ||
| 416 | if ( $args->{show_sizes} | ||
| 417 | && $size | ||
| 418 | && ($field_type =~ /^ (?: NUMERIC | DECIMAL | (VAR)?CHAR2? ) $/ix)) { | ||
| 419 | $field_info .= '(' . $size . ')'; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | my $constraints; | ||
| 424 | if ($args->{show_constraints}) { | ||
| 425 | my @constraints; | ||
| 426 | push(@constraints, $field->is_auto_increment ? 'PA' : 'PK') | ||
| 427 | if $field->is_primary_key; | ||
| 428 | push(@constraints, 'FK') if $field->is_foreign_key; | ||
| 429 | push(@constraints, 'U') if $field->is_unique; | ||
| 430 | push(@constraints, 'N') if $field->is_nullable; | ||
| 431 | |||
| 432 | $constraints = join(',', @constraints); | ||
| 433 | } | ||
| 434 | |||
| 435 | # construct the field line from all info gathered so far | ||
| 436 | push @fmt_fields, join(' ', '-', $field->name, $field_info || (), $constraints ? "[$constraints]" : (),); | ||
| 437 | } | ||
| 438 | |||
| 439 | # join field lines with graphviz formatting | ||
| 440 | $field_str = join('\l', @fmt_fields) . '\l'; | ||
| 441 | |||
| 442 | } | ||
| 443 | |||
| 444 | my $index_str = ''; | ||
| 445 | if ($args->{show_indexes}) { | ||
| 446 | |||
| 447 | my @fmt_indexes; | ||
| 448 | for my $index ($table->get_indices) { | ||
| 449 | next unless $index->is_valid; | ||
| 450 | |||
| 451 | push @fmt_indexes, | ||
| 452 | join(' ', | ||
| 453 | '*', | ||
| 454 | $args->{show_index_names} | ||
| 455 | ? $index->name . ':' | ||
| 456 | : (), | ||
| 457 | join(', ', $index->fields), | ||
| 458 | ($index->type eq 'UNIQUE') ? '[U]' : (), | ||
| 459 | ); | ||
| 460 | } | ||
| 461 | |||
| 462 | # join index lines with graphviz formatting (if any indexes at all) | ||
| 463 | $index_str = join('\l', @fmt_indexes) . '\l' if @fmt_indexes; | ||
| 464 | } | ||
| 465 | |||
| 466 | my $name_str = $table_name . '\n'; | ||
| 467 | |||
| 468 | # escape spaces | ||
| 469 | for ($name_str, $field_str, $index_str) { | ||
| 470 | $_ =~ s/ /\\ /g; | ||
| 471 | } | ||
| 472 | |||
| 473 | my $node_args; | ||
| 474 | |||
| 475 | # only the 'record' type supports nice formatting | ||
| 476 | if ($args->{node}{shape} eq 'record') { | ||
| 477 | |||
| 478 | # the necessity to supply shape => 'record' is a graphviz bug | ||
| 479 | $node_args = { | ||
| 480 | shape => 'record', | ||
| 481 | label => sprintf('{%s}', join('|', $name_str, $field_str || (), $index_str || (),),), | ||
| 482 | }; | ||
| 483 | } else { | ||
| 484 | my $sep = sprintf('%s\n', '-' x ((length $table_name) + 2)); | ||
| 485 | |||
| 486 | $node_args = { label => join($sep, $name_str, $field_str || (), $index_str || (),), }; | ||
| 487 | } | ||
| 488 | |||
| 489 | if (my $cluster_name = $cluster{$table_name}) { | ||
| 490 | $node_args->{cluster} = $cluster_name; | ||
| 491 | } | ||
| 492 | |||
| 493 | $gv->add_node(qq["$table_name"], %$node_args); | ||
| 494 | |||
| 495 | debug("Processing table '$table_name'"); | ||
| 496 | |||
| 497 | debug("Fields = ", join(', ', map { $_->name } @fields)) if $DEBUG; | ||
| 498 | |||
| 499 | for my $f (@fields) { | ||
| 500 | my $name = $f->name or next; | ||
| 501 | my $is_pk = $f->is_primary_key; | ||
| 502 | my $is_unique = $f->is_unique; | ||
| 503 | |||
| 504 | # | ||
| 505 | # Decide if we should skip this field. | ||
| 506 | # | ||
| 507 | if ($args->{natural_join}) { | ||
| 508 | next unless $is_pk || $f->is_foreign_key; | ||
| 509 | } | ||
| 510 | |||
| 511 | my $constraints = $f->{'constraints'}; | ||
| 512 | |||
| 513 | if ($args->{natural_join} && !$skip_fields{$name}) { | ||
| 514 | push @{ $nj_registry{$name} }, $table_name; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | unless ($args->{natural_join}) { | ||
| 519 | for my $c ($table->get_constraints) { | ||
| 520 | next unless $c->type eq FOREIGN_KEY; | ||
| 521 | my $fk_table = $c->reference_table or next; | ||
| 522 | |||
| 523 | for my $field_name ($c->fields) { | ||
| 524 | for my $fk_field ($c->reference_fields) { | ||
| 525 | next unless defined $schema->get_table($fk_table); | ||
| 526 | |||
| 527 | # a condition is optional if at least one fk is nullable | ||
| 528 | push @fk_registry, [ $table_name, $fk_table, scalar(grep { $_->is_nullable } ($c->fields)) ]; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | # | ||
| 536 | # Process relationships, create edges | ||
| 537 | # | ||
| 538 | my (@table_bunches, %optional_constraints); | ||
| 539 | if ($args->{natural_join}) { | ||
| 540 | for my $field_name (keys %nj_registry) { | ||
| 541 | my @table_names = @{ $nj_registry{$field_name} || [] } or next; | ||
| 542 | next if scalar @table_names == 1; | ||
| 543 | push @table_bunches, [@table_names]; | ||
| 544 | } | ||
| 545 | } else { | ||
| 546 | for my $i (0 .. $#fk_registry) { | ||
| 547 | my $fk = $fk_registry[$i]; | ||
| 548 | push @table_bunches, [ $fk->[0], $fk->[1] ]; | ||
| 549 | $optional_constraints{$i} = $fk->[2]; | ||
| 550 | } | ||
| 551 | } | ||
| 552 | |||
| 553 | my %done; | ||
| 554 | for my $bi (0 .. $#table_bunches) { | ||
| 555 | my @tables = @{ $table_bunches[$bi] }; | ||
| 556 | |||
| 557 | for my $i (0 .. $#tables) { | ||
| 558 | my $table1 = $tables[$i]; | ||
| 559 | for my $j (1 .. $#tables) { | ||
| 560 | next if $i == $j; | ||
| 561 | my $table2 = $tables[$j]; | ||
| 562 | next if $done{$table1}{$table2}; | ||
| 563 | debug("Adding edge '$table2' -> '$table1'"); | ||
| 564 | $gv->add_edge( | ||
| 565 | qq["$table2"], qq["$table1"], arrowhead => $optional_constraints{$bi} | ||
| 566 | ? 'empty' | ||
| 567 | : 'normal', | ||
| 568 | ); | ||
| 569 | $done{$table1}{$table2} = 1; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | # | ||
| 575 | # Print the image | ||
| 576 | # | ||
| 577 | if (my $out = $args->{out_file}) { | ||
| 578 | if (openhandle($out)) { | ||
| 579 | print $out $gv->$output_method; | ||
| 580 | } else { | ||
| 581 | open my $fh, '>', $out or die "Can't write '$out': $!\n"; | ||
| 582 | binmode $fh; | ||
| 583 | print $fh $gv->$output_method; | ||
| 584 | close $fh; | ||
| 585 | } | ||
| 586 | } else { | ||
| 587 | return $gv->$output_method; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | 1; | ||
| 592 | |||
| 593 | =pod | ||
| 594 | |||
| 595 | =head1 AUTHOR | ||
| 596 | |||
| 597 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt> | ||
| 598 | |||
| 599 | Jonathan Yu E<lt>frequency@cpan.orgE<gt> | ||
| 600 | |||
| 601 | =head1 SEE ALSO | ||
| 602 | |||
| 603 | SQL::Translator, GraphViz | ||
| 604 | |||
| 605 | =cut |