Fixed bug (illegal div by 0) if "no_columns" wasn't numeric, also fixed
[dbsrgits/SQL-Translator.git] / bin / auto-graph.pl
1 #!/usr/bin/perl
2
3 # $Id: auto-graph.pl,v 1.2 2003-04-24 16:14:07 kycl4rk Exp $
4
5 =head1 NAME 
6
7 auto-graph.pl - Automatically create a graph from a database schema
8
9 =head1 SYNOPSIS
10
11   ./auto-graph.pl -d|--db=db_parser [options] schema.sql
12
13   Options:
14
15     -l|--layout        Layout schema for GraphViz
16                        ("dot," "neato," "twopi"; default "dot")
17     -n|--node-shape    Shape of the nodes ("record," "plaintext," 
18                        "ellipse," "circle," "egg," "triangle," "box," 
19                        "diamond," "trapezium," "parallelogram," "house," 
20                        "hexagon," "octagon," default "ellipse")
21     -o|--output        Output file name (default STDOUT)
22     -t|--output-type   Output file type ("canon", "text," "ps," "hpgl,"
23                        "pcl," "mif," "pic," "gd," "gd2," "gif," "jpeg,"
24                        "png," "wbmp," "cmap," "ismap," "imap," "vrml,"
25                        "vtx," "mp," "fig," "svg," "plain," default "png")
26     --color            Add colors
27     --natural-join     Perform natural joins
28     --natural-join-pk  Perform natural joins from primary keys only
29     -s|--skip          Fields to skip in natural joins
30     --debug            Print debugging information
31
32 =head1 DESCRIPTION
33
34 This script will create a graph of your schema.  Only the database
35 driver argument (for SQL::Translator) is required.  If no output file
36 name is given, then image will be printed to STDOUT, so you should
37 redirect the output into a file.
38
39 The default action is to assume the presence of foreign key
40 relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
41 the tables.  If you are parsing the schema of a file that does not
42 have these, you will find the natural join options helpful.  With
43 natural joins, like-named fields will be considered foreign keys.
44 This can prove too permissive, however, as you probably don't want a
45 field called "name" to be considered a foreign key, so you could
46 include it in the "skip" option, and all fields called "name" will be
47 excluded from natural joins.  A more efficient method, however, might
48 be to simply deduce the foriegn keys from primary keys to other fields
49 named the same in other tables.  Use the "natural-join-pk" option
50 to acheive this.
51
52 If the schema defines foreign keys, then the graph produced will be
53 directed showing the direction of the relationship.  If the foreign
54 keys are intuited via natural joins, the graph will be undirected.
55
56 =cut
57
58 use strict;
59 use Data::Dumper;
60 use Getopt::Long;
61 use GraphViz;
62 use Pod::Usage;
63 use SQL::Translator;
64
65 my $VERSION = (qw$Revision: 1.2 $)[-1];
66
67 #
68 # Get arguments.
69 #
70 my ( 
71     $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color, 
72     $natural_join, $join_pk_only, $skip_fields, $debug, $help
73 );
74
75 GetOptions(
76     'd|db=s'           => \$db_driver,
77     'o|output:s'       => \$out_file,
78     'l|layout:s'       => \$layout,
79     'n|node-shape:s'   => \$node_shape,
80     't|output-type:s'  => \$output_type,
81     'color'            => \$add_color,
82     'natural-join'     => \$natural_join,
83     'natural-join-pk'  => \$join_pk_only,
84     's|skip:s'         => \$skip_fields,
85     'debug'            => \$debug,
86     'h|help'           => \$help,
87 ) or die pod2usage;
88 my @files = @ARGV; # the create script(s) for the original db
89
90 pod2usage(1) if $help;
91 pod2usage( -message => "No db driver specified" ) unless $db_driver;
92 pod2usage( -message => 'No input file'          ) unless @files;
93
94 my $translator          =  SQL::Translator->new( 
95     from                => $db_driver,
96     to                  => 'GraphViz',
97     debug               => $debug || 0,
98     producer_args       => {
99         out_file        => $out_file,
100         layout          => $layout,
101         node_shape      => $node_shape,
102         output_type     => $output_type,
103         add_color       => $add_color,
104         natural_join    => $natural_join,
105         natural_join_pk => $join_pk_only,
106         skip_fields     => $skip_fields,
107     },
108 ) or die SQL::Translator->error;
109
110 for my $file (@files) {
111     my $output = $translator->translate( $file ) or die
112                  "Error: " . $translator->error;
113     if ( $out_file ) {
114         print "Image written to '$out_file'.  Done.\n";
115     }
116     else {
117         print $output;
118     }
119 }
120
121 =pod
122
123 =head1 AUTHOR
124
125 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
126
127 =cut