Update the Free Software Foundation's address (RT#100531)
[dbsrgits/SQL-Translator.git] / script / sqlt-graph
1 #!/usr/bin/env perl
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301 USA.
19 # -------------------------------------------------------------------
20
21 =head1 NAME
22
23 sqlt-graph - Automatically create a graph from a database schema
24
25 =head1 SYNOPSIS
26
27   ./sqlt-graph -d|--db|-f|--from=db_parser [options] schema.sql
28
29   Options:
30
31     -l|--layout        Layout schema for GraphViz
32                        ("dot," "neato," "twopi"; default "dot")
33     -n|--node-shape    Shape of the nodes ("record," "plaintext,"
34                        "ellipse," "circle," "egg," "triangle," "box,"
35                        "diamond," "trapezium," "parallelogram," "house,"
36                        "hexagon," "octagon," default "record")
37     -o|--output        Output file name (default STDOUT)
38     -t|--output-type   Output file type ("canon", "text," "ps," "hpgl,"
39                        "pcl," "mif," "pic," "gd," "gd2," "gif," "jpeg,"
40                        "png," "wbmp," "cmap," "ismap," "imap," "vrml,"
41                        "vtx," "mp," "fig," "svg," "plain," default "png")
42     -c|--color         Add colors
43     --cluster          Cluster tables
44     --no-fields        Don't show field names
45     --height           Image height (in inches, default "11",
46                        set to "0" to undefine)
47     --width            Image width (in inches, default "8.5",
48                        set to "0" to undefine)
49     --fontsize         custom font size for node and edge labels
50     --fontname         name of custom font (or full path to font file) for
51                        node, edge, and graph labels
52     --nodeattr         attribute name and value (in key=val syntax) for
53                        nodes; this option may be repeated to specify
54                        multiple node attributes
55     --edgeattr         same as --nodeattr, but for edge attributes
56     --graphattr        same as --nodeattr, but for graph attributes
57     --natural-join     Perform natural joins
58     --natural-join-pk  Perform natural joins from primary keys only
59     --show-datatypes   Show datatype of each field
60     --show-sizes       Show column sizes for VARCHAR and CHAR fields
61     --show-constraints Show list of constraints for each field
62     -s|--skip          Fields to skip in natural joins
63     --skip-tables      Comma-separated list of table names to exclude
64     --skip-tables-like Comma-separated list of regexen to exclude tables
65     --debug            Print debugging information
66     --trace            Print parser trace info
67
68 =head1 DESCRIPTION
69
70 This script will create a graph of your schema.  Only the database
71 driver argument (for SQL::Translator) is required.  If no output file
72 name is given, then image will be printed to STDOUT, so you should
73 redirect the output into a file.
74
75 The default action is to assume the presence of foreign key
76 relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
77 the tables.  If you are parsing the schema of a file that does not
78 have these, you will find the natural join options helpful.  With
79 natural joins, like-named fields will be considered foreign keys.
80 This can prove too permissive, however, as you probably don't want a
81 field called "name" to be considered a foreign key, so you could
82 include it in the "skip" option, and all fields called "name" will be
83 excluded from natural joins.  A more efficient method, however, might
84 be to simply deduce the foreign keys from primary keys to other fields
85 named the same in other tables.  Use the "natural-join-pk" option
86 to achieve this.
87
88 If the schema defines foreign keys, then the graph produced will be
89 directed showing the direction of the relationship.  If the foreign
90 keys are intuited via natural joins, the graph will be undirected.
91
92 Clustering of tables allows you to group and box tables according to
93 function or domain or whatever criteria you choose.  The syntax for
94 clustering tables is:
95
96   cluster1=table1,table2;cluster2=table3,table4
97
98 =cut
99
100 # -------------------------------------------------------------------
101
102 use strict;
103 use warnings;
104 use Data::Dumper;
105 use Getopt::Long;
106 use GraphViz;
107 use Pod::Usage;
108 use SQL::Translator;
109
110 use vars '$VERSION';
111 $VERSION = '1.59';
112
113 #
114 # Get arguments.
115 #
116 my (
117     $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
118     $natural_join, $join_pk_only, $skip_fields, $show_datatypes,
119     $show_sizes, $show_constraints, $debug, $help, $height, $width,
120     $no_fields, $fontsize, $fontname, $skip_tables, $skip_tables_like,
121     $cluster, $trace
122 );
123
124 # multi-valued options:
125 my %edgeattrs = ();
126 my %nodeattrs = ();
127 my %graphattrs = ();
128
129 GetOptions(
130     'd|db|f|from=s'      => \$db_driver,
131     'o|output:s'         => \$out_file,
132     'l|layout:s'         => \$layout,
133     'n|node-shape:s'     => \$node_shape,
134     't|output-type:s'    => \$output_type,
135     'height:f'           => \$height,
136     'width:f'            => \$width,
137     'fontsize=i'         => \$fontsize,
138     'fontname=s'         => \$fontname,
139     'nodeattr=s'         => \%nodeattrs,
140     'edgeattr=s'         => \%edgeattrs,
141     'graphattr=s'        => \%graphattrs,
142     'c|color'            => \$add_color,
143     'cluster:s'          => \$cluster,
144     'no-fields'          => \$no_fields,
145     'natural-join'       => \$natural_join,
146     'natural-join-pk'    => \$join_pk_only,
147     's|skip:s'           => \$skip_fields,
148     'skip-tables:s'      => \$skip_tables,
149     'skip-tables-like:s' => \$skip_tables_like,
150     'show-datatypes'     => \$show_datatypes,
151     'show-sizes'         => \$show_sizes,
152     'show-constraints'   => \$show_constraints,
153     'debug'              => \$debug,
154     'trace'              => \$trace,
155     'h|help'             => \$help,
156 ) or die pod2usage;
157 my @files = @ARGV; # the create script(s) for the original db
158
159 pod2usage(1) if $help;
160 pod2usage( -message => "No db driver specified" ) unless $db_driver;
161 pod2usage( -message => 'No input file'          ) unless @files;
162
163 my $translator           =  SQL::Translator->new(
164     from                 => $db_driver,
165     to                   => 'GraphViz',
166     debug                => $debug || 0,
167     trace                => $trace || 0,
168     producer_args        => {
169         out_file         => $out_file,
170         layout           => $layout,
171         node_shape       => $node_shape,
172         output_type      => $output_type,
173         add_color        => $add_color,
174         natural_join     => $natural_join,
175         natural_join_pk  => $join_pk_only,
176         skip_fields      => $skip_fields,
177         skip_tables      => $skip_tables,
178         skip_tables_like => $skip_tables_like,
179         show_datatypes   => $show_datatypes,
180         show_sizes       => $show_sizes,
181         show_constraints => $show_constraints,
182         cluster          => $cluster,
183         height           => $height || 0,
184         width            => $width  || 0,
185         fontsize         => $fontsize,
186         fontname         => $fontname,
187         nodeattrs        => \%nodeattrs,
188         edgeattrs        => \%edgeattrs,
189         graphattrs       => \%graphattrs,
190         show_fields      => $no_fields ? 0 : 1,
191     },
192 ) or die SQL::Translator->error;
193
194 for my $file (@files) {
195     my $output = $translator->translate( $file ) or die
196                  "Error: " . $translator->error;
197     if ( $out_file ) {
198         print "Image written to '$out_file'.  Done.\n";
199     }
200     else {
201         print $output;
202     }
203 }
204
205 # -------------------------------------------------------------------
206
207 =pod
208
209 =head1 AUTHOR
210
211 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
212
213 =head1 SEE ALSO
214
215 perl, SQL::Translator.
216
217 =cut