3 # $Id: sqlt-diagram.pl,v 1.2 2003-06-16 18:25:36 kycl4rk Exp $
7 sqlt-diagram.pl - Automatically create a diagram from a database schema
11 ./sqlt-diagram.pl -d|--db=db_parser [options] schema.sql
15 -o|--output Output file name (default STDOUT)
16 -i|--image Output image type ("png" or "jpeg," default "png")
17 -t|--title Title to give schema
18 -c|--cols Number of columns
19 -n|--no-lines Don't draw lines
20 -f|--font-size Font size ("small," "medium," "large," or "huge,"
23 --show-fk-only Only show fields that act as primary
26 --natural-join Perform natural joins
27 --natural-join-pk Perform natural joins from primary keys only
28 -s|--skip Fields to skip in natural joins
29 --debug Print debugging information
33 This script will create a picture of your schema. Only the database
34 driver argument (for SQL::Translator) is required. If no output file
35 name is given, then image will be printed to STDOUT, so you should
36 redirect the output into a file.
38 The default action is to assume the presence of foreign key
39 relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
40 the tables. If you are parsing the schema of a file that does not
41 have these, you will find the natural join options helpful. With
42 natural joins, like-named fields will be considered foreign keys.
43 This can prove too permissive, however, as you probably don't want a
44 field called "name" to be considered a foreign key, so you could
45 include it in the "skip" option, and all fields called "name" will be
46 excluded from natural joins. A more efficient method, however, might
47 be to simply deduce the foriegn keys from primary keys to other fields
48 named the same in other tables. Use the "natural-join-pk" option
59 my $VERSION = (qw$Revision: 1.2 $)[-1];
65 $out_file, $image_type, $db_driver, $title, $no_columns,
66 $no_lines, $font_size, $add_color, $debug, $show_fk_only,
67 $natural_join, $join_pk_only, $skip_fields, $help
71 'd|db=s' => \$db_driver,
72 'o|output:s' => \$out_file,
73 'i|image:s' => \$image_type,
74 't|title:s' => \$title,
75 'c|columns:i' => \$no_columns,
76 'n|no-lines' => \$no_lines,
77 'f|font-size:s' => \$font_size,
78 'color' => \$add_color,
79 'show-fk-only' => \$show_fk_only,
80 'natural-join' => \$natural_join,
81 'natural-join-pk' => \$join_pk_only,
82 's|skip:s' => \$skip_fields,
86 my @files = @ARGV; # the create script(s) for the original db
88 pod2usage(1) if $help;
89 pod2usage( -message => "No db driver specified" ) unless $db_driver;
90 pod2usage( -message => 'No input file' ) unless @files;
92 my $translator = SQL::Translator->new(
97 out_file => $out_file,
98 image_type => $image_type,
100 no_columns => $no_columns,
101 no_lines => $no_lines,
102 font_size => $font_size,
103 add_color => $add_color,
104 show_fk_only => $show_fk_only,
105 natural_join => $natural_join,
106 join_pk_only => $join_pk_only,
107 skip_fields => $skip_fields,
109 ) or die SQL::Translator->error;
111 for my $file (@files) {
112 my $output = $translator->translate( $file ) or die
113 "Error: " . $translator->error;
115 print "Image written to '$out_file'. Done.\n";
126 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>