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