- Added some stuff to MANIFEST.SKIP
[dbsrgits/SQL-Translator.git] / bin / sqlt.cgi
1 #!/usr/bin/perl
2
3 # -------------------------------------------------------------------
4 # $Id$
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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 sqlt.cgi - CGI front-end for SQL::Translator
26
27 =head1 DESCRIPTION
28
29 Place this script in your "cgi-bin" directory and point your browser
30 to it.  This script is meant to be a simple graphical interface to 
31 all the parsers and producers of SQL::Translator.
32
33 =cut
34
35 # -------------------------------------------------------------------
36
37 use strict;
38 use CGI;
39 use SQL::Translator;
40
41 use vars '$VERSION';
42 $VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;
43
44 my $q = CGI->new;
45
46 eval {
47     if ( $q->param ) {
48         my $data;
49         if ( $q->param('schema') ) {
50             $data = $q->param('schema');
51         }
52         elsif ( my $fh = $q->upload('schema_file') ) {
53             local $/;
54             $data = <$fh>;
55         }
56         die "No schema provided!\n" unless $data;
57
58         my $producer    = $q->param('producer');
59         my $output_type = $producer eq 'Diagram'
60             ? $q->param('diagram_output_type')
61             : $producer eq 'GraphViz'
62             ? $q->param('graphviz_output_type') 
63             : ''
64         ;
65
66         my $t                    =  SQL::Translator->new( 
67             from                 => $q->param('parser'),
68             producer_args        => {
69                 add_drop_table   => $q->param('add_drop_table'),
70                 output_type      => $output_type,
71                 title            => $q->param('title')       || 'Schema',
72                 natural_join     => $q->param('natural_join') eq 'no' ? 0 : 1, 
73                 join_pk_only     => $q->param('natural_join') eq 'pk_only' 
74                                     ? 1 : 0,
75                 add_color        => $q->param('add_color'),
76                 skip_fields      => $q->param('skip_fields'),
77                 show_fk_only     => $q->param('show_fk_only'),
78                 font_size        => $q->param('font_size'),
79                 no_columns       => $q->param('no_columns'),
80                 node_shape       => $q->param('node_shape'),
81                 layout           => $q->param('layout')      || '',
82                 height           => $q->param('height')      || 0,
83                 width            => $q->param('width')       || 0,
84                 show_fields      => $q->param('show_fields') || 0,
85                 ttfile           => $q->upload('template'),
86                 validate         => $q->param('validate'),
87                 emit_empty_tags  => $q->param('emit_empty_tags'),
88                 attrib_values    => $q->param('attrib_values'),
89                 no_comments      => !$q->param('comments'),
90             },
91             parser_args => {
92                 trim_fields      => $q->param('trim_fields'),
93                 scan_fields      => $q->param('scan_fields'),
94                 field_separator  => $q->param('fs'),
95                 record_separator => $q->param('rs'),
96             },
97         ) or die SQL::Translator->error;
98
99         my $image_type = '';
100         my $text_type  = 'plain';
101         if ( $output_type =~ /(gif|png|jpeg)/ ) {
102             $image_type = $output_type;
103         }
104         elsif ( $output_type eq 'svg' ) {
105             $image_type = 'svg+xml';
106         }
107         elsif ( $output_type =~ /gd/ ) {
108             $image_type = 'png';
109         }
110         elsif ( $output_type eq 'ps' ) {
111             $text_type = 'postscript';
112         }
113         elsif ( $producer eq 'HTML' ) {
114             $text_type = 'html';
115         }
116
117         my $header_type = $image_type ? "image/$image_type" : "text/$text_type";
118
119         $t->data( $data );
120         $t->producer( $producer );
121         my $output = $t->translate or die $t->error;
122
123         print $q->header( -type => $header_type ), $output;
124     }
125     else {
126         show_form( $q );
127     }
128 };
129
130 if ( my $error = $@ ) {
131     print $q->header, $q->start_html('Error'),
132         $q->h1('Error'), $error, $q->end_html;
133 }
134
135 # -------------------------------------------------------------------
136 sub show_form {
137     my $q     = shift;
138     my $title = 'SQL::Translator';
139
140     print $q->header, 
141         $q->start_html( -title => $title ),
142         $q->h1( qq[<a href="http://sqlfairy.sourceforge.net">$title</a>] ),
143         $q->start_form(-enctype => 'multipart/form-data'),
144         $q->table( { -border => 1 },
145             $q->Tr( 
146                 $q->td( [
147                     'Upload your schema file:',
148                     $q->filefield( -name => 'schema_file'),
149                 ] ),
150             ),
151             $q->Tr( 
152                 $q->td( [
153                     'Or paste your schema here:',
154                     $q->textarea( 
155                         -name    => 'schema', 
156                         -rows    => 5, 
157                         -columns => 60,
158                     ),
159                 ] ),
160             ),
161             $q->Tr( 
162                 $q->td( [
163                     'Parser:',
164                     $q->radio_group(
165                         -name    => 'parser',
166                         -values  => [ qw( MySQL PostgreSQL Oracle 
167                             Sybase Excel XML-SQLFairy xSV  
168                         ) ],
169                         -default => 'MySQL',
170                         -rows    => 3,
171                     ),
172                 ] ),
173             ),
174             $q->Tr( 
175                 $q->td( [
176                     'Producer:',
177                     $q->radio_group(
178                         -name    => 'producer',
179                         -values  => [ qw[ ClassDBI Diagram GraphViz HTML
180                             MySQL Oracle POD PostgreSQL SQLite Sybase
181                             TTSchema XML-SQLFairy
182                         ] ],
183                         -default => 'GraphViz',
184                         -rows    => 3,
185                     ),
186                 ] ),
187             ),
188             $q->Tr( 
189                 $q->td(
190                     { -colspan => 2, -align => 'center' },
191                     $q->submit( 
192                         -name  => 'submit', 
193                         -value => 'Submit',
194                     )
195                 ),
196             ),
197             $q->Tr( 
198                 $q->th( 
199                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
200                     'General Options:' 
201                 ),
202             ),
203             $q->Tr( 
204                 $q->td( [
205                     'Validate Schema:',
206                     $q->radio_group(
207                         -name    => 'validate',
208                         -values  => [ 1, 0 ],
209                         -labels  => { 
210                             1    => 'Yes', 
211                             0    => 'No' 
212                         },
213                         -default => 0,
214                         -rows    => 2,
215                     ),
216                 ] ),
217             ),
218             $q->Tr( 
219                 $q->th( 
220                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
221                     'DB Producer Options:' 
222                 ),
223             ),
224             $q->Tr( 
225                 $q->td( [
226                     'Add &quot;DROP TABLE&quot; statements:',
227                     $q->radio_group(
228                         -name    => 'add_drop_table',
229                         -values  => [ 1, 0 ],
230                         -labels  => { 
231                             1    => 'Yes', 
232                             0    => 'No' 
233                         },
234                         -default => 0,
235                         -rows    => 2,
236                     ),
237                 ] ),
238             ),
239             $q->Tr( 
240                 $q->td( [
241                     'Include comments:',
242                     $q->radio_group(
243                         -name    => 'comments',
244                         -values  => [ 1, 0 ],
245                         -labels  => { 
246                             1    => 'Yes', 
247                             0    => 'No' 
248                         },
249                         -default => 1,
250                         -rows    => 2,
251                     ),
252                 ] ),
253             ),
254             $q->Tr( 
255                 $q->th( 
256                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
257                     'HTML/POD/Diagram Producer Options:' 
258                 ),
259             ),
260             $q->Tr( 
261                 $q->td( [
262                     'Title:',
263                     $q->textfield('title'),
264                 ] ),
265             ),
266             $q->Tr( 
267                 $q->th( 
268                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
269                     'TTSchema Producer Options:' 
270                 ),
271             ),
272             $q->Tr( 
273                 $q->td( [
274                     'Template:',
275                     $q->filefield( -name => 'template'),
276                 ] ),
277             ),
278             $q->Tr( 
279                 $q->th( 
280                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
281                     'Graphical Producer Options'
282                 ),
283             ),
284             $q->Tr( 
285                 $q->td( [
286                     'Perform Natural Joins:',
287                     $q->radio_group(
288                         -name       => 'natural_join',
289                         -values     => [ 'no', 'yes', 'pk_only' ],
290                         -labels     => {
291                             no      => 'No',
292                             yes     => 'Yes, on all like-named fields',
293                             pk_only => 'Yes, but only from primary keys'
294                         },
295                         -default    => 'no',
296                         -rows       => 3,
297                     ),
298                 ] ),
299             ),
300             $q->Tr( 
301                 $q->td( [
302                     'Skip These Fields in Natural Joins:',
303                     $q->textarea(
304                         -name    => 'skip_fields',
305                         -rows    => 3,
306                         -columns => 60,
307                     ),
308                 ] ),
309             ),
310             $q->Tr( 
311                 $q->td( [
312                     'Show Only Foreign Keys:',
313                     $q->radio_group(
314                         -name    => 'show_fk_only',
315                         -values  => [ 1, 0 ],
316                         -default => 0,
317                         -labels  => {
318                             1    => 'Yes',
319                             0    => 'No',
320                         },
321                         -rows    => 2,
322                     ),
323                 ] ),
324             ),
325             $q->Tr( 
326                 $q->td( [
327                     'Add Color:',
328                     $q->radio_group(
329                         -name    => 'add_color',
330                         -values  => [ 1, 0 ],
331                         -labels  => { 
332                             1    => 'Yes', 
333                             0    => 'No' 
334                         },
335                         -default => 1,
336                         -rows    => 2,
337                     ),
338                 ] ),
339             ),
340             $q->Tr( 
341                 $q->td( [
342                     'Show Field Names:',
343                     $q->radio_group(
344                         -name    => 'show_fields',
345                         -values  => [ 1, 0 ],
346                         -default => 1,
347                         -labels  => {
348                             1    => 'Yes',
349                             0    => 'No',
350                         },
351                         -rows    => 2,
352                     ),
353                 ] ),
354             ),
355             $q->Tr( 
356                 $q->th( 
357                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
358                     'Diagram Producer Options'
359                 ),
360             ),
361             $q->Tr( 
362                 $q->td( [
363                     'Output Type:',
364                     $q->radio_group(
365                         -name    => 'diagram_output_type',
366                         -values  => [ 'png', 'jpeg' ],
367                         -default => 'png',
368                         -rows    => 2,
369                     ),
370                 ] ),
371             ),
372             $q->Tr( 
373                 $q->td( [
374                     'Font Size:',
375                     $q->radio_group(
376                         -name    => 'font_size',
377                         -values  => [ qw( small medium large ) ],
378                         -default => 'medium',
379                         -rows    => 3,
380                     ),
381                 ] ),
382             ),
383             $q->Tr( 
384                 $q->td( [
385                     'Number of Columns:',
386                     $q->textfield('no_columns'),
387                 ] ),
388             ),
389             $q->Tr( 
390                 $q->th( 
391                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
392                     'GraphViz Producer Options'
393                 ),
394             ),
395             $q->Tr( 
396                 $q->td( [
397                     'Output Type:',
398                     $q->radio_group(
399                         -name    => 'graphviz_output_type',
400                         -values  => [ qw( canon text ps hpgl pcl mif pic
401                             gd gd2 gif jpeg png wbmp cmap ismap imap
402                             vrml vtx mp fig svg plain
403                         ) ],
404                         -default => 'png',
405                         -rows    => 4,
406                     ),
407                 ] ),
408             ),
409             $q->Tr( 
410                 $q->td( [
411                     'Layout:',
412                     $q->radio_group(
413                         -name    => 'layout',
414                         -values  => [ qw( dot neato twopi ) ],
415                         -default => 'dot',
416                         -rows    => 3,
417                     ),
418                 ] ),
419             ),
420             $q->Tr( 
421                 $q->td( [
422                     'Node Shape:',
423                     $q->radio_group(
424                         -name    => 'node_shape',
425                         -values  => [ qw( record plaintext ellipse 
426                             circle egg triangle box diamond trapezium 
427                             parallelogram house hexagon octagon 
428                         ) ],
429                         -default => 'record',
430                         -rows    => 4,
431                     ),
432                 ] ),
433             ),
434             $q->Tr( 
435                 $q->td( [
436                     'Height:',
437                     $q->textfield( -name => 'height' ),
438                 ] ),
439             ),
440             $q->Tr( 
441                 $q->td( [
442                     'Width:',
443                     $q->textfield( -name => 'width' ),
444                 ] ),
445             ),
446             $q->Tr( 
447                 $q->th( 
448                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
449                     'XML Producer Options:' 
450                 ),
451             ),
452             $q->Tr( 
453                 $q->td( [
454                     'Use attributes for values:',
455                     $q->radio_group(
456                         -name    => 'attrib-values',
457                         -values  => [ 1, 0 ],
458                         -labels  => { 
459                             1    => 'Yes', 
460                             0    => 'No' 
461                         },
462                         -default => 0,
463                         -rows    => 2,
464                     ),
465                 ] ),
466             ),
467             $q->Tr( 
468                 $q->td( [
469                     'Emit Empty Tags:',
470                     $q->radio_group(
471                         -name    => 'emit-empty-tags',
472                         -values  => [ 1, 0 ],
473                         -labels  => { 
474                             1    => 'Yes', 
475                             0    => 'No' 
476                         },
477                         -default => 0,
478                         -rows    => 2,
479                     ),
480                 ] ),
481             ),
482             $q->Tr( 
483                 $q->th( 
484                     { align => 'left', bgcolor => 'lightgrey', colspan => 2 }, 
485                     'xSV Parser Options'
486                 ),
487             ),
488             $q->Tr( 
489                 $q->td( [
490                     'Field Separator:',
491                     $q->textfield( -name => 'fs' ),
492                 ] ),
493             ),
494             $q->Tr( 
495                 $q->td( [
496                     'Record Separator:',
497                     $q->textfield( -name => 'rs' ),
498                 ] ),
499             ),
500             $q->Tr( 
501                 $q->td( [
502                     'Trim Whitespace Around Fields:',
503                     $q->radio_group(
504                         -name    => 'trim_fields',
505                         -values  => [ 1, 0 ],
506                         -default => 1,
507                         -labels  => {
508                             1    => 'Yes',
509                             0    => 'No',
510                         },
511                         -rows    => 2,
512                     ),
513                 ] ),
514             ),
515             $q->Tr( 
516                 $q->td( [
517                     'Scan Fields for Data Type:',
518                     $q->radio_group(
519                         -name    => 'scan_fields',
520                         -values  => [ 1, 0 ],
521                         -default => 1,
522                         -labels  => {
523                             1    => 'Yes',
524                             0    => 'No',
525                         },
526                         -rows    => 2,
527                     ),
528                 ] ),
529             ),
530             $q->Tr( 
531                 $q->td(
532                     { -colspan => 2, -align => 'center' },
533                     $q->submit( 
534                         -name  => 'submit', 
535                         -value => 'Submit',
536                     )
537                 ),
538             ),
539         ),
540         $q->end_form,
541         $q->end_html;
542 }
543
544 # -------------------------------------------------------------------
545
546 =pod
547
548 =head1 AUTHOR
549
550 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
551
552 =head1 SEE ALSO
553
554 perl, SQL::Translator.
555
556 =cut