image is returned, not written w/o out_file (bug #71398)
[dbsrgits/SQL-Translator.git] / script / sqlt-diagram
CommitLineData
969049ba 1#!/usr/bin/env perl
dd9550a4 2
44659089 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
dd9550a4 21=head1 NAME
22
fd72ea41 23sqlt-diagram - Automatically create a diagram from a database schema
dd9550a4 24
25=head1 SYNOPSIS
26
fd72ea41 27 ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql
dd9550a4 28
29 Options:
30
2621b0fc 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
80e716f5 36 --font-size Font size ("small," "medium," "large," or "huge,"
2621b0fc 37 default "medium")
bb44ccb5 38 --gutter Gutter size between tables
2621b0fc 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
507a1bf8 46 --skip-tables Comma-separated list of table names to exclude
47 --skip-tables-like Comma-separated list of regexen to exclude tables
2621b0fc 48 --debug Print debugging information
dd9550a4 49
50=head1 DESCRIPTION
51
52This script will create a picture of your schema. Only the database
aa0a19b9 53driver argument (for SQL::Translator) is required. If no output file
54name is given, then image will be printed to STDOUT, so you should
55redirect the output into a file.
dd9550a4 56
14655604 57The default action is to assume the presence of foreign key
fd72ea41 58relationships defined via "REFERENCES" or "FOREIGN KEY" constraints on
14655604 59the tables. If you are parsing the schema of a file that does not
60have these, you will find the natural join options helpful. With
61natural joins, like-named fields will be considered foreign keys.
62This can prove too permissive, however, as you probably don't want a
63field called "name" to be considered a foreign key, so you could
64include it in the "skip" option, and all fields called "name" will be
65excluded from natural joins. A more efficient method, however, might
66be to simply deduce the foriegn keys from primary keys to other fields
2621b0fc 67named the same in other tables. Use the "natural-join-pk" option
daf4f623 68to achieve this.
14655604 69
dd9550a4 70=cut
71
72use strict;
969049ba 73use warnings;
2621b0fc 74use Data::Dumper;
dd9550a4 75use Getopt::Long;
dd9550a4 76use Pod::Usage;
77use SQL::Translator;
78
da06ac74 79use vars '$VERSION';
11ad2df9 80$VERSION = '1.59';
da06ac74 81
aa0a19b9 82#
83# Get arguments.
84#
14655604 85my (
507a1bf8 86 $out_file, $image_type, $db_driver, $title, $num_columns,
2621b0fc 87 $no_lines, $font_size, $add_color, $debug, $show_fk_only,
507a1bf8 88 $gutter, $natural_join, $join_pk_only, $skip_fields,
89 $skip_tables, $skip_tables_like, $help
14655604 90);
91
dd9550a4 92GetOptions(
507a1bf8 93 'd|db|f|from=s' => \$db_driver,
94 'o|output:s' => \$out_file,
95 'i|image:s' => \$image_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,
108 'debug' => \$debug,
109 'h|help' => \$help,
dd9550a4 110) or die pod2usage;
a9208a9a 111my @files = @ARGV; # the create script(s) for the original db
dd9550a4 112
a9208a9a 113pod2usage(1) if $help;
dd9550a4 114pod2usage( -message => "No db driver specified" ) unless $db_driver;
a9208a9a 115pod2usage( -message => 'No input file' ) unless @files;
116
117my $translator = SQL::Translator->new(
118 from => $db_driver,
119 to => 'Diagram',
120 debug => $debug || 0,
121 producer_args => {
507a1bf8 122 out_file => $out_file,
123 image_type => $image_type,
124 gutter => $gutter || 0,
125 title => $title,
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,
a9208a9a 136 },
137) or die SQL::Translator->error;
138
139for my $file (@files) {
140 my $output = $translator->translate( $file ) or die
141 "Error: " . $translator->error;
142 if ( $out_file ) {
143 print "Image written to '$out_file'. Done.\n";
14655604 144 }
145 else {
a9208a9a 146 print $output;
14655604 147 }
dd9550a4 148}
149
daf4f623 150# -------------------------------------------------------------------
151
dd9550a4 152=pod
153
154=head1 AUTHOR
155
969049ba 156Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
dd9550a4 157
158=cut