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