Fixing docs.
[dbsrgits/SQL-Translator.git] / bin / sqlt-graph.pl
CommitLineData
036a7d3b 1#!/usr/bin/perl
2
9cbbf4df 3# $Id: sqlt-graph.pl,v 1.3 2003-08-21 02:48:04 kycl4rk Exp $
036a7d3b 4
5=head1 NAME
6
ef3b7e2e 7sqlt-graph.pl - Automatically create a graph from a database schema
036a7d3b 8
9=head1 SYNOPSIS
10
ef3b7e2e 11 ./sqlt-graph.pl -d|--db=db_parser [options] schema.sql
036a7d3b 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,"
9cbbf4df 20 "hexagon," "octagon," default "record")
036a7d3b 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")
03526ec7 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)
036a7d3b 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
39This script will create a graph of your schema. Only the database
40driver argument (for SQL::Translator) is required. If no output file
41name is given, then image will be printed to STDOUT, so you should
42redirect the output into a file.
43
44The default action is to assume the presence of foreign key
45relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
46the tables. If you are parsing the schema of a file that does not
47have these, you will find the natural join options helpful. With
48natural joins, like-named fields will be considered foreign keys.
49This can prove too permissive, however, as you probably don't want a
50field called "name" to be considered a foreign key, so you could
51include it in the "skip" option, and all fields called "name" will be
52excluded from natural joins. A more efficient method, however, might
53be to simply deduce the foriegn keys from primary keys to other fields
54named the same in other tables. Use the "natural-join-pk" option
55to acheive this.
56
57If the schema defines foreign keys, then the graph produced will be
58directed showing the direction of the relationship. If the foreign
59keys are intuited via natural joins, the graph will be undirected.
60
61=cut
62
63use strict;
64use Data::Dumper;
65use Getopt::Long;
66use GraphViz;
67use Pod::Usage;
68use SQL::Translator;
69
9cbbf4df 70my $VERSION = (qw$Revision: 1.3 $)[-1];
036a7d3b 71
72#
73# Get arguments.
74#
75my (
76 $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
03526ec7 77 $natural_join, $join_pk_only, $skip_fields, $debug, $help, $height,
78 $width, $no_fields
036a7d3b 79);
80
81GetOptions(
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,
03526ec7 87 'height:i' => \$height,
88 'width:i' => \$width,
89 'c|color' => \$add_color,
90 'no-fields' => \$no_fields,
036a7d3b 91 'natural-join' => \$natural_join,
92 'natural-join-pk' => \$join_pk_only,
93 's|skip:s' => \$skip_fields,
94 'debug' => \$debug,
945a44d2 95 'h|help' => \$help,
036a7d3b 96) or die pod2usage;
945a44d2 97my @files = @ARGV; # the create script(s) for the original db
036a7d3b 98
945a44d2 99pod2usage(1) if $help;
036a7d3b 100pod2usage( -message => "No db driver specified" ) unless $db_driver;
945a44d2 101pod2usage( -message => 'No input file' ) unless @files;
102
103my $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,
03526ec7 116 height => $height || 0,
117 width => $width || 0,
118 show_fields => $no_fields ? 0 : 1,
036a7d3b 119 },
945a44d2 120) or die SQL::Translator->error;
036a7d3b 121
945a44d2 122for 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";
036a7d3b 127 }
945a44d2 128 else {
129 print $output;
036a7d3b 130 }
131}
132
036a7d3b 133=pod
134
135=head1 AUTHOR
136
137Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
138
139=cut