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