turnkey producer still broken, will try to get it working on the plane...
[dbsrgits/SQL-Translator.git] / bin / sqlt
CommitLineData
7a8e1f51 1#!/usr/bin/perl -w
16dc9970 2
49e1eb70 3# -------------------------------------------------------------------
44598b94 4# $Id: sqlt,v 1.2 2003-08-26 03:54:59 kycl4rk Exp $
49e1eb70 5# -------------------------------------------------------------------
44598b94 6# Copyright (C) 2002 Ken Y. Clark <kclar@cpan.org>,
783908a1 7# darren chamberlain <darren@cpan.org>
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2.
12#
13# This program is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21# 02111-1307 USA
22# -------------------------------------------------------------------
16dc9970 23
44598b94 24=head1 NAME
25
26sqlt - convert SQL schema using SQL::Translator
27
28=head1 SYNOPSIS
29
30For help:
31
32 sqlt -h|--help
33
34For a list of all parsers and producers:
35
36 sqlt -l|--list
37
38To translate a schema:
39
40 sqlt -f|--from|--parser MySQL
41 -t|--to|--producer Oracle
42 [options]
43 file [file2 ...]
44
45 General Options:
46
47 -d|--debug Print debug info
48 -v|--validate Validate the schema
49 --trace Print parser trace info
50 --show-warnings Print warnings to STDERR
51
52 xSV Parser Options:
53
54 --fs The field separator
55 --rs The record separator
56 --no-trim Don't trim whitespace on fields
57 --no-scan Don't scan fields for data types and sizes
58
59 DB Producer Options:
60
61 --add-drop-table Add 'DROP TABLE' statements before creates
62 --no-comments Don't include comments in SQL output
63
64 Diagram Producer Options:
65
66 --imap-file Filename to put image map data
67 --imap-url URL to use for image map
68
69 HTML/POD Producer Options:
70
71 --pretty Use CGI::Pretty for the outpu
72 --title Title of schema
73
74 TTSchema Producer Options:
75
76 --template The path to the template
77
78 XML-SQLFairy Producer Options:
79
80 --emit-empty-tags Print empty tags for attributes
81 --attrib-values Use attributes instead of tags for
82 values of the schema objects
83
84=head1 DESCRIPTION
85
86This script is part of the SQL Fairy project. It will try to convert
87any source file for which it has a grammar into any format for which
88it has a producer.
89
90If using "show-warnings," be sure to redirect STDERR to a separate file.
91In bash, you could do this:
92
93 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
94 file.sql 1>out 2>err
95
96You can specify a parser or producer located in any module that Perl
97knows about, allowing you to easily substitute your own.
98
99=cut
100
101# -------------------------------------------------------------------
102
16dc9970 103use strict;
104use Getopt::Long;
105use Pod::Usage;
106use SQL::Translator;
49e1eb70 107
16dc9970 108use vars qw( $VERSION );
44598b94 109$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
64ad4e66 110
111my $from; # the original database
112my $to; # the destination database
113my $help; # show POD and bail
114my $stdin; # whether to read STDIN for create script
115my $no_comments; # whether to put comments in out file
116my $show_warnings; # whether to show warnings from SQL::Translator
117my $add_drop_table; # whether to show warnings from SQL::Translator
64ad4e66 118my $debug; # whether to print debug info
119my $trace; # whether to print parser trace
120my $list; # list all parsers and producers
032bd3c7 121my $no_trim; # don't trim whitespace on xSV fields
122my $no_scan; # don't scan xSV fields for data types and sizes
64ad4e66 123my $field_separator; # for xSV files
124my $record_separator; # for xSV files
35d75351 125my $validate; # whether to validate the parsed document
5cb879e2 126my $imap_file; # filename where to place image map coords
127my $imap_url; # URL to use in making image map
1ea530d4 128my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
8ac2c35e 129my $template; # template to pass to TTSchema producer
44598b94 130my $title; # title for HTML/POD producer
131my $emit_empty_tags; # show empty XML tags
132my $attrib_values; # use XML attributes instead of tags
16dc9970 133
16dc9970 134GetOptions(
44598b94 135 'add-drop-table' => \$add_drop_table,
136 'attrib-values' => \$attrib_values,
137 'd|debug' => \$debug,
138 'emit_empty_tags' => \$emit_empty_tags,
d529894e 139 'f|from|parser:s' => \$from,
44598b94 140 'fs:s' => \$field_separator,
783908a1 141 'h|help' => \$help,
44598b94 142 'imap-file:s' => \$imap_file,
143 'imap-url:s' => \$imap_url,
144 't|to|producer:s' => \$to,
d529894e 145 'l|list' => \$list,
44598b94 146 'pretty!' => \$pretty,
d529894e 147 'no-comments' => \$no_comments,
032bd3c7 148 'no-scan' => \$no_scan,
44598b94 149 'no-trim' => \$no_trim,
64ad4e66 150 'rs:s' => \$record_separator,
44598b94 151 'show-warnings' => \$show_warnings,
8ac2c35e 152 'template:s' => \$template,
44598b94 153 'title:s' => \$title,
154 'trace' => \$trace,
155 'v|validate' => \$validate,
16dc9970 156) or pod2usage(2);
157
44598b94 158my @files = @ARGV; # source files
16dc9970 159
160pod2usage(1) if $help;
d529894e 161
44598b94 162my $translator = SQL::Translator->new(
163 debug => $debug || 0,
164 trace => $trace || 0,
165 no_comments => $no_comments || 0,
166 show_warnings => $show_warnings || 0,
167 add_drop_table => $add_drop_table || 0,
168 validate => $validate || 0,
169 parser_args => {
032bd3c7 170 trim_fields => $no_trim ? 0 : 1,
171 scan_fields => $no_scan ? 0 : 1,
64ad4e66 172 field_separator => $field_separator,
173 record_separator => $record_separator,
5cb879e2 174 },
175 producer_args => {
176 imap_file => $imap_file,
177 imap_url => $imap_url,
1ea530d4 178 pretty => $pretty,
8ac2c35e 179 ttfile => $template,
44598b94 180 title => $title,
181 emit_empty_tags => $emit_empty_tags,
182 attrib_values => $attrib_values,
5cb879e2 183 },
d529894e 184);
185
186if ( $list ) {
187 my @parsers = $translator->list_parsers;
188 my @producers = $translator->list_producers;
189
190 for ( @parsers, @producers ) {
191 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
192 $_ = $1;
193 }
194 }
195
196 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
197 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
198 print "\n";
199 exit(0);
200}
201
202pod2usage(2) unless $from && $to && @files;
203
783908a1 204$translator->parser($from);
205$translator->producer($to);
206
207for my $file (@files) {
1ea530d4 208 my $output = $translator->translate(file => $file) or die
49e1eb70 209 "Error: " . $translator->error;
210 print $output;
783908a1 211}
16dc9970 212
49e1eb70 213# ----------------------------------------------------
16dc9970 214# It is not all books that are as dull as their readers.
215# Henry David Thoreau
49e1eb70 216# ----------------------------------------------------
16dc9970 217
44598b94 218=pod
96844cae 219
16dc9970 220=head1 AUTHOR
221
44598b94 222Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
16dc9970 223
224=head1 SEE ALSO
225
44598b94 226perl, SQL::Translator, Parse::RecDescent,
227L<http://sqlfairy.sourceforge.net>.
16dc9970 228
229=cut