Changes to quit using "SqlfXML."
[dbsrgits/SQL-Translator.git] / bin / sqlt-graph.pl
1 #!/usr/bin/perl
2
3 # $Id: sqlt-graph.pl,v 1.3 2003-08-21 02:48:04 kycl4rk Exp $
4
5 =head1 NAME 
6
7 sqlt-graph.pl - Automatically create a graph from a database schema
8
9 =head1 SYNOPSIS
10
11   ./sqlt-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 "record")
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     -c|--color         Add colors
27     --no-fields        Don't show field names
28     --height           Image height (in inches, default "11",
29                        set to "0" to undefine)
30     --width            Image width (in inches, default "8.5", 
31                        set to "0" to undefine)
32     --natural-join     Perform natural joins
33     --natural-join-pk  Perform natural joins from primary keys only
34     -s|--skip          Fields to skip in natural joins
35     --debug            Print debugging information
36
37 =head1 DESCRIPTION
38
39 This script will create a graph of your schema.  Only the database
40 driver argument (for SQL::Translator) is required.  If no output file
41 name is given, then image will be printed to STDOUT, so you should
42 redirect the output into a file.
43
44 The default action is to assume the presence of foreign key
45 relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
46 the tables.  If you are parsing the schema of a file that does not
47 have these, you will find the natural join options helpful.  With
48 natural joins, like-named fields will be considered foreign keys.
49 This can prove too permissive, however, as you probably don't want a
50 field called "name" to be considered a foreign key, so you could
51 include it in the "skip" option, and all fields called "name" will be
52 excluded from natural joins.  A more efficient method, however, might
53 be to simply deduce the foriegn keys from primary keys to other fields
54 named the same in other tables.  Use the "natural-join-pk" option
55 to acheive this.
56
57 If the schema defines foreign keys, then the graph produced will be
58 directed showing the direction of the relationship.  If the foreign
59 keys are intuited via natural joins, the graph will be undirected.
60
61 =cut
62
63 use strict;
64 use Data::Dumper;
65 use Getopt::Long;
66 use GraphViz;
67 use Pod::Usage;
68 use SQL::Translator;
69
70 my $VERSION = (qw$Revision: 1.3 $)[-1];
71
72 #
73 # Get arguments.
74 #
75 my ( 
76     $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color, 
77     $natural_join, $join_pk_only, $skip_fields, $debug, $help, $height, 
78     $width, $no_fields
79 );
80
81 GetOptions(
82     'd|db=s'           => \$db_driver,
83     'o|output:s'       => \$out_file,
84     'l|layout:s'       => \$layout,
85     'n|node-shape:s'   => \$node_shape,
86     't|output-type:s'  => \$output_type,
87     'height:i'         => \$height,
88     'width:i'          => \$width,
89     'c|color'          => \$add_color,
90     'no-fields'        => \$no_fields,
91     'natural-join'     => \$natural_join,
92     'natural-join-pk'  => \$join_pk_only,
93     's|skip:s'         => \$skip_fields,
94     'debug'            => \$debug,
95     'h|help'           => \$help,
96 ) or die pod2usage;
97 my @files = @ARGV; # the create script(s) for the original db
98
99 pod2usage(1) if $help;
100 pod2usage( -message => "No db driver specified" ) unless $db_driver;
101 pod2usage( -message => 'No input file'          ) unless @files;
102
103 my $translator          =  SQL::Translator->new( 
104     from                => $db_driver,
105     to                  => 'GraphViz',
106     debug               => $debug || 0,
107     producer_args       => {
108         out_file        => $out_file,
109         layout          => $layout,
110         node_shape      => $node_shape,
111         output_type     => $output_type,
112         add_color       => $add_color,
113         natural_join    => $natural_join,
114         natural_join_pk => $join_pk_only,
115         skip_fields     => $skip_fields,
116         height          => $height || 0,
117         width           => $width  || 0,
118         show_fields     => $no_fields ? 0 : 1,
119     },
120 ) or die SQL::Translator->error;
121
122 for my $file (@files) {
123     my $output = $translator->translate( $file ) or die
124                  "Error: " . $translator->error;
125     if ( $out_file ) {
126         print "Image written to '$out_file'.  Done.\n";
127     }
128     else {
129         print $output;
130     }
131 }
132
133 =pod
134
135 =head1 AUTHOR
136
137 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
138
139 =cut