Added lib/SQL/Translator/XMI/Parser.pm
[dbsrgits/SQL-Translator.git] / bin / sqlt.cgi
CommitLineData
45df156a 1#!/usr/bin/perl
2
3# -------------------------------------------------------------------
42b19e6f 4# $Id: sqlt.cgi,v 1.2 2003-08-27 04:01:14 kycl4rk Exp $
45df156a 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
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';
42b19e6f 42$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\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')
63 : 'png'
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
114 my $header_type = $image_type ? "image/$image_type" : "text/$text_type";
45df156a 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
127if ( my $error = $@ ) {
128 print $q->header, $q->start_html('Error'),
129 $q->h1('Error'), $error, $q->end_html;
130}
131
132# -------------------------------------------------------------------
133sub 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( [
42b19e6f 144 'Upload your schema file:',
145 $q->filefield( -name => 'schema_file'),
45df156a 146 ] ),
147 ),
148 $q->Tr(
149 $q->td( [
42b19e6f 150 'Or paste your schema here:',
151 $q->textarea(
152 -name => 'schema',
153 -rows => 5,
154 -columns => 60,
155 ),
45df156a 156 ] ),
157 ),
158 $q->Tr(
159 $q->td( [
160 'Parser:',
161 $q->radio_group(
162 -name => 'parser',
42b19e6f 163 -values => [ qw( MySQL PostgreSQL Oracle
164 Sybase Excel XML-SQLFairy xSV
165 ) ],
45df156a 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
42b19e6f 177 MySQL Oracle POD PostgreSQL SQLite Sybase
178 TTSchema XML-SQLFairy
45df156a 179 ] ],
180 -default => 'GraphViz',
181 -rows => 3,
182 ),
183 ] ),
184 ),
185 $q->Tr(
42b19e6f 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(
45df156a 201 $q->td( [
42b19e6f 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 ),
45df156a 213 ] ),
214 ),
215 $q->Tr(
42b19e6f 216 $q->th(
217 { align => 'left', bgcolor => 'lightgrey', colspan => 2 },
218 'DB Producer Options:'
219 ),
220 ),
221 $q->Tr(
45df156a 222 $q->td( [
42b19e6f 223 'Add &quot;DROP TABLE&quot; statements:',
45df156a 224 $q->radio_group(
42b19e6f 225 -name => 'add_drop_table',
226 -values => [ 1, 0 ],
227 -labels => {
228 1 => 'Yes',
229 0 => 'No'
230 },
231 -default => 0,
45df156a 232 -rows => 2,
233 ),
234 ] ),
235 ),
236 $q->Tr(
237 $q->td( [
42b19e6f 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( [
45df156a 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( [
42b19e6f 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:',
45df156a 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( [
42b19e6f 339 'Show Field Names:',
45df156a 340 $q->radio_group(
42b19e6f 341 -name => 'show_fields',
45df156a 342 -values => [ 1, 0 ],
42b19e6f 343 -default => 1,
45df156a 344 -labels => {
345 1 => 'Yes',
346 0 => 'No',
347 },
348 -rows => 2,
349 ),
350 ] ),
351 ),
352 $q->Tr(
42b19e6f 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(
45df156a 370 $q->td( [
42b19e6f 371 'Font Size:',
45df156a 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( [
42b19e6f 382 'Number of Columns:',
45df156a 383 $q->textfield('no_columns'),
384 ] ),
385 ),
386 $q->Tr(
42b19e6f 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(
45df156a 407 $q->td( [
42b19e6f 408 'Layout:',
45df156a 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( [
42b19e6f 419 'Node Shape:',
45df156a 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',
42b19e6f 427 -rows => 4,
45df156a 428 ),
429 ] ),
430 ),
431 $q->Tr(
432 $q->td( [
42b19e6f 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:',
45df156a 452 $q->radio_group(
42b19e6f 453 -name => 'attrib-values',
45df156a 454 -values => [ 1, 0 ],
42b19e6f 455 -labels => {
456 1 => 'Yes',
457 0 => 'No'
45df156a 458 },
42b19e6f 459 -default => 0,
45df156a 460 -rows => 2,
461 ),
462 ] ),
463 ),
464 $q->Tr(
465 $q->td( [
42b19e6f 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' ),
45df156a 489 ] ),
490 ),
491 $q->Tr(
492 $q->td( [
42b19e6f 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 ),
45df156a 525 ] ),
526 ),
527 $q->Tr(
528 $q->td(
529 { -colspan => 2, -align => 'center' },
530 $q->submit(
531 -name => 'submit',
532 -value => 'Submit',
42b19e6f 533 )
45df156a 534 ),
535 ),
536 ),
537 $q->end_form,
538 $q->end_html;
539}
540
541# -------------------------------------------------------------------
542
543=pod
544
545=head1 AUTHOR
546
547Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
548
549=head1 SEE ALSO
550
551perl, SQL::Translator.
552
553=cut