c19e2b302f52917711238c2711f0ae87c8a3907e
[dbsrgits/SQL-Translator.git] / script / sqlt-diagram
1 #!/usr/bin/env perl
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
5 #
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.
9 #
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.
14 #
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
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21 =head1 NAME 
22
23 sqlt-diagram - Automatically create a diagram from a database schema
24
25 =head1 SYNOPSIS
26
27   ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql
28
29   Options:
30
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,"
37                        default "medium")
38     --gutter           Gutter size between tables
39     --color            Add colors
40     --show-fk-only     Only show fields that act as primary 
41                        or foreign keys
42
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     --debug            Print debugging information
47
48 =head1 DESCRIPTION
49
50 This script will create a picture of your schema.  Only the database
51 driver argument (for SQL::Translator) is required.  If no output file
52 name is given, then image will be printed to STDOUT, so you should
53 redirect the output into a file.
54
55 The default action is to assume the presence of foreign key
56 relationships defined via "REFERENCES" or "FOREIGN KEY" constraints on
57 the tables.  If you are parsing the schema of a file that does not
58 have these, you will find the natural join options helpful.  With
59 natural joins, like-named fields will be considered foreign keys.
60 This can prove too permissive, however, as you probably don't want a
61 field called "name" to be considered a foreign key, so you could
62 include it in the "skip" option, and all fields called "name" will be
63 excluded from natural joins.  A more efficient method, however, might
64 be to simply deduce the foriegn keys from primary keys to other fields
65 named the same in other tables.  Use the "natural-join-pk" option
66 to achieve this.
67
68 =cut
69
70 use strict;
71 use warnings;
72 use Data::Dumper;
73 use Getopt::Long;
74 use Pod::Usage;
75 use SQL::Translator;
76
77 use vars '$VERSION';
78 $VERSION = '1.60';
79
80 #
81 # Get arguments.
82 #
83 my ( 
84     $out_file, $image_type, $db_driver, $title, $no_columns, 
85     $no_lines, $font_size, $add_color, $debug, $show_fk_only,
86     $gutter, $natural_join, $join_pk_only, $skip_fields, $help
87 );
88
89 GetOptions(
90     'd|db|f|from=s'   => \$db_driver,
91     'o|output:s'      => \$out_file,
92     'i|image:s'       => \$image_type,
93     't|title:s'       => \$title,
94     'c|columns:i'     => \$no_columns,
95     'n|no-lines'      => \$no_lines,
96     'font-size:s'     => \$font_size,
97     'gutter:i'        => \$gutter,
98     'color'           => \$add_color,
99     'show-fk-only'    => \$show_fk_only,
100     'natural-join'    => \$natural_join,
101     'natural-join-pk' => \$join_pk_only,
102     's|skip:s'        => \$skip_fields,
103     'debug'           => \$debug,
104     'h|help'          => \$help,
105 ) or die pod2usage;
106 my @files = @ARGV; # the create script(s) for the original db
107
108 pod2usage(1) if $help;
109 pod2usage( -message => "No db driver specified" ) unless $db_driver;
110 pod2usage( -message => 'No input file'          ) unless @files;
111
112 my $translator       =  SQL::Translator->new( 
113     from             => $db_driver,
114     to               => 'Diagram',
115     debug            => $debug || 0,
116     producer_args    => {
117         out_file     => $out_file,
118         image_type   => $image_type,
119         gutter       => $gutter || 0,
120         title        => $title,
121         no_columns   => $no_columns,
122         no_lines     => $no_lines,
123         font_size    => $font_size,
124         add_color    => $add_color,
125         show_fk_only => $show_fk_only,
126         natural_join => $natural_join,
127         join_pk_only => $join_pk_only,
128         skip_fields  => $skip_fields,
129     },
130 ) or die SQL::Translator->error;
131
132 for my $file (@files) {
133     my $output = $translator->translate( $file ) or die
134                  "Error: " . $translator->error;
135     if ( $out_file ) {
136         print "Image written to '$out_file'.  Done.\n";
137     }
138     else {
139         print $output;
140     }
141 }
142
143 # -------------------------------------------------------------------
144
145 =pod
146
147 =head1 AUTHOR
148
149 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
150
151 =cut