Commit | Line | Data |
dd9550a4 |
1 | #!/usr/bin/perl |
2 | |
daf4f623 |
3 | # ------------------------------------------------------------------- |
4 | # $Id: sqlt-diagram,v 1.3 2004-02-06 17:48:16 kycl4rk Exp $ |
5 | # ------------------------------------------------------------------- |
6 | # Copyright (C) 2002-4 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 | # ------------------------------------------------------------------- |
dd9550a4 |
22 | |
23 | =head1 NAME |
24 | |
fd72ea41 |
25 | sqlt-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 |
38 | -f|--font-size Font size ("small," "medium," "large," or "huge," |
39 | default "medium") |
40 | --color Add colors |
41 | --show-fk-only Only show fields that act as primary |
42 | or foreign keys |
43 | |
44 | --natural-join Perform natural joins |
45 | --natural-join-pk Perform natural joins from primary keys only |
46 | -s|--skip Fields to skip in natural joins |
47 | --debug Print debugging information |
dd9550a4 |
48 | |
49 | =head1 DESCRIPTION |
50 | |
51 | This script will create a picture of your schema. Only the database |
aa0a19b9 |
52 | driver argument (for SQL::Translator) is required. If no output file |
53 | name is given, then image will be printed to STDOUT, so you should |
54 | redirect the output into a file. |
dd9550a4 |
55 | |
14655604 |
56 | The default action is to assume the presence of foreign key |
fd72ea41 |
57 | relationships defined via "REFERENCES" or "FOREIGN KEY" constraints on |
14655604 |
58 | the tables. If you are parsing the schema of a file that does not |
59 | have these, you will find the natural join options helpful. With |
60 | natural joins, like-named fields will be considered foreign keys. |
61 | This can prove too permissive, however, as you probably don't want a |
62 | field called "name" to be considered a foreign key, so you could |
63 | include it in the "skip" option, and all fields called "name" will be |
64 | excluded from natural joins. A more efficient method, however, might |
65 | be to simply deduce the foriegn keys from primary keys to other fields |
2621b0fc |
66 | named the same in other tables. Use the "natural-join-pk" option |
daf4f623 |
67 | to achieve this. |
14655604 |
68 | |
dd9550a4 |
69 | =cut |
70 | |
71 | use strict; |
2621b0fc |
72 | use Data::Dumper; |
dd9550a4 |
73 | use Getopt::Long; |
dd9550a4 |
74 | use Pod::Usage; |
75 | use SQL::Translator; |
76 | |
fd72ea41 |
77 | use vars '$VERSION'; |
daf4f623 |
78 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; |
2621b0fc |
79 | |
aa0a19b9 |
80 | # |
81 | # Get arguments. |
82 | # |
14655604 |
83 | my ( |
84 | $out_file, $image_type, $db_driver, $title, $no_columns, |
2621b0fc |
85 | $no_lines, $font_size, $add_color, $debug, $show_fk_only, |
a9208a9a |
86 | $natural_join, $join_pk_only, $skip_fields, $help |
14655604 |
87 | ); |
88 | |
dd9550a4 |
89 | GetOptions( |
fd72ea41 |
90 | 'd|db|f|from=s' => \$db_driver, |
2621b0fc |
91 | 'o|output:s' => \$out_file, |
92 | 'i|image:s' => \$image_type, |
93 | 't|title:s' => \$title, |
94 | 'c|columns:i' => \$no_columns, |
95 | 'n|no-lines' => \$no_lines, |
96 | 'f|font-size:s' => \$font_size, |
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 |
105 | my @files = @ARGV; # the create script(s) for the original db |
dd9550a4 |
106 | |
a9208a9a |
107 | pod2usage(1) if $help; |
dd9550a4 |
108 | pod2usage( -message => "No db driver specified" ) unless $db_driver; |
a9208a9a |
109 | pod2usage( -message => 'No input file' ) unless @files; |
110 | |
111 | my $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, |
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"; |
14655604 |
135 | } |
136 | else { |
a9208a9a |
137 | print $output; |
14655604 |
138 | } |
dd9550a4 |
139 | } |
140 | |
daf4f623 |
141 | # ------------------------------------------------------------------- |
142 | |
dd9550a4 |
143 | =pod |
144 | |
145 | =head1 AUTHOR |
146 | |
daf4f623 |
147 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
dd9550a4 |
148 | |
149 | =cut |