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