Adding option for skipping tables.
[dbsrgits/SQL-Translator.git] / bin / sqlt-graph
CommitLineData
354b1807 1#!/usr/bin/perl
2
3# -------------------------------------------------------------------
478f608d 4# Copyright (C) 2002-2009 SQLFairy Authors
354b1807 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., 59 Temple Place, Suite 330, Boston, MA
18# 02111-1307 USA
19# -------------------------------------------------------------------
20
21=head1 NAME
22
23sqlt-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 --no-fields Don't show field names
44 --height Image height (in inches, default "11",
45 set to "0" to undefine)
46 --width Image width (in inches, default "8.5",
47 set to "0" to undefine)
027cebc7 48 --fontsize custom font size for node and edge labels
49 --fontname name of custom font (or full path to font file) for
50 node, edge, and graph labels
51 --nodeattr attribute name and value (in key=val syntax) for
52 nodes; this option may be repeated to specify
53 multiple node attributes
54 --edgeattr same as --nodeattr, but for edge attributes
55 --graphattr same as --nodeattr, but for graph attributes
354b1807 56 --natural-join Perform natural joins
57 --natural-join-pk Perform natural joins from primary keys only
88c73648 58 --show-datatypes Show datatype of each field
59 --show-sizes Show column sizes for VARCHAR and CHAR fields
7b908fb7 60 --show-constraints Show list of constraints for each field
354b1807 61 -s|--skip Fields to skip in natural joins
fddd7578 62 --skip-tables Comma-separated list of regexen to exclude tables
354b1807 63 --debug Print debugging information
64
65=head1 DESCRIPTION
66
67This script will create a graph of your schema. Only the database
68driver argument (for SQL::Translator) is required. If no output file
69name is given, then image will be printed to STDOUT, so you should
70redirect the output into a file.
71
72The default action is to assume the presence of foreign key
73relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
74the tables. If you are parsing the schema of a file that does not
75have these, you will find the natural join options helpful. With
76natural joins, like-named fields will be considered foreign keys.
77This can prove too permissive, however, as you probably don't want a
78field called "name" to be considered a foreign key, so you could
79include it in the "skip" option, and all fields called "name" will be
80excluded from natural joins. A more efficient method, however, might
81be to simply deduce the foriegn keys from primary keys to other fields
82named the same in other tables. Use the "natural-join-pk" option
83to acheive this.
84
85If the schema defines foreign keys, then the graph produced will be
86directed showing the direction of the relationship. If the foreign
87keys are intuited via natural joins, the graph will be undirected.
88
89=cut
90
91# -------------------------------------------------------------------
92
93use strict;
94use Data::Dumper;
95use Getopt::Long;
96use GraphViz;
97use Pod::Usage;
98use SQL::Translator;
99
da06ac74 100use vars '$VERSION';
4ab3763d 101$VERSION = '1.59';
da06ac74 102
354b1807 103#
104# Get arguments.
105#
106my (
107 $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
d491c962 108 $natural_join, $join_pk_only, $skip_fields, $show_datatypes,
f382d57f 109 $show_sizes, $show_constraints, $debug, $help, $height, $width,
fddd7578 110 $no_fields, $fontsize, $fontname, $skip_tables
354b1807 111);
112
027cebc7 113# multi-valued options:
114my %edgeattrs = ();
115my %nodeattrs = ();
116my %graphattrs = ();
117
354b1807 118GetOptions(
119 'd|db|f|from=s' => \$db_driver,
120 'o|output:s' => \$out_file,
121 'l|layout:s' => \$layout,
122 'n|node-shape:s' => \$node_shape,
123 't|output-type:s' => \$output_type,
7b908fb7 124 'height:f' => \$height,
125 'width:f' => \$width,
027cebc7 126 'fontsize=i' => \$fontsize,
127 'fontname=s' => \$fontname,
128 'nodeattr=s' => \%nodeattrs,
129 'edgeattr=s' => \%edgeattrs,
130 'graphattr=s' => \%graphattrs,
354b1807 131 'c|color' => \$add_color,
132 'no-fields' => \$no_fields,
133 'natural-join' => \$natural_join,
134 'natural-join-pk' => \$join_pk_only,
135 's|skip:s' => \$skip_fields,
fddd7578 136 'skip-tables:s' => \$skip_tables,
e0a284cd 137 'show-datatypes' => \$show_datatypes,
138 'show-sizes' => \$show_sizes,
139 'show-constraints' => \$show_constraints,
354b1807 140 'debug' => \$debug,
141 'h|help' => \$help,
142) or die pod2usage;
143my @files = @ARGV; # the create script(s) for the original db
144
145pod2usage(1) if $help;
146pod2usage( -message => "No db driver specified" ) unless $db_driver;
147pod2usage( -message => 'No input file' ) unless @files;
148
7b908fb7 149my $translator = SQL::Translator->new(
150 from => $db_driver,
151 to => 'GraphViz',
152 debug => $debug || 0,
153 producer_args => {
154 out_file => $out_file,
155 layout => $layout,
156 node_shape => $node_shape,
157 output_type => $output_type,
158 add_color => $add_color,
159 natural_join => $natural_join,
160 natural_join_pk => $join_pk_only,
161 skip_fields => $skip_fields,
fddd7578 162 skip_tables => $skip_tables,
7b908fb7 163 show_datatypes => $show_datatypes,
164 show_sizes => $show_sizes,
165 show_constraints => $show_constraints,
166 height => $height || 0,
167 width => $width || 0,
027cebc7 168 fontsize => $fontsize,
169 fontname => $fontname,
170 nodeattrs => \%nodeattrs,
171 edgeattrs => \%edgeattrs,
172 graphattrs => \%graphattrs,
7b908fb7 173 show_fields => $no_fields ? 0 : 1,
354b1807 174 },
175) or die SQL::Translator->error;
176
177for my $file (@files) {
178 my $output = $translator->translate( $file ) or die
179 "Error: " . $translator->error;
180 if ( $out_file ) {
181 print "Image written to '$out_file'. Done.\n";
182 }
183 else {
184 print $output;
185 }
186}
187
188# -------------------------------------------------------------------
189
190=pod
191
192=head1 AUTHOR
193
194Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
195
196=head1 SEE ALSO
197
198perl, SQL::Translator.
199
200=cut