Add sqlite roundtrip test (probably need to do the same for the rest of the parser...
[dbsrgits/SQL-Translator.git] / bin / sqlt-diagram
1 #!/usr/bin/perl
2
3 # -------------------------------------------------------------------
4 # $Id$
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =head1 NAME 
24
25 sqlt-diagram - Automatically create a diagram from a database schema
26
27 =head1 SYNOPSIS
28
29   ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql
30
31   Options:
32
33     -o|--output        Output file name (default STDOUT)
34     -i|--image         Output image type ("png" or "jpeg," default "png")
35     -t|--title         Title to give schema
36     -c|--cols          Number of columns
37     -n|--no-lines      Don't draw lines
38     --font-size        Font size ("small," "medium," "large," or "huge,"
39                        default "medium")
40     --gutter           Gutter size between tables
41     --color            Add colors
42     --show-fk-only     Only show fields that act as primary 
43                        or foreign keys
44
45     --natural-join     Perform natural joins
46     --natural-join-pk  Perform natural joins from primary keys only
47     -s|--skip          Fields to skip in natural joins
48     --debug            Print debugging information
49
50 =head1 DESCRIPTION
51
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.
56
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
68 to achieve this.
69
70 =cut
71
72 use strict;
73 use Data::Dumper;
74 use Getopt::Long;
75 use Pod::Usage;
76 use SQL::Translator;
77
78 #
79 # Get arguments.
80 #
81 my ( 
82     $out_file, $image_type, $db_driver, $title, $no_columns, 
83     $no_lines, $font_size, $add_color, $debug, $show_fk_only,
84     $gutter, $natural_join, $join_pk_only, $skip_fields, $help
85 );
86
87 GetOptions(
88     'd|db|f|from=s'   => \$db_driver,
89     'o|output:s'      => \$out_file,
90     'i|image:s'       => \$image_type,
91     't|title:s'       => \$title,
92     'c|columns:i'     => \$no_columns,
93     'n|no-lines'      => \$no_lines,
94     'font-size:s'   => \$font_size,
95     'gutter:i'        => \$gutter,
96     'color'           => \$add_color,
97     'show-fk-only'    => \$show_fk_only,
98     'natural-join'    => \$natural_join,
99     'natural-join-pk' => \$join_pk_only,
100     's|skip:s'        => \$skip_fields,
101     'debug'           => \$debug,
102     'h|help'          => \$help,
103 ) or die pod2usage;
104 my @files = @ARGV; # the create script(s) for the original db
105
106 pod2usage(1) if $help;
107 pod2usage( -message => "No db driver specified" ) unless $db_driver;
108 pod2usage( -message => 'No input file'          ) unless @files;
109
110 my $translator       =  SQL::Translator->new( 
111     from             => $db_driver,
112     to               => 'Diagram',
113     debug            => $debug || 0,
114     producer_args    => {
115         out_file     => $out_file,
116         image_type   => $image_type,
117         gutter       => $gutter || 0,
118         title        => $title,
119         no_columns   => $no_columns,
120         no_lines     => $no_lines,
121         font_size    => $font_size,
122         add_color    => $add_color,
123         show_fk_only => $show_fk_only,
124         natural_join => $natural_join,
125         join_pk_only => $join_pk_only,
126         skip_fields  => $skip_fields,
127     },
128 ) or die SQL::Translator->error;
129
130 for my $file (@files) {
131     my $output = $translator->translate( $file ) or die
132                  "Error: " . $translator->error;
133     if ( $out_file ) {
134         print "Image written to '$out_file'.  Done.\n";
135     }
136     else {
137         print $output;
138     }
139 }
140
141 # -------------------------------------------------------------------
142
143 =pod
144
145 =head1 AUTHOR
146
147 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
148
149 =cut