Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / bin / sqlt-diagram
CommitLineData
dd9550a4 1#!/usr/bin/perl
2
daf4f623 3# -------------------------------------------------------------------
782b5a43 4# $Id$
daf4f623 5# -------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
daf4f623 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# -------------------------------------------------------------------
dd9550a4 22
23=head1 NAME
24
fd72ea41 25sqlt-diagram - Automatically create a diagram from a database schema
dd9550a4 26
27=head1 SYNOPSIS
28
fd72ea41 29 ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql
dd9550a4 30
31 Options:
32
2621b0fc 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
80e716f5 38 --font-size Font size ("small," "medium," "large," or "huge,"
2621b0fc 39 default "medium")
bb44ccb5 40 --gutter Gutter size between tables
2621b0fc 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
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;
2621b0fc 73use Data::Dumper;
dd9550a4 74use Getopt::Long;
dd9550a4 75use Pod::Usage;
76use SQL::Translator;
77
da06ac74 78use vars '$VERSION';
79$VERSION = '1.99';
80
aa0a19b9 81#
82# Get arguments.
83#
14655604 84my (
85 $out_file, $image_type, $db_driver, $title, $no_columns,
2621b0fc 86 $no_lines, $font_size, $add_color, $debug, $show_fk_only,
bb44ccb5 87 $gutter, $natural_join, $join_pk_only, $skip_fields, $help
14655604 88);
89
dd9550a4 90GetOptions(
fd72ea41 91 'd|db|f|from=s' => \$db_driver,
2621b0fc 92 'o|output:s' => \$out_file,
93 'i|image:s' => \$image_type,
94 't|title:s' => \$title,
95 'c|columns:i' => \$no_columns,
96 'n|no-lines' => \$no_lines,
e0343999 97 'font-size:s' => \$font_size,
bb44ccb5 98 'gutter:i' => \$gutter,
2621b0fc 99 'color' => \$add_color,
100 'show-fk-only' => \$show_fk_only,
101 'natural-join' => \$natural_join,
102 'natural-join-pk' => \$join_pk_only,
103 's|skip:s' => \$skip_fields,
104 'debug' => \$debug,
a9208a9a 105 'h|help' => \$help,
dd9550a4 106) or die pod2usage;
a9208a9a 107my @files = @ARGV; # the create script(s) for the original db
dd9550a4 108
a9208a9a 109pod2usage(1) if $help;
dd9550a4 110pod2usage( -message => "No db driver specified" ) unless $db_driver;
a9208a9a 111pod2usage( -message => 'No input file' ) unless @files;
112
113my $translator = SQL::Translator->new(
114 from => $db_driver,
115 to => 'Diagram',
116 debug => $debug || 0,
117 producer_args => {
118 out_file => $out_file,
119 image_type => $image_type,
bb44ccb5 120 gutter => $gutter || 0,
a9208a9a 121 title => $title,
122 no_columns => $no_columns,
123 no_lines => $no_lines,
124 font_size => $font_size,
125 add_color => $add_color,
126 show_fk_only => $show_fk_only,
127 natural_join => $natural_join,
128 join_pk_only => $join_pk_only,
129 skip_fields => $skip_fields,
130 },
131) or die SQL::Translator->error;
132
133for my $file (@files) {
134 my $output = $translator->translate( $file ) or die
135 "Error: " . $translator->error;
136 if ( $out_file ) {
137 print "Image written to '$out_file'. Done.\n";
14655604 138 }
139 else {
a9208a9a 140 print $output;
14655604 141 }
dd9550a4 142}
143
daf4f623 144# -------------------------------------------------------------------
145
dd9550a4 146=pod
147
148=head1 AUTHOR
149
daf4f623 150Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
dd9550a4 151
152=cut