3 # $Id: auto-graph.pl,v 1.2 2003-04-24 16:14:07 kycl4rk Exp $
7 auto-graph.pl - Automatically create a graph from a database schema
11 ./auto-graph.pl -d|--db=db_parser [options] schema.sql
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")
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
34 This script will create a graph of your schema. Only the database
35 driver argument (for SQL::Translator) is required. If no output file
36 name is given, then image will be printed to STDOUT, so you should
37 redirect the output into a file.
39 The default action is to assume the presence of foreign key
40 relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
41 the tables. If you are parsing the schema of a file that does not
42 have these, you will find the natural join options helpful. With
43 natural joins, like-named fields will be considered foreign keys.
44 This can prove too permissive, however, as you probably don't want a
45 field called "name" to be considered a foreign key, so you could
46 include it in the "skip" option, and all fields called "name" will be
47 excluded from natural joins. A more efficient method, however, might
48 be to simply deduce the foriegn keys from primary keys to other fields
49 named the same in other tables. Use the "natural-join-pk" option
52 If the schema defines foreign keys, then the graph produced will be
53 directed showing the direction of the relationship. If the foreign
54 keys are intuited via natural joins, the graph will be undirected.
65 my $VERSION = (qw$Revision: 1.2 $)[-1];
71 $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
72 $natural_join, $join_pk_only, $skip_fields, $debug, $help
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,
88 my @files = @ARGV; # the create script(s) for the original db
90 pod2usage(1) if $help;
91 pod2usage( -message => "No db driver specified" ) unless $db_driver;
92 pod2usage( -message => 'No input file' ) unless @files;
94 my $translator = SQL::Translator->new(
99 out_file => $out_file,
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,
108 ) or die SQL::Translator->error;
110 for my $file (@files) {
111 my $output = $translator->translate( $file ) or die
112 "Error: " . $translator->error;
114 print "Image written to '$out_file'. Done.\n";
125 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>