Adding new ER diagramming producer.
[dbsrgits/SQL-Translator.git] / bin / auto-graph.pl
CommitLineData
036a7d3b 1#!/usr/bin/perl
2
945a44d2 3# $Id: auto-graph.pl,v 1.2 2003-04-24 16:14:07 kycl4rk Exp $
036a7d3b 4
5=head1 NAME
6
7auto-graph.pl - Automatically create a graph from a database schema
8
9=head1 SYNOPSIS
10
11 ./auto-graph.pl -d|--db=db_parser [options] schema.sql
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,"
20 "hexagon," "octagon," default "ellipse")
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")
26 --color Add colors
27 --natural-join Perform natural joins
28 --natural-join-pk Perform natural joins from primary keys only
29 -s|--skip Fields to skip in natural joins
30 --debug Print debugging information
31
32=head1 DESCRIPTION
33
34This script will create a graph of your schema. Only the database
35driver argument (for SQL::Translator) is required. If no output file
36name is given, then image will be printed to STDOUT, so you should
37redirect the output into a file.
38
39The default action is to assume the presence of foreign key
40relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
41the tables. If you are parsing the schema of a file that does not
42have these, you will find the natural join options helpful. With
43natural joins, like-named fields will be considered foreign keys.
44This can prove too permissive, however, as you probably don't want a
45field called "name" to be considered a foreign key, so you could
46include it in the "skip" option, and all fields called "name" will be
47excluded from natural joins. A more efficient method, however, might
48be to simply deduce the foriegn keys from primary keys to other fields
49named the same in other tables. Use the "natural-join-pk" option
50to acheive this.
51
52If the schema defines foreign keys, then the graph produced will be
53directed showing the direction of the relationship. If the foreign
54keys are intuited via natural joins, the graph will be undirected.
55
56=cut
57
58use strict;
59use Data::Dumper;
60use Getopt::Long;
61use GraphViz;
62use Pod::Usage;
63use SQL::Translator;
64
945a44d2 65my $VERSION = (qw$Revision: 1.2 $)[-1];
036a7d3b 66
67#
68# Get arguments.
69#
70my (
71 $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
945a44d2 72 $natural_join, $join_pk_only, $skip_fields, $debug, $help
036a7d3b 73);
74
75GetOptions(
76 'd|db=s' => \$db_driver,
77 'o|output:s' => \$out_file,
78 'l|layout:s' => \$layout,
79 'n|node-shape:s' => \$node_shape,
80 't|output-type:s' => \$output_type,
81 'color' => \$add_color,
82 'natural-join' => \$natural_join,
83 'natural-join-pk' => \$join_pk_only,
84 's|skip:s' => \$skip_fields,
85 'debug' => \$debug,
945a44d2 86 'h|help' => \$help,
036a7d3b 87) or die pod2usage;
945a44d2 88my @files = @ARGV; # the create script(s) for the original db
036a7d3b 89
945a44d2 90pod2usage(1) if $help;
036a7d3b 91pod2usage( -message => "No db driver specified" ) unless $db_driver;
945a44d2 92pod2usage( -message => 'No input file' ) unless @files;
93
94my $translator = SQL::Translator->new(
95 from => $db_driver,
96 to => 'GraphViz',
97 debug => $debug || 0,
98 producer_args => {
99 out_file => $out_file,
100 layout => $layout,
101 node_shape => $node_shape,
102 output_type => $output_type,
103 add_color => $add_color,
104 natural_join => $natural_join,
105 natural_join_pk => $join_pk_only,
106 skip_fields => $skip_fields,
036a7d3b 107 },
945a44d2 108) or die SQL::Translator->error;
036a7d3b 109
945a44d2 110for my $file (@files) {
111 my $output = $translator->translate( $file ) or die
112 "Error: " . $translator->error;
113 if ( $out_file ) {
114 print "Image written to '$out_file'. Done.\n";
036a7d3b 115 }
945a44d2 116 else {
117 print $output;
036a7d3b 118 }
119}
120
036a7d3b 121=pod
122
123=head1 AUTHOR
124
125Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
126
127=cut