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