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