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