Moved POD around, added all options for all parsers and non-graphical
[dbsrgits/SQL-Translator.git] / bin / sqlt-diagram
CommitLineData
dd9550a4 1#!/usr/bin/perl
2
7d8b3489 3# $Id: sqlt-diagram,v 1.1 2003-08-26 02:29:12 kycl4rk Exp $
dd9550a4 4
5=head1 NAME
6
60fe3432 7sqlt-diagram.pl - Automatically create a diagram from a database schema
dd9550a4 8
9=head1 SYNOPSIS
10
60fe3432 11 ./sqlt-diagram.pl -d|--db=db_parser [options] schema.sql
dd9550a4 12
13 Options:
14
2621b0fc 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,"
21 default "medium")
22 --color Add colors
23 --show-fk-only Only show fields that act as primary
24 or foreign keys
25
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
dd9550a4 30
31=head1 DESCRIPTION
32
33This script will create a picture of your schema. Only the database
aa0a19b9 34driver argument (for SQL::Translator) is required. If no output file
35name is given, then image will be printed to STDOUT, so you should
36redirect the output into a file.
dd9550a4 37
14655604 38The default action is to assume the presence of foreign key
39relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
40the tables. If you are parsing the schema of a file that does not
41have these, you will find the natural join options helpful. With
42natural joins, like-named fields will be considered foreign keys.
43This can prove too permissive, however, as you probably don't want a
44field called "name" to be considered a foreign key, so you could
45include it in the "skip" option, and all fields called "name" will be
46excluded from natural joins. A more efficient method, however, might
47be to simply deduce the foriegn keys from primary keys to other fields
2621b0fc 48named the same in other tables. Use the "natural-join-pk" option
14655604 49to acheive this.
50
dd9550a4 51=cut
52
53use strict;
2621b0fc 54use Data::Dumper;
dd9550a4 55use Getopt::Long;
dd9550a4 56use Pod::Usage;
57use SQL::Translator;
58
7d8b3489 59my $VERSION = (qw$Revision: 1.1 $)[-1];
2621b0fc 60
aa0a19b9 61#
62# Get arguments.
63#
14655604 64my (
65 $out_file, $image_type, $db_driver, $title, $no_columns,
2621b0fc 66 $no_lines, $font_size, $add_color, $debug, $show_fk_only,
a9208a9a 67 $natural_join, $join_pk_only, $skip_fields, $help
14655604 68);
69
dd9550a4 70GetOptions(
2621b0fc 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,
83 'debug' => \$debug,
a9208a9a 84 'h|help' => \$help,
dd9550a4 85) or die pod2usage;
a9208a9a 86my @files = @ARGV; # the create script(s) for the original db
dd9550a4 87
a9208a9a 88pod2usage(1) if $help;
dd9550a4 89pod2usage( -message => "No db driver specified" ) unless $db_driver;
a9208a9a 90pod2usage( -message => 'No input file' ) unless @files;
91
92my $translator = SQL::Translator->new(
93 from => $db_driver,
94 to => 'Diagram',
95 debug => $debug || 0,
96 producer_args => {
97 out_file => $out_file,
98 image_type => $image_type,
99 title => $title,
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,
108 },
109) or die SQL::Translator->error;
110
111for my $file (@files) {
112 my $output = $translator->translate( $file ) or die
113 "Error: " . $translator->error;
114 if ( $out_file ) {
115 print "Image written to '$out_file'. Done.\n";
14655604 116 }
117 else {
a9208a9a 118 print $output;
14655604 119 }
dd9550a4 120}
121
122=pod
123
124=head1 AUTHOR
125
126Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
127
128=cut