Adding new CGI script front-end for GraphViz and Diagram producers.
[dbsrgits/SQL-Translator.git] / bin / auto-viv.cgi
1 #!/usr/bin/perl
2
3 # -------------------------------------------------------------------
4 # $Id: auto-viv.cgi,v 1.1 2003-04-24 19:58:39 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             },
57         ) or die SQL::Translator->error;
58
59         my $data;
60         if ( $q->param('schema') ) {
61             $data = $q->param('schema');
62         }
63         elsif ( my $fh = $q->upload('schema_file') ) {
64             local $/;
65             $data = <$fh>;
66         }
67
68         die "No schema provided!\n" unless $data;
69         $t->data( $data );
70         $t->producer( $q->param('do_graph') ? 'GraphViz' : 'Diagram' );
71         my $output = $t->translate or die $t->error;
72
73         my $image_type = $q->param('output_type') || 'png';
74         print $q->header( -type => "image/$image_type" ), $output;
75     }
76     else {
77         show_form( $q );
78     }
79 };
80
81 if ( my $error = $@ ) {
82     print $q->header, $q->start_html('Error'),
83         $q->h1('Error'), $error, $q->end_html;
84 }
85
86 # -------------------------------------------------------------------
87 sub show_form {
88     my $q     = shift;
89     my $title = 'SQL::Translator';
90
91     print $q->header, 
92         $q->start_html( -title => $title ),
93         $q->h1( qq[<a href="http://sqlfairy.sourceforge.net">$title</a>] ),
94         $q->start_form(-enctype => 'multipart/form-data'),
95         $q->table( { -border => 1 },
96             $q->Tr( 
97                 $q->td( [
98                     'Paste your schema here:',
99                     $q->textarea( 
100                         -name    => 'schema', 
101                         -rows    => 10, 
102                         -columns => 60,
103                     ),
104                 ] ),
105             ),
106             $q->Tr( 
107                 $q->td( [
108                     'Or upload your schema file:',
109                     $q->filefield( -name => 'schema_file'),
110                 ] ),
111             ),
112             $q->Tr( 
113                 $q->td( [
114                     'Database:',
115                     $q->radio_group(
116                         -name    => 'database',
117                         -values  => [ 'MySQL', 'PostgreSQL', 'Oracle' ],
118                         -default => 'MySQL',
119                         -rows    => 3,
120                     ),
121                 ] ),
122             ),
123             $q->Tr( 
124                 $q->td( [
125                     'Title:',
126                     $q->textfield('title'),
127                 ] ),
128             ),
129             $q->Tr( 
130                 $q->td( [
131                     'Output Type:',
132                     $q->radio_group(
133                         -name    => 'output_type',
134                         -values  => [ 'png', 'jpeg' ],
135                         -default => 'png',
136                         -rows    => 2,
137                     ),
138                 ] ),
139             ),
140             $q->Tr( 
141                 $q->td( [
142                     'Perform Natural Joins:',
143                     $q->radio_group(
144                         -name       => 'natural_join',
145                         -values     => [ 'no', 'yes', 'pk_only' ],
146                         -labels     => {
147                             no      => 'No',
148                             yes     => 'Yes, on all like-named fields',
149                             pk_only => 'Yes, but only from primary keys'
150                         },
151                         -default    => 'no',
152                         -rows       => 3,
153                     ),
154                 ] ),
155             ),
156             $q->Tr( 
157                 $q->td( [
158                     'Skip These Fields in Natural Joins:',
159                     $q->textarea(
160                         -name    => 'skip_fields',
161                         -rows    => 3,
162                         -columns => 60,
163                     ),
164                 ] ),
165             ),
166             $q->Tr( 
167                 $q->td( [
168                     'Color:',
169                     $q->radio_group(
170                         -name    => 'add_color',
171                         -values  => [ 1, 0 ],
172                         -labels  => { 
173                             1    => 'Yes', 
174                             0    => 'No' 
175                         },
176                         -default => 1,
177                         -rows    => 2,
178                     ),
179                 ] ),
180             ),
181             $q->Tr( 
182                 $q->td( [
183                     'Show Only Foreign Keys *:',
184                     $q->radio_group(
185                         -name    => 'show_fk_only',
186                         -values  => [ 1, 0 ],
187                         -default => 0,
188                         -labels  => {
189                             1    => 'Yes',
190                             0    => 'No',
191                         },
192                         -rows    => 2,
193                     ),
194                 ] ),
195             ),
196             $q->Tr( 
197                 $q->td( [
198                     'Font Size *:',
199                     $q->radio_group(
200                         -name    => 'font_size',
201                         -values  => [ qw( small medium large ) ],
202                         -default => 'medium',
203                         -rows    => 3,
204                     ),
205                 ] ),
206             ),
207             $q->Tr( 
208                 $q->td( [
209                     'Number of Columns *:',
210                     $q->textfield('no_columns'),
211                 ] ),
212             ),
213             $q->Tr( 
214                 $q->td( [
215                     'Layout **:',
216                     $q->radio_group(
217                         -name    => 'layout',
218                         -values  => [ qw( dot neato twopi ) ],
219                         -default => 'neato',
220                         -rows    => 3,
221                     ),
222                 ] ),
223             ),
224             $q->Tr( 
225                 $q->td( [
226                     'Node Shape **:',
227                     $q->radio_group(
228                         -name    => 'node_shape',
229                         -values  => [ qw( record plaintext ellipse 
230                             circle egg triangle box diamond trapezium 
231                             parallelogram house hexagon octagon 
232                         ) ],
233                         -default => 'ellipse',
234                         -rows    => 13,
235                     ),
236                 ] ),
237             ),
238             $q->Tr( 
239                 $q->td(
240                     { -colspan => 2, -align => 'center' },
241                     $q->submit( 
242                         -name  => 'do_diagram', 
243                         -value => 'Create ER Diagram' 
244                     ),
245                     $q->submit( 
246                         -name  => 'do_graph', 
247                         -value => 'Create Graph' 
248                     ),
249                     $q->br,
250                     q[
251                         <small>
252                         * -- Applies to diagram only<br>
253                         ** -- Applies to graph only<br>
254                         </small>
255                     ],
256                 ),
257             ),
258         ),
259         $q->end_form,
260         $q->end_html;
261 }
262
263 =pod
264
265 =head1 AUTHOR
266
267 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
268
269 =cut