package SQL::Translator::Producer::Diagram;
# -------------------------------------------------------------------
-# $Id: Diagram.pm,v 1.1 2003-04-24 16:36:49 kycl4rk Exp $
+# $Id$
# -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
+# Copyright (C) 2002-4 SQLFairy Authors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# 02111-1307 USA
# -------------------------------------------------------------------
+=head1 NAME
+
+SQL::Translator::Producer::Diagram - ER diagram producer for SQL::Translator
+
+=head1 SYNOPSIS
+
+Use via SQL::Translator:
+
+ use SQL::Translator;
+
+ my $t = SQL::Translator->new( producer => 'Diagram', '...' );
+ $t->translate;
+
+=cut
+
use strict;
use GD;
use Data::Dumper;
+use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(debug);
use vars qw[ $VERSION $DEBUG ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use constant VALID_FONT_SIZE => {
};
sub produce {
- my ($t, $data) = @_;
+ my $t = shift;
+ my $schema = $t->schema;
my $args = $t->producer_args;
local $DEBUG = $t->debug;
- debug("Data =\n", Dumper( $data ));
+ debug("Schema =\n", Dumper( $schema ));
debug("Producer args =\n", Dumper( $args ));
- my $out_file = $args->{'out_file'} || '';
- my $image_type = $args->{'image_type'} || 'png';
- my $title = $args->{'title'} || $t->filename;
- my $font_size = $args->{'font_size'} || 'medium';
+ my $out_file = $args->{'out_file'} || '';
+ my $output_type = $args->{'output_type'} || 'png';
+ my $title = $args->{'title'} || $t->filename;
+ my $font_size = $args->{'font_size'} || 'medium';
+ my $imap_file = $args->{'imap_file'} || '';
+ my $imap_url = $args->{'imap_url'} || '';
+ my $gutter = $args->{'gutter'} || 30; # distance b/w columns
my $no_columns = $args->{'no_columns'};
my $no_lines = $args->{'no_lines'};
my $add_color = $args->{'add_color'};
my $join_pk_only = $args->{'join_pk_only'};
my $natural_join = $args->{'natural_join'} || $join_pk_only;
my $skip_fields = $args->{'skip_fields'};
- my %skip = map { $_, 1 } split ( /,/, $skip_fields );
+ my %skip = map { s/^\s+|\s+$//g; $_,1 } split (/,/, $skip_fields);
+
+# my @tables = $schema->get_tables;
+ my @table_names;
+ if ( $natural_join ) {
+ $schema->make_natural_joins(
+ join_pk_only => $join_pk_only,
+ skip_fields => $args->{'skip_fields'},
+ );
- die "Invalid image type '$image_type'"
- unless VALID_IMAGE_TYPE ->{ $image_type };
+ my $g = $schema->as_graph_pm;
+ my $d = Graph::Traversal::DFS->new( $g, next_alphabetic => 1 );
+ $d->preorder;
+
+ @table_names = $d->dfs;
+ }
+ else {
+ @table_names = map { $_->name } $schema->get_tables;
+ }
+
+ die "Invalid image type '$output_type'"
+ unless VALID_IMAGE_TYPE ->{ $output_type };
die "Invalid font size '$font_size'"
unless VALID_FONT_SIZE->{ $font_size };
#
# Layout the image.
#
- my $font =
- $font_size eq 'small' ? gdTinyFont :
- $font_size eq 'medium' ? gdSmallFont :
- $font_size eq 'large' ? gdLargeFont : gdGiantFont;
- my $no_tables = scalar keys %$data;
+ my $font
+ = $font_size eq 'small' ? gdTinyFont
+ : $font_size eq 'medium' ? gdSmallFont
+ : $font_size eq 'large' ? gdLargeFont
+ : gdGiantFont;
+
+ my $no_tables = scalar @table_names;
+ $no_columns = 0 unless $no_columns =~ /^\d+$/;
$no_columns ||= sprintf( "%.0f", sqrt( $no_tables ) + .5 );
$no_columns ||= .5;
my $no_per_col = sprintf( "%.0f", $no_tables/$no_columns + .5 );
my $cur_col = 1; # the current column
my $no_this_col = 0; # number of tables in current column
my $this_col_x = $x; # current column's x
- my $gutter = 30; # distance b/w columns
my %nj_registry; # for locations of fields for natural joins
my @fk_registry; # for locations of fields for foreign keys
my %table_x; # for max x of each table
my $field_no; # counter to give distinct no. to each field
+ my %coords; # holds fields coordinates
+ my @imap_coords; # for making clickable image map
my %legend;
- #
- # If necessary, pre-process fields to find foreign keys.
- #
- if ( $show_fk_only && $natural_join ) {
- my ( %common_keys, %pk );
- for my $table ( values %$data ) {
- for my $index (
- @{ $table->{'indices'} || [] },
- @{ $table->{'constraints'} || [] },
- ) {
- my @fields = @{ $index->{'fields'} || [] } or next;
- if ( $index->{'type'} eq 'primary_key' ) {
- $pk{ $_ } = 1 for @fields;
- }
- }
-
- for my $field ( values %{ $table->{'fields'} } ) {
- push @{ $common_keys{ $field->{'name'} } },
- $table->{'table_name'};
- }
- }
-
- for my $field ( keys %common_keys ) {
- my @tables = @{ $common_keys{ $field } };
- next unless scalar @tables > 1;
- for my $table ( @tables ) {
- next if $join_pk_only and !defined $pk{ $field };
- $data->{ $table }{'fields'}{ $field }{'is_fk'} = 1;
- }
- }
- }
- else {
- for my $table ( values %$data ) {
- for my $field ( values %{ $table->{'fields'} } ) {
- for my $constraint (
- grep { $_->{'type'} eq 'foreign_key' }
- @{ $field->{'constraints'} }
- ) {
- my $ref_table = $constraint->{'reference_table'} or next;
- my @ref_fields = @{$constraint->{'reference_fields'} || []};
-
- unless ( @ref_fields ) {
- for my $field (
- values %{ $data->{ $ref_table }{'fields'} }
- ) {
- for my $pk (
- grep { $_->{'type'} eq 'primary_key' }
- @{ $field->{'constraints'} }
- ) {
- push @ref_fields, @{ $pk->{'fields'} };
- }
- }
-
- $constraint->{'reference_fields'} = [ @ref_fields ];
- }
-
- for my $ref_field (
- @{ $constraint->{'reference_fields'} }
- ) {
- $data->{$ref_table}{'fields'}{$ref_field}{'is_fk'} = 1;
- }
- }
- }
- }
- }
-
-
- for my $table (
- map { $_->[1] }
- sort { $a->[0] <=> $b->[0] }
- map { [ $_->{'order'}, $_ ] }
- values %$data
- ) {
- my $table_name = $table->{'table_name'};
- my $top = $y;
+ for my $table_name ( @table_names ) {
+ my $table = $schema->get_table( $table_name );
+ my $top = $y;
push @shapes,
[ 'string', $font, $this_col_x, $y, $table_name, 'black' ];
-
$y += $font->height + 2;
my $below_table_name = $y;
$y += 2;
debug("Processing table '$table_name'");
- my @fields =
- map { $_->[1] }
- sort { $a->[0] <=> $b->[0] }
- map { [ $_->{'order'}, $_ ] }
- values %{ $table->{'fields'} };
-
- debug("Fields = ", join(', ', map { $_->{'name'} } @fields));
-
- my ( %pk, %unique );
- for my $index (
- @{ $table->{'indices'} || [] },
- @{ $table->{'constraints'} || [] },
- ) {
- my @fields = @{ $index->{'fields'} || [] } or next;
- if ( $index->{'type'} eq 'primary_key' ) {
- $pk{ $_ } = 1 for @fields;
- }
- elsif ( $index->{'type'} eq 'unique' ) {
- $unique{ $_ } = 1 for @fields;
- }
- }
-
- debug("PK = ", join(', ', sort keys %pk)) if %pk;
- debug("Unique = ", join(', ', sort keys %unique)) if %unique;
+ my @fields = $table->get_fields;
+ debug("Fields = ", join(', ', map { $_->name } @fields));
- my ( @fld_desc, $max_name );
+ my ( @fld_desc, $max_name, $max_desc );
for my $f ( @fields ) {
- my $name = $f->{'name'} or next;
- my $is_pk = $pk{ $name };
- my $is_unique = $unique{ $name };
+ my $name = $f->name or next;
+ my $is_pk = $f->is_primary_key;
+
+ my @attr;
#
# Decide if we should skip this field.
#
if ( $show_fk_only ) {
- if ( $natural_join ) {
- next unless $is_pk || $f->{'is_fk'};
- }
- else {
- next unless $is_pk || $f->{'is_fk'} ||
- grep { $_->{'type'} eq 'foreign_key' }
- @{ $f->{'constraints'} }
- ;
- }
+ next unless $is_pk || $f->is_foreign_key;
}
if ( $is_pk ) {
- $name .= ' *';
- $legend{'Primary key'} = '*';
+ push @attr, 'PK';
+ $legend{'Primary key'} = '[PK]';
}
- elsif ( $is_unique ) {
- $name .= ' [U]';
+
+ if ( $f->is_unique ) {
+ push @attr, 'U';
$legend{'Unique constraint'} = '[U]';
}
- my $size = @{ $f->{'size'} || [] }
- ? '(' . join( ',', @{ $f->{'size'} } ) . ')'
- : '';
- my $desc = join( ' ', map { $_ || () } $f->{'data_type'}, $size );
+ if ( $f->is_foreign_key ) {
+ push @attr, 'FK';
+ $legend{'Foreign Key'} = '[FK]';
+ }
+
+ my $attr = '';
+ if ( @attr ) {
+ $attr .= '[' . join(', ', @attr) . ']';
+ }
+
+ my $desc = $f->data_type;
+ $desc .= '('.$f->size.')' if $f->size &&
+ $f->data_type =~ /^(VAR)?CHAR2?$/i;
my $nlen = length $name;
+ my $dlen = length $desc;
$max_name = $nlen if $nlen > $max_name;
- push @fld_desc, [ $name, $desc, $f->{'name'}, $is_pk ];
+ $max_desc = $dlen if $dlen > $max_desc;
+ push @fld_desc, [ $name, $desc, $f->{'name'}, $is_pk, $attr ];
}
- $max_name += 4;
+ $max_name += 2;
+ $max_desc += 2;
for my $fld_desc ( @fld_desc ) {
- my ( $name, $desc, $orig_name, $is_pk ) = @$fld_desc;
- my $diff = $max_name - length $name;
- $name .= ' ' x $diff;
- $desc = $name . $desc;
+ my ( $name, $desc, $orig_name, $is_pk, $attr ) = @$fld_desc;
+ my $diff1 = $max_name - length $name;
+ my $diff2 = $max_desc - length $desc;
+ $name .= ' ' x $diff1;
+ $desc .= ' ' x $diff2;
+ $desc = $name . $desc . $attr;
push @shapes, [ 'string', $font, $this_col_x, $y, $desc, 'black' ];
$y += $font->height + 2;
if ( $natural_join && !$skip{ $orig_name } ) {
push @{ $nj_registry{ $orig_name } }, $table_name;
}
- elsif ( @{ $constraints || [] } ) {
- for my $constraint ( @$constraints ) {
- next unless $constraint->{'type'} eq 'foreign_key';
- for my $fk_field (
- @{ $constraint->{'reference_fields'} || [] }
- ) {
- my $fk_table = $constraint->{'reference_table'};
- next unless defined $data->{ $fk_table };
- push @fk_registry, [
- [ $table_name, $orig_name ],
- [ $fk_table , $fk_field ],
- ];
- }
- }
- }
my $y_link = $y - $font->height/2;
- $table->{'fields'}{ $orig_name }{'coords'} = {
+ $coords{ $table_name }{ $orig_name }{'coords'} = {
left => [ $this_col_x - 6, $y_link ],
right => [ $length + 2 , $y_link ],
table => $table_name,
is_pk => $is_pk,
fld_name => $orig_name,
};
+
+ push @imap_coords, [
+ $imap_url."#$table_name-$orig_name",
+ $this_col_x, $y - $font->height, $length, $y_link,
+ ];
+ }
+
+ unless ( $natural_join ) {
+ for my $c ( $table->get_constraints ) {
+ next unless $c->type eq FOREIGN_KEY;
+ my $fk_table = $c->reference_table or next;
+
+ for my $field_name ( $c->fields ) {
+ for my $fk_field ( $c->reference_fields ) {
+ next unless defined $schema->get_table( $fk_table );
+ push @fk_registry, [
+ [ $fk_table , $fk_field ],
+ [ $table_name, $field_name ],
+ ];
+ }
+ }
+ }
}
$this_max_x += 5;
];
unshift @shapes, [ 'filledRectangle', @bounds, 'white' ];
}
+
+ push @imap_coords, [
+ $imap_url."#$table_name",
+ $bounds[0], $bounds[1], $this_max_x, $below_table_name,
+ ];
+
push @shapes, [ 'rectangle', @bounds, 'black' ];
$max_x = $this_max_x if $this_max_x > $max_x;
$y += 25;
for my $table_name ( @table_names ) {
push @positions,
- $data->{$table_name}{'fields'}{ $field_name }{'coords'};
+ $coords{ $table_name }{ $field_name }{'coords'};
}
push @position_bunches, [ @positions ];
else {
for my $pair ( @fk_registry ) {
push @position_bunches, [
- $data->{$pair->[0][0]}{'fields'}{ $pair->[0][1] }{'coords'},
- $data->{$pair->[1][0]}{'fields'}{ $pair->[1][1] }{'coords'},
+ $coords{$pair->[0][0]}{ $pair->[0][1] }{'coords'},
+ $coords{$pair->[1][0]}{ $pair->[1][1] }{'coords'},
];
}
}
}
}
- my $sig = __PACKAGE__." $VERSION";
+ my $sig = 'Created by SQL::Translator ' . $t->version;
my $sig_len = $font->width * length $sig;
push @shapes, [
'string', $font, $max_x - $sig_len, $max_y - $font->height - 4,
# Render the image.
#
my $gd = GD::Image->new( $max_x + 30, $max_y );
- unless ( $gd->can( $image_type ) ) {
- die "GD can't create images of type '$image_type'\n";
+ unless ( $gd->can( $output_type ) ) {
+ die "GD can't create images of type '$output_type'\n";
}
my %colors = map { $_->[0], $gd->colorAllocate( @{$_->[1]} ) } (
[ white => [ 255, 255, 255 ] ],
}
#
+ # Make image map.
+ #
+ debug("imap file = '$imap_file'");
+ if ( $imap_file && @imap_coords ) {
+ open my $fh, ">$imap_file" or die "Can't write '$imap_file': $!\n";
+ print $fh qq[<html><body><img src="" usemap="#imap" border="0">\n].
+ qq[<map name="imap">\n];
+ for my $rec ( @imap_coords ) {
+ my $href = shift @$rec;
+ print $fh q[<area coords="].join(',', @$rec).qq[" href="$href">\n];
+ }
+ print $fh qq[</body></html>];
+ close $fh;
+ }
+
+ #
# Print the image.
#
if ( $out_file ) {
open my $fh, ">$out_file" or die "Can't write '$out_file': $!\n";
- print $fh $gd->$image_type;
+ print $fh $gd->$output_type;
close $fh;
}
else {
- print $gd->$image_type;
+ return $gd->$output_type;
}
}
1;
-=pod
-
-=head1 NAME
+# -------------------------------------------------------------------
-SQL::Translator::Producer::Diagram - ER diagram producer for SQL::Translator
+=pod
=head1 AUTHOR
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
=cut