- Added some stuff to MANIFEST.SKIP
[dbsrgits/SQL-Translator.git] / bin / sqlt.cgi
CommitLineData
45df156a 1#!/usr/bin/perl
2
3# -------------------------------------------------------------------
821a0fde 4# $Id$
45df156a 5# -------------------------------------------------------------------
daf4f623 6# Copyright (C) 2002-4 SQLFairy Authors
45df156a 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
42b19e6f 25sqlt.cgi - CGI front-end for SQL::Translator
45df156a 26
27=head1 DESCRIPTION
28
42b19e6f 29Place this script in your "cgi-bin" directory and point your browser
30to it. This script is meant to be a simple graphical interface to
31all the parsers and producers of SQL::Translator.
45df156a 32
33=cut
34
42b19e6f 35# -------------------------------------------------------------------
36
45df156a 37use strict;
38use CGI;
39use SQL::Translator;
40
41use vars '$VERSION';
821a0fde 42$VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;
45df156a 43
44my $q = CGI->new;
45
46eval {
47 if ( $q->param ) {
45df156a 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');
42b19e6f 59 my $output_type = $producer eq 'Diagram'
60 ? $q->param('diagram_output_type')
61 : $producer eq 'GraphViz'
62 ? $q->param('graphviz_output_type')
db443c26 63 : ''
42b19e6f 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 }
db443c26 113 elsif ( $producer eq 'HTML' ) {
114 $text_type = 'html';
115 }
42b19e6f 116
117 my $header_type = $image_type ? "image/$image_type" : "text/$text_type";
45df156a 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
130if ( my $error = $@ ) {
131 print $q->header, $q->start_html('Error'),
132 $q->h1('Error'), $error, $q->end_html;
133}
134
135# -------------------------------------------------------------------
136sub 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( [
42b19e6f 147 'Upload your schema file:',
148 $q->filefield( -name => 'schema_file'),
45df156a 149 ] ),
150 ),
151 $q->Tr(
152 $q->td( [
42b19e6f 153 'Or paste your schema here:',
154 $q->textarea(
155 -name => 'schema',
156 -rows => 5,
157 -columns => 60,
158 ),
45df156a 159 ] ),
160 ),
161 $q->Tr(
162 $q->td( [
163 'Parser:',
164 $q->radio_group(
165 -name => 'parser',
42b19e6f 166 -values => [ qw( MySQL PostgreSQL Oracle
167 Sybase Excel XML-SQLFairy xSV
168 ) ],
45df156a 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
42b19e6f 180 MySQL Oracle POD PostgreSQL SQLite Sybase
181 TTSchema XML-SQLFairy
45df156a 182 ] ],
183 -default => 'GraphViz',
184 -rows => 3,
185 ),
186 ] ),
187 ),
188 $q->Tr(
42b19e6f 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(
45df156a 204 $q->td( [
42b19e6f 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 ),
45df156a 216 ] ),
217 ),
218 $q->Tr(
42b19e6f 219 $q->th(
220 { align => 'left', bgcolor => 'lightgrey', colspan => 2 },
221 'DB Producer Options:'
222 ),
223 ),
224 $q->Tr(
45df156a 225 $q->td( [
42b19e6f 226 'Add &quot;DROP TABLE&quot; statements:',
45df156a 227 $q->radio_group(
42b19e6f 228 -name => 'add_drop_table',
229 -values => [ 1, 0 ],
230 -labels => {
231 1 => 'Yes',
232 0 => 'No'
233 },
234 -default => 0,
45df156a 235 -rows => 2,
236 ),
237 ] ),
238 ),
239 $q->Tr(
240 $q->td( [
42b19e6f 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( [
45df156a 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( [
42b19e6f 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:',
45df156a 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( [
42b19e6f 342 'Show Field Names:',
45df156a 343 $q->radio_group(
42b19e6f 344 -name => 'show_fields',
45df156a 345 -values => [ 1, 0 ],
42b19e6f 346 -default => 1,
45df156a 347 -labels => {
348 1 => 'Yes',
349 0 => 'No',
350 },
351 -rows => 2,
352 ),
353 ] ),
354 ),
355 $q->Tr(
42b19e6f 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(
45df156a 373 $q->td( [
42b19e6f 374 'Font Size:',
45df156a 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( [
42b19e6f 385 'Number of Columns:',
45df156a 386 $q->textfield('no_columns'),
387 ] ),
388 ),
389 $q->Tr(
42b19e6f 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(
45df156a 410 $q->td( [
42b19e6f 411 'Layout:',
45df156a 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( [
42b19e6f 422 'Node Shape:',
45df156a 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',
42b19e6f 430 -rows => 4,
45df156a 431 ),
432 ] ),
433 ),
434 $q->Tr(
435 $q->td( [
42b19e6f 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:',
45df156a 455 $q->radio_group(
42b19e6f 456 -name => 'attrib-values',
45df156a 457 -values => [ 1, 0 ],
42b19e6f 458 -labels => {
459 1 => 'Yes',
460 0 => 'No'
45df156a 461 },
42b19e6f 462 -default => 0,
45df156a 463 -rows => 2,
464 ),
465 ] ),
466 ),
467 $q->Tr(
468 $q->td( [
42b19e6f 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' ),
45df156a 492 ] ),
493 ),
494 $q->Tr(
495 $q->td( [
42b19e6f 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 ),
45df156a 528 ] ),
529 ),
530 $q->Tr(
531 $q->td(
532 { -colspan => 2, -align => 'center' },
533 $q->submit(
534 -name => 'submit',
535 -value => 'Submit',
42b19e6f 536 )
45df156a 537 ),
538 ),
539 ),
540 $q->end_form,
541 $q->end_html;
542}
543
544# -------------------------------------------------------------------
545
546=pod
547
548=head1 AUTHOR
549
daf4f623 550Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
45df156a 551
552=head1 SEE ALSO
553
554perl, SQL::Translator.
555
556=cut