3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2011 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 sqlt-diagram - Automatically create a diagram from a database schema
27 ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql
31 -o|--output Output file name (default STDOUT)
32 -i|--image Output image type ("png" or "jpeg," default "png")
33 -t|--title Title to give schema
34 -c|--cols Number of columns
35 -n|--no-lines Don't draw lines
36 --font-size Font size ("small," "medium," "large," or "huge,"
38 --gutter Gutter size between tables
40 --show-fk-only Only show fields that act as primary
43 --natural-join Perform natural joins
44 --natural-join-pk Perform natural joins from primary keys only
45 -s|--skip Fields to skip in natural joins
46 --skip-tables Comma-separated list of table names to exclude
47 --skip-tables-like Comma-separated list of regexen to exclude tables
48 --debug Print debugging information
52 This script will create a picture of your schema. Only the database
53 driver argument (for SQL::Translator) is required. If no output file
54 name is given, then image will be printed to STDOUT, so you should
55 redirect the output into a file.
57 The default action is to assume the presence of foreign key
58 relationships defined via "REFERENCES" or "FOREIGN KEY" constraints on
59 the tables. If you are parsing the schema of a file that does not
60 have these, you will find the natural join options helpful. With
61 natural joins, like-named fields will be considered foreign keys.
62 This can prove too permissive, however, as you probably don't want a
63 field called "name" to be considered a foreign key, so you could
64 include it in the "skip" option, and all fields called "name" will be
65 excluded from natural joins. A more efficient method, however, might
66 be to simply deduce the foriegn keys from primary keys to other fields
67 named the same in other tables. Use the "natural-join-pk" option
86 $out_file, $output_type, $db_driver, $title, $num_columns,
87 $no_lines, $font_size, $add_color, $debug, $show_fk_only,
88 $gutter, $natural_join, $join_pk_only, $skip_fields,
89 $skip_tables, $skip_tables_like, $help
93 'd|db|f|from=s' => \$db_driver,
94 'o|output:s' => \$out_file,
95 'i|image:s' => \$output_type,
96 't|title:s' => \$title,
97 'c|columns:i' => \$num_columns,
98 'n|no-lines' => \$no_lines,
99 'font-size:s' => \$font_size,
100 'gutter:i' => \$gutter,
101 'color' => \$add_color,
102 'show-fk-only' => \$show_fk_only,
103 'natural-join' => \$natural_join,
104 'natural-join-pk' => \$join_pk_only,
105 's|skip:s' => \$skip_fields,
106 'skip-tables:s' => \$skip_tables,
107 'skip-tables-like:s' => \$skip_tables_like,
111 my @files = @ARGV; # the create script(s) for the original db
113 pod2usage(1) if $help;
114 pod2usage( -message => "No db driver specified" ) unless $db_driver;
115 pod2usage( -message => 'No input file' ) unless @files;
117 my $translator = SQL::Translator->new(
120 debug => $debug || 0,
122 out_file => $out_file,
123 output_type => $output_type,
124 gutter => $gutter || 0,
126 num_columns => $num_columns,
127 no_lines => $no_lines,
128 font_size => $font_size,
129 add_color => $add_color,
130 show_fk_only => $show_fk_only,
131 natural_join => $natural_join,
132 join_pk_only => $join_pk_only,
133 skip_fields => $skip_fields,
134 skip_tables => $skip_tables,
135 skip_tables_like => $skip_tables_like,
137 ) or die SQL::Translator->error;
139 binmode STDOUT unless $out_file;
141 for my $file (@files) {
142 my $output = $translator->translate( $file ) or die
143 "Error: " . $translator->error;
145 print "Image written to '$out_file'. Done.\n";
152 # -------------------------------------------------------------------
158 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.