Just the fairy.
[dbsrgits/SQL-Translator.git] / bin / sqlt-graph
CommitLineData
354b1807 1#!/usr/bin/perl
2
3# -------------------------------------------------------------------
7b908fb7 4# $Id: sqlt-graph,v 1.3 2004-02-11 21:32:25 kycl4rk Exp $
354b1807 5# -------------------------------------------------------------------
cb9f6535 6# Copyright (C) 2002-4 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)
50 --natural-join Perform natural joins
51 --natural-join-pk Perform natural joins from primary keys only
7b908fb7 52 --show-datatypes Show datatype of each field
53 --show-sizes Show field sizes for VARCHAR and CHAR fields
54 --show-constraints Show list of constraints for each field
354b1807 55 -s|--skip Fields to skip in natural joins
56 --debug Print debugging information
57
58=head1 DESCRIPTION
59
60This script will create a graph of your schema. Only the database
61driver argument (for SQL::Translator) is required. If no output file
62name is given, then image will be printed to STDOUT, so you should
63redirect the output into a file.
64
65The default action is to assume the presence of foreign key
66relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
67the tables. If you are parsing the schema of a file that does not
68have these, you will find the natural join options helpful. With
69natural joins, like-named fields will be considered foreign keys.
70This can prove too permissive, however, as you probably don't want a
71field called "name" to be considered a foreign key, so you could
72include it in the "skip" option, and all fields called "name" will be
73excluded from natural joins. A more efficient method, however, might
74be to simply deduce the foriegn keys from primary keys to other fields
75named the same in other tables. Use the "natural-join-pk" option
76to acheive this.
77
78If the schema defines foreign keys, then the graph produced will be
79directed showing the direction of the relationship. If the foreign
80keys are intuited via natural joins, the graph will be undirected.
81
82=cut
83
84# -------------------------------------------------------------------
85
86use strict;
87use Data::Dumper;
88use Getopt::Long;
89use GraphViz;
90use Pod::Usage;
91use SQL::Translator;
92
93use vars '$VERSION';
7b908fb7 94$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
354b1807 95
96#
97# Get arguments.
98#
99my (
100 $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
101 $natural_join, $join_pk_only, $skip_fields, $debug, $help, $height,
7b908fb7 102 $width, $no_fields, $show_datatypes, $show_sizes, $show_constraints
354b1807 103);
104
105GetOptions(
106 'd|db|f|from=s' => \$db_driver,
107 'o|output:s' => \$out_file,
108 'l|layout:s' => \$layout,
109 'n|node-shape:s' => \$node_shape,
110 't|output-type:s' => \$output_type,
7b908fb7 111 'height:f' => \$height,
112 'width:f' => \$width,
354b1807 113 'c|color' => \$add_color,
114 'no-fields' => \$no_fields,
115 'natural-join' => \$natural_join,
116 'natural-join-pk' => \$join_pk_only,
117 's|skip:s' => \$skip_fields,
7b908fb7 118 'show-datatypes' => \$show_datatypes,
119 'show-sizes' => \$show_sizes,
120 'show-constraints' => \$show_constraints,
354b1807 121 'debug' => \$debug,
122 'h|help' => \$help,
123) or die pod2usage;
124my @files = @ARGV; # the create script(s) for the original db
125
126pod2usage(1) if $help;
127pod2usage( -message => "No db driver specified" ) unless $db_driver;
128pod2usage( -message => 'No input file' ) unless @files;
129
7b908fb7 130my $translator = SQL::Translator->new(
131 from => $db_driver,
132 to => 'GraphViz',
133 debug => $debug || 0,
134 producer_args => {
135 out_file => $out_file,
136 layout => $layout,
137 node_shape => $node_shape,
138 output_type => $output_type,
139 add_color => $add_color,
140 natural_join => $natural_join,
141 natural_join_pk => $join_pk_only,
142 skip_fields => $skip_fields,
143 show_datatypes => $show_datatypes,
144 show_sizes => $show_sizes,
145 show_constraints => $show_constraints,
146 height => $height || 0,
147 width => $width || 0,
148 show_fields => $no_fields ? 0 : 1,
354b1807 149 },
150) or die SQL::Translator->error;
151
152for my $file (@files) {
153 my $output = $translator->translate( $file ) or die
154 "Error: " . $translator->error;
155 if ( $out_file ) {
156 print "Image written to '$out_file'. Done.\n";
157 }
158 else {
159 print $output;
160 }
161}
162
163# -------------------------------------------------------------------
164
165=pod
166
167=head1 AUTHOR
168
169Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
170
171=head1 SEE ALSO
172
173perl, SQL::Translator.
174
175=cut