3 # $Id: sqlt-graph.pl,v 1.2 2003-06-16 18:25:59 kycl4rk Exp $
7 sqlt-graph.pl - Automatically create a graph from a database schema
11 ./sqlt-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 --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)
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
39 This script will create a graph of your schema. Only the database
40 driver argument (for SQL::Translator) is required. If no output file
41 name is given, then image will be printed to STDOUT, so you should
42 redirect the output into a file.
44 The default action is to assume the presence of foreign key
45 relationships defined via "REFERNCES" or "FOREIGN KEY" constraints on
46 the tables. If you are parsing the schema of a file that does not
47 have these, you will find the natural join options helpful. With
48 natural joins, like-named fields will be considered foreign keys.
49 This can prove too permissive, however, as you probably don't want a
50 field called "name" to be considered a foreign key, so you could
51 include it in the "skip" option, and all fields called "name" will be
52 excluded from natural joins. A more efficient method, however, might
53 be to simply deduce the foriegn keys from primary keys to other fields
54 named the same in other tables. Use the "natural-join-pk" option
57 If the schema defines foreign keys, then the graph produced will be
58 directed showing the direction of the relationship. If the foreign
59 keys are intuited via natural joins, the graph will be undirected.
70 my $VERSION = (qw$Revision: 1.2 $)[-1];
76 $layout, $node_shape, $out_file, $output_type, $db_driver, $add_color,
77 $natural_join, $join_pk_only, $skip_fields, $debug, $help, $height,
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,
87 'height:i' => \$height,
89 'c|color' => \$add_color,
90 'no-fields' => \$no_fields,
91 'natural-join' => \$natural_join,
92 'natural-join-pk' => \$join_pk_only,
93 's|skip:s' => \$skip_fields,
97 my @files = @ARGV; # the create script(s) for the original db
99 pod2usage(1) if $help;
100 pod2usage( -message => "No db driver specified" ) unless $db_driver;
101 pod2usage( -message => 'No input file' ) unless @files;
103 my $translator = SQL::Translator->new(
106 debug => $debug || 0,
108 out_file => $out_file,
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,
116 height => $height || 0,
117 width => $width || 0,
118 show_fields => $no_fields ? 0 : 1,
120 ) or die SQL::Translator->error;
122 for my $file (@files) {
123 my $output = $translator->translate( $file ) or die
124 "Error: " . $translator->error;
126 print "Image written to '$out_file'. Done.\n";
137 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>