Added ability to manipulate height, width, and whether to show the field
[dbsrgits/SQL-Translator.git] / bin / auto-viv.cgi
1 #!/usr/bin/perl
2
3 # -------------------------------------------------------------------
4 # $Id: auto-viv.cgi,v 1.2 2003-06-05 01:43:35 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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 # -------------------------------------------------------------------
22
23 =head1 NAME
24
25 auto-viv.cgi
26
27 =head1 DESCRIPTION
28
29 A CGI script for transforming SQL schemas into pictures, either GraphViz
30 graphs or ER diagrams.  Basically, a simple web-form front-end for the
31 myriad options available to "auto-dia.pl" and "auto-graph.pl."
32
33 =cut
34
35 use strict;
36 use CGI;
37 use SQL::Translator;
38
39 my $q = CGI->new;
40
41 eval {
42     if ( $q->param ) {
43         my $t                =  SQL::Translator->new( 
44             from             => $q->param('database'),
45             producer_args    => {
46                 image_type   => $q->param('output_type') || 'png',
47                 title        => $q->param('title')       || 'Schema',
48                 natural_join => $q->param('natural_join') eq 'no' ? 0 : 1, 
49                 join_pk_only => $q->param('natural_join') eq 'pk_only' ? 1 : 0,
50                 add_color    => $q->param('add_color'),
51                 skip_fields  => $q->param('skip_fields'),
52                 show_fk_only => $q->param('show_fk_only'),
53                 font_size    => $q->param('font_size'),
54                 no_columns   => $q->param('no_columns'),
55                 node_shape   => $q->param('node_shape'),
56                 height       => $q->param('height')      || 0,
57                 width        => $q->param('width')       || 0,
58                 show_fields  => $q->param('show_fields') || 0,
59             },
60         ) or die SQL::Translator->error;
61
62         my $data;
63         if ( $q->param('schema') ) {
64             $data = $q->param('schema');
65         }
66         elsif ( my $fh = $q->upload('schema_file') ) {
67             local $/;
68             $data = <$fh>;
69         }
70
71         die "No schema provided!\n" unless $data;
72         $t->data( $data );
73         $t->producer( $q->param('do_graph') ? 'GraphViz' : 'Diagram' );
74         my $output = $t->translate or die $t->error;
75
76         my $image_type = $q->param('output_type') || 'png';
77         print $q->header( -type => "image/$image_type" ), $output;
78     }
79     else {
80         show_form( $q );
81     }
82 };
83
84 if ( my $error = $@ ) {
85     print $q->header, $q->start_html('Error'),
86         $q->h1('Error'), $error, $q->end_html;
87 }
88
89 # -------------------------------------------------------------------
90 sub show_form {
91     my $q     = shift;
92     my $title = 'SQL::Translator';
93
94     print $q->header, 
95         $q->start_html( -title => $title ),
96         $q->h1( qq[<a href="http://sqlfairy.sourceforge.net">$title</a>] ),
97         $q->start_form(-enctype => 'multipart/form-data'),
98         $q->table( { -border => 1 },
99             $q->Tr( 
100                 $q->td( [
101                     'Paste your schema here:',
102                     $q->textarea( 
103                         -name    => 'schema', 
104                         -rows    => 10, 
105                         -columns => 60,
106                     ),
107                 ] ),
108             ),
109             $q->Tr( 
110                 $q->td( [
111                     'Or upload your schema file:',
112                     $q->filefield( -name => 'schema_file'),
113                 ] ),
114             ),
115             $q->Tr( 
116                 $q->td( [
117                     'Database:',
118                     $q->radio_group(
119                         -name    => 'database',
120                         -values  => [ 'MySQL', 'PostgreSQL', 'Oracle' ],
121                         -default => 'MySQL',
122                         -rows    => 3,
123                     ),
124                 ] ),
125             ),
126             $q->Tr( 
127                 $q->td( [
128                     'Title:',
129                     $q->textfield('title'),
130                 ] ),
131             ),
132             $q->Tr( 
133                 $q->td( [
134                     'Output Type:',
135                     $q->radio_group(
136                         -name    => 'output_type',
137                         -values  => [ 'png', 'jpeg' ],
138                         -default => 'png',
139                         -rows    => 2,
140                     ),
141                 ] ),
142             ),
143             $q->Tr( 
144                 $q->td( [
145                     'Perform Natural Joins:',
146                     $q->radio_group(
147                         -name       => 'natural_join',
148                         -values     => [ 'no', 'yes', 'pk_only' ],
149                         -labels     => {
150                             no      => 'No',
151                             yes     => 'Yes, on all like-named fields',
152                             pk_only => 'Yes, but only from primary keys'
153                         },
154                         -default    => 'no',
155                         -rows       => 3,
156                     ),
157                 ] ),
158             ),
159             $q->Tr( 
160                 $q->td( [
161                     'Skip These Fields in Natural Joins:',
162                     $q->textarea(
163                         -name    => 'skip_fields',
164                         -rows    => 3,
165                         -columns => 60,
166                     ),
167                 ] ),
168             ),
169             $q->Tr( 
170                 $q->td( [
171                     'Color:',
172                     $q->radio_group(
173                         -name    => 'add_color',
174                         -values  => [ 1, 0 ],
175                         -labels  => { 
176                             1    => 'Yes', 
177                             0    => 'No' 
178                         },
179                         -default => 1,
180                         -rows    => 2,
181                     ),
182                 ] ),
183             ),
184             $q->Tr( 
185                 $q->td( [
186                     'Show Only Foreign Keys *:',
187                     $q->radio_group(
188                         -name    => 'show_fk_only',
189                         -values  => [ 1, 0 ],
190                         -default => 0,
191                         -labels  => {
192                             1    => 'Yes',
193                             0    => 'No',
194                         },
195                         -rows    => 2,
196                     ),
197                 ] ),
198             ),
199             $q->Tr( 
200                 $q->td( [
201                     'Font Size *:',
202                     $q->radio_group(
203                         -name    => 'font_size',
204                         -values  => [ qw( small medium large ) ],
205                         -default => 'medium',
206                         -rows    => 3,
207                     ),
208                 ] ),
209             ),
210             $q->Tr( 
211                 $q->td( [
212                     'Number of Columns *:',
213                     $q->textfield('no_columns'),
214                 ] ),
215             ),
216             $q->Tr( 
217                 $q->td( [
218                     'Layout **:',
219                     $q->radio_group(
220                         -name    => 'layout',
221                         -values  => [ qw( dot neato twopi ) ],
222                         -default => 'neato',
223                         -rows    => 3,
224                     ),
225                 ] ),
226             ),
227             $q->Tr( 
228                 $q->td( [
229                     'Node Shape **:',
230                     $q->radio_group(
231                         -name    => 'node_shape',
232                         -values  => [ qw( record plaintext ellipse 
233                             circle egg triangle box diamond trapezium 
234                             parallelogram house hexagon octagon 
235                         ) ],
236                         -default => 'record',
237                         -rows    => 13,
238                     ),
239                 ] ),
240             ),
241             $q->Tr( 
242                 $q->td( [
243                     'Show Field Names **:',
244                     $q->radio_group(
245                         -name    => 'show_fields',
246                         -values  => [ 1, 0 ],
247                         -default => 1,
248                         -labels  => {
249                             1    => 'Yes',
250                             0    => 'No',
251                         },
252                         -rows    => 2,
253                     ),
254                 ] ),
255             ),
256             $q->Tr( 
257                 $q->td( [
258                     'Height **:',
259                     $q->textfield( -name => 'height', -default => 11 ),
260                 ] ),
261             ),
262             $q->Tr( 
263                 $q->td( [
264                     'Width **:',
265                     $q->textfield( -name => 'width', -default => 8.5 ),
266                 ] ),
267             ),
268             $q->Tr( 
269                 $q->td(
270                     { -colspan => 2, -align => 'center' },
271                     $q->submit( 
272                         -name  => 'do_diagram', 
273                         -value => 'Create ER Diagram' 
274                     ),
275                     $q->submit( 
276                         -name  => 'do_graph', 
277                         -value => 'Create Graph' 
278                     ),
279                     $q->br,
280                     q[
281                         <small>
282                         * -- Applies to diagram only<br>
283                         ** -- Applies to graph only<br>
284                         </small>
285                     ],
286                 ),
287             ),
288         ),
289         $q->end_form,
290         $q->end_html;
291 }
292
293 =pod
294
295 =head1 AUTHOR
296
297 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
298
299 =cut