Commit | Line | Data |
dd9550a4 |
1 | #!/usr/bin/perl |
2 | |
fd72ea41 |
3 | # $Id: sqlt-diagram,v 1.2 2003-08-26 03:55:33 kycl4rk Exp $ |
dd9550a4 |
4 | |
5 | =head1 NAME |
6 | |
fd72ea41 |
7 | sqlt-diagram - Automatically create a diagram from a database schema |
dd9550a4 |
8 | |
9 | =head1 SYNOPSIS |
10 | |
fd72ea41 |
11 | ./sqlt-diagram -d|-f|--from|--db=db_parser [options] schema.sql |
dd9550a4 |
12 | |
13 | Options: |
14 | |
2621b0fc |
15 | -o|--output Output file name (default STDOUT) |
16 | -i|--image Output image type ("png" or "jpeg," default "png") |
17 | -t|--title Title to give schema |
18 | -c|--cols Number of columns |
19 | -n|--no-lines Don't draw lines |
20 | -f|--font-size Font size ("small," "medium," "large," or "huge," |
21 | default "medium") |
22 | --color Add colors |
23 | --show-fk-only Only show fields that act as primary |
24 | or foreign keys |
25 | |
26 | --natural-join Perform natural joins |
27 | --natural-join-pk Perform natural joins from primary keys only |
28 | -s|--skip Fields to skip in natural joins |
29 | --debug Print debugging information |
dd9550a4 |
30 | |
31 | =head1 DESCRIPTION |
32 | |
33 | This script will create a picture of your schema. Only the database |
aa0a19b9 |
34 | driver argument (for SQL::Translator) is required. If no output file |
35 | name is given, then image will be printed to STDOUT, so you should |
36 | redirect the output into a file. |
dd9550a4 |
37 | |
14655604 |
38 | The default action is to assume the presence of foreign key |
fd72ea41 |
39 | relationships defined via "REFERENCES" or "FOREIGN KEY" constraints on |
14655604 |
40 | the tables. If you are parsing the schema of a file that does not |
41 | have these, you will find the natural join options helpful. With |
42 | natural joins, like-named fields will be considered foreign keys. |
43 | This can prove too permissive, however, as you probably don't want a |
44 | field called "name" to be considered a foreign key, so you could |
45 | include it in the "skip" option, and all fields called "name" will be |
46 | excluded from natural joins. A more efficient method, however, might |
47 | be to simply deduce the foriegn keys from primary keys to other fields |
2621b0fc |
48 | named the same in other tables. Use the "natural-join-pk" option |
14655604 |
49 | to acheive this. |
50 | |
dd9550a4 |
51 | =cut |
52 | |
53 | use strict; |
2621b0fc |
54 | use Data::Dumper; |
dd9550a4 |
55 | use Getopt::Long; |
dd9550a4 |
56 | use Pod::Usage; |
57 | use SQL::Translator; |
58 | |
fd72ea41 |
59 | use vars '$VERSION'; |
60 | $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/; |
2621b0fc |
61 | |
aa0a19b9 |
62 | # |
63 | # Get arguments. |
64 | # |
14655604 |
65 | my ( |
66 | $out_file, $image_type, $db_driver, $title, $no_columns, |
2621b0fc |
67 | $no_lines, $font_size, $add_color, $debug, $show_fk_only, |
a9208a9a |
68 | $natural_join, $join_pk_only, $skip_fields, $help |
14655604 |
69 | ); |
70 | |
dd9550a4 |
71 | GetOptions( |
fd72ea41 |
72 | 'd|db|f|from=s' => \$db_driver, |
2621b0fc |
73 | 'o|output:s' => \$out_file, |
74 | 'i|image:s' => \$image_type, |
75 | 't|title:s' => \$title, |
76 | 'c|columns:i' => \$no_columns, |
77 | 'n|no-lines' => \$no_lines, |
78 | 'f|font-size:s' => \$font_size, |
79 | 'color' => \$add_color, |
80 | 'show-fk-only' => \$show_fk_only, |
81 | 'natural-join' => \$natural_join, |
82 | 'natural-join-pk' => \$join_pk_only, |
83 | 's|skip:s' => \$skip_fields, |
84 | 'debug' => \$debug, |
a9208a9a |
85 | 'h|help' => \$help, |
dd9550a4 |
86 | ) or die pod2usage; |
a9208a9a |
87 | my @files = @ARGV; # the create script(s) for the original db |
dd9550a4 |
88 | |
a9208a9a |
89 | pod2usage(1) if $help; |
dd9550a4 |
90 | pod2usage( -message => "No db driver specified" ) unless $db_driver; |
a9208a9a |
91 | pod2usage( -message => 'No input file' ) unless @files; |
92 | |
93 | my $translator = SQL::Translator->new( |
94 | from => $db_driver, |
95 | to => 'Diagram', |
96 | debug => $debug || 0, |
97 | producer_args => { |
98 | out_file => $out_file, |
99 | image_type => $image_type, |
100 | title => $title, |
101 | no_columns => $no_columns, |
102 | no_lines => $no_lines, |
103 | font_size => $font_size, |
104 | add_color => $add_color, |
105 | show_fk_only => $show_fk_only, |
106 | natural_join => $natural_join, |
107 | join_pk_only => $join_pk_only, |
108 | skip_fields => $skip_fields, |
109 | }, |
110 | ) or die SQL::Translator->error; |
111 | |
112 | for my $file (@files) { |
113 | my $output = $translator->translate( $file ) or die |
114 | "Error: " . $translator->error; |
115 | if ( $out_file ) { |
116 | print "Image written to '$out_file'. Done.\n"; |
14655604 |
117 | } |
118 | else { |
a9208a9a |
119 | print $output; |
14655604 |
120 | } |
dd9550a4 |
121 | } |
122 | |
123 | =pod |
124 | |
125 | =head1 AUTHOR |
126 | |
127 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |
128 | |
129 | =cut |