59b092581fc0727aed3cf9c6caba5c183070d558
[dbsrgits/SQL-Translator.git] / script / sqlt-diagram
1 #!/usr/bin/env perl
2
3 =head1 NAME 
4
5 sqlt-diagram - Automatically create a diagram from a database schema
6
7 =head1 SYNOPSIS
8
9   ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql
10
11   Options:
12
13     -o|--output        Output file name (default STDOUT)
14     -i|--image         Output image type ("png" or "jpeg," default "png")
15     -t|--title         Title to give schema
16     -c|--cols          Number of columns
17     -n|--no-lines      Don't draw lines
18     --font-size        Font size ("small," "medium," "large," or "huge,"
19                        default "medium")
20     --gutter           Gutter size between tables
21     --color            Add colors
22     --show-fk-only     Only show fields that act as primary 
23                        or foreign keys
24
25     --natural-join     Perform natural joins
26     --natural-join-pk  Perform natural joins from primary keys only
27     -s|--skip          Fields to skip in natural joins
28     --skip-tables      Comma-separated list of table names to exclude 
29     --skip-tables-like Comma-separated list of regexen to exclude tables
30     --debug            Print debugging information
31
32 =head1 DESCRIPTION
33
34 This script will create a picture 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.
38
39 The default action is to assume the presence of foreign key
40 relationships defined via "REFERENCES" 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
50 to achieve this.
51
52 =cut
53
54 use strict;
55 use warnings;
56 use Data::Dumper;
57 use Getopt::Long;
58 use Pod::Usage;
59 use SQL::Translator;
60
61 use vars '$VERSION';
62 $VERSION = '1.59';
63
64 #
65 # Get arguments.
66 #
67 my ( 
68     $out_file, $image_type, $db_driver, $title, $num_columns, 
69     $no_lines, $font_size, $add_color, $debug, $show_fk_only,
70     $gutter, $natural_join, $join_pk_only, $skip_fields, 
71     $skip_tables, $skip_tables_like, $help
72 );
73
74 GetOptions(
75     'd|db|f|from=s'      => \$db_driver,
76     'o|output:s'         => \$out_file,
77     'i|image:s'          => \$image_type,
78     't|title:s'          => \$title,
79     'c|columns:i'        => \$num_columns,
80     'n|no-lines'         => \$no_lines,
81     'font-size:s'        => \$font_size,
82     'gutter:i'           => \$gutter,
83     'color'              => \$add_color,
84     'show-fk-only'       => \$show_fk_only,
85     'natural-join'       => \$natural_join,
86     'natural-join-pk'    => \$join_pk_only,
87     's|skip:s'           => \$skip_fields,
88     'skip-tables:s'      => \$skip_tables,
89     'skip-tables-like:s' => \$skip_tables_like,
90     'debug'              => \$debug,
91     'h|help'             => \$help,
92 ) or die pod2usage;
93 my @files = @ARGV; # the create script(s) for the original db
94
95 pod2usage(1) if $help;
96 pod2usage( -message => "No db driver specified" ) unless $db_driver;
97 pod2usage( -message => 'No input file'          ) unless @files;
98
99 my $translator       =  SQL::Translator->new( 
100     from             => $db_driver,
101     to               => 'Diagram',
102     debug            => $debug || 0,
103     producer_args    => {
104         out_file         => $out_file,
105         image_type       => $image_type,
106         gutter           => $gutter || 0,
107         title            => $title,
108         num_columns      => $num_columns,
109         no_lines         => $no_lines,
110         font_size        => $font_size,
111         add_color        => $add_color,
112         show_fk_only     => $show_fk_only,
113         natural_join     => $natural_join,
114         join_pk_only     => $join_pk_only,
115         skip_fields      => $skip_fields,
116         skip_tables      => $skip_tables,
117         skip_tables_like => $skip_tables_like,
118     },
119 ) or die SQL::Translator->error;
120
121 for my $file (@files) {
122     my $output = $translator->translate( $file ) or die
123                  "Error: " . $translator->error;
124     if ( $out_file ) {
125         print "Image written to '$out_file'.  Done.\n";
126     }
127     else {
128         print $output;
129     }
130 }
131
132 # -------------------------------------------------------------------
133
134 =pod
135
136 =head1 AUTHOR
137
138 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
139
140 =cut