Allow more producers than just the two graphical.
[dbsrgits/SQL-Translator.git] / bin / sql_translator.cgi
1 #!/usr/bin/perl
2
3 # -------------------------------------------------------------------
4 # $Id: sql_translator.cgi,v 1.2 2003-08-04 20:55:45 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('parser'),
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         die "No schema provided!\n" unless $data;
71
72         my $producer    = $q->param('producer');
73         my $image_type  = $q->param('output_type') || 'png';
74         my $header_type = 
75             $producer =~ m/(GraphViz|Diagram)/ 
76             ? "image/$image_type"
77             : 'text/plain';
78
79         $t->data( $data );
80         $t->producer( $producer );
81         my $output = $t->translate or die $t->error;
82
83         print $q->header( -type => $header_type ), $output;
84     }
85     else {
86         show_form( $q );
87     }
88 };
89
90 if ( my $error = $@ ) {
91     print $q->header, $q->start_html('Error'),
92         $q->h1('Error'), $error, $q->end_html;
93 }
94
95 # -------------------------------------------------------------------
96 sub show_form {
97     my $q     = shift;
98     my $title = 'SQL::Translator';
99
100     print $q->header, 
101         $q->start_html( -title => $title ),
102         $q->h1( qq[<a href="http://sqlfairy.sourceforge.net">$title</a>] ),
103         $q->start_form(-enctype => 'multipart/form-data'),
104         $q->table( { -border => 1 },
105             $q->Tr( 
106                 $q->td( [
107                     'Paste your schema here:',
108                     $q->textarea( 
109                         -name    => 'schema', 
110                         -rows    => 10, 
111                         -columns => 60,
112                     ),
113                 ] ),
114             ),
115             $q->Tr( 
116                 $q->td( [
117                     'Or upload your schema file:',
118                     $q->filefield( -name => 'schema_file'),
119                 ] ),
120             ),
121             $q->Tr( 
122                 $q->td( [
123                     'Parser:',
124                     $q->radio_group(
125                         -name    => 'parser',
126                         -values  => [ 'MySQL', 'PostgreSQL', 'Oracle' ],
127                         -default => 'MySQL',
128                         -rows    => 3,
129                     ),
130                 ] ),
131             ),
132             $q->Tr( 
133                 $q->td( [
134                     'Producer:',
135                     $q->radio_group(
136                         -name    => 'producer',
137                         -values  => [ qw[ ClassDBI Diagram GraphViz HTML
138                             MySQL Oracle POD PostgreSQL SQLite Sybase XML
139                         ] ],
140                         -default => 'GraphViz',
141                         -rows    => 3,
142                     ),
143                 ] ),
144             ),
145             $q->Tr( 
146                 $q->td( [
147                     'Title:',
148                     $q->textfield('title'),
149                 ] ),
150             ),
151             $q->Tr( 
152                 $q->td( [
153                     'Output Type:',
154                     $q->radio_group(
155                         -name    => 'output_type',
156                         -values  => [ 'png', 'jpeg' ],
157                         -default => 'png',
158                         -rows    => 2,
159                     ),
160                 ] ),
161             ),
162             $q->Tr( 
163                 $q->td( [
164                     'Perform Natural Joins:',
165                     $q->radio_group(
166                         -name       => 'natural_join',
167                         -values     => [ 'no', 'yes', 'pk_only' ],
168                         -labels     => {
169                             no      => 'No',
170                             yes     => 'Yes, on all like-named fields',
171                             pk_only => 'Yes, but only from primary keys'
172                         },
173                         -default    => 'no',
174                         -rows       => 3,
175                     ),
176                 ] ),
177             ),
178             $q->Tr( 
179                 $q->td( [
180                     'Skip These Fields in Natural Joins:',
181                     $q->textarea(
182                         -name    => 'skip_fields',
183                         -rows    => 3,
184                         -columns => 60,
185                     ),
186                 ] ),
187             ),
188             $q->Tr( 
189                 $q->td( [
190                     'Color:',
191                     $q->radio_group(
192                         -name    => 'add_color',
193                         -values  => [ 1, 0 ],
194                         -labels  => { 
195                             1    => 'Yes', 
196                             0    => 'No' 
197                         },
198                         -default => 1,
199                         -rows    => 2,
200                     ),
201                 ] ),
202             ),
203             $q->Tr( 
204                 $q->td( [
205                     'Show Only Foreign Keys *:',
206                     $q->radio_group(
207                         -name    => 'show_fk_only',
208                         -values  => [ 1, 0 ],
209                         -default => 0,
210                         -labels  => {
211                             1    => 'Yes',
212                             0    => 'No',
213                         },
214                         -rows    => 2,
215                     ),
216                 ] ),
217             ),
218             $q->Tr( 
219                 $q->td( [
220                     'Font Size *:',
221                     $q->radio_group(
222                         -name    => 'font_size',
223                         -values  => [ qw( small medium large ) ],
224                         -default => 'medium',
225                         -rows    => 3,
226                     ),
227                 ] ),
228             ),
229             $q->Tr( 
230                 $q->td( [
231                     'Number of Columns *:',
232                     $q->textfield('no_columns'),
233                 ] ),
234             ),
235             $q->Tr( 
236                 $q->td( [
237                     'Layout **:',
238                     $q->radio_group(
239                         -name    => 'layout',
240                         -values  => [ qw( dot neato twopi ) ],
241                         -default => 'neato',
242                         -rows    => 3,
243                     ),
244                 ] ),
245             ),
246             $q->Tr( 
247                 $q->td( [
248                     'Node Shape **:',
249                     $q->radio_group(
250                         -name    => 'node_shape',
251                         -values  => [ qw( record plaintext ellipse 
252                             circle egg triangle box diamond trapezium 
253                             parallelogram house hexagon octagon 
254                         ) ],
255                         -default => 'record',
256                         -rows    => 13,
257                     ),
258                 ] ),
259             ),
260             $q->Tr( 
261                 $q->td( [
262                     'Show Field Names **:',
263                     $q->radio_group(
264                         -name    => 'show_fields',
265                         -values  => [ 1, 0 ],
266                         -default => 1,
267                         -labels  => {
268                             1    => 'Yes',
269                             0    => 'No',
270                         },
271                         -rows    => 2,
272                     ),
273                 ] ),
274             ),
275             $q->Tr( 
276                 $q->td( [
277                     'Height **:',
278                     $q->textfield( -name => 'height', -default => 11 ),
279                 ] ),
280             ),
281             $q->Tr( 
282                 $q->td( [
283                     'Width **:',
284                     $q->textfield( -name => 'width', -default => 8.5 ),
285                 ] ),
286             ),
287             $q->Tr( 
288                 $q->td(
289                     { -colspan => 2, -align => 'center' },
290                     $q->submit( 
291                         -name  => 'submit', 
292                         -value => 'Submit',
293                     ),
294                     $q->br,
295                     q[
296                         <small>
297                         * -- Applies to diagram only<br>
298                         ** -- Applies to graph only<br>
299                         </small>
300                     ],
301                 ),
302             ),
303         ),
304         $q->end_form,
305         $q->end_html;
306 }
307
308 =pod
309
310 =head1 AUTHOR
311
312 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
313
314 =cut