refining the parser, but it still doesn't do everything it should:
[dbsrgits/SQL-Translator.git] / bin / sqlt
CommitLineData
7a8e1f51 1#!/usr/bin/perl -w
e107f4d2 2# vim: set ft=perl:
16dc9970 3
49e1eb70 4# -------------------------------------------------------------------
c1addbe9 5# $Id: sqlt,v 1.18 2005-07-06 19:20:27 duality72 Exp $
49e1eb70 6# -------------------------------------------------------------------
daf4f623 7# Copyright (C) 2002-4 SQLFairy Authors
783908a1 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
af2301d7 34For a list of all parsers and producers:
44598b94 35
36 sqlt -l|--list
37
38To translate a schema:
39
af2301d7 40 sqlt -f|--from|--parser MySQL
41 -t|--to|--producer Oracle
42 [options]
44598b94 43 file [file2 ...]
44
45 General Options:
46
47 -d|--debug Print debug info
48 -v|--validate Validate the schema
4ae3a779 49 --version Show the version of SQL::Translator
44598b94 50 --trace Print parser trace info
51 --show-warnings Print warnings to STDERR
52
e107f4d2 53 DBI Parser Options:
54
55 --dsn DSN for connecting to database
56 --db-user Database user
af2301d7 57 --db-password Database password
e107f4d2 58
44598b94 59 xSV Parser Options:
60
61 --fs The field separator
62 --rs The record separator
af2301d7 63 --no-trim Don't trim whitespace on fields
64 --no-scan Don't scan fields for data types and sizes
44598b94 65
66 DB Producer Options:
67
68 --add-drop-table Add 'DROP TABLE' statements before creates
69 --no-comments Don't include comments in SQL output
70
71 Diagram Producer Options:
72
73 --imap-file Filename to put image map data
74 --imap-url URL to use for image map
75
e8aa1b67 76 Dumper Producer Options:
77
78 --skip Comma-separated list of tables to skip
79 --skiplike Regex for tables to skip
80 --dumper-db-user Database user for dumper script
81 --dumper-db-pass Database password for dumper script
82 --dumper-dsn DSN for dumper script
83 --add-truncate Add "TRUNCATE TABLE" statements for each table
84
44598b94 85 HTML/POD Producer Options:
86
1ae0e8cb 87 --pretty Use CGI::Pretty for the output
44598b94 88 --title Title of schema
89
90 TTSchema Producer Options:
91
d955ed82 92 --template The path to the template
93 --tt-var var=value Pass extra variables to the template
94 --tt-conf option=value Pass extra config options to Template
44598b94 95
96 XML-SQLFairy Producer Options:
97
af2301d7 98 --add-prefix Use an explicit namespace prefix of 'sqlf:'
99 --prefix=<p> Use the namespace prefix given as argument.
100 --no-newlines Write the XML as a single line.
101 --indent=<n> Use <n> characters of whitespace to indent the XML.
44598b94 102
103=head1 DESCRIPTION
104
105This script is part of the SQL Fairy project. It will try to convert
106any source file for which it has a grammar into any format for which
107it has a producer.
108
af2301d7 109If using "show-warnings," be sure to redirect STDERR to a separate file.
44598b94 110In bash, you could do this:
111
112 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
113 file.sql 1>out 2>err
114
115You can specify a parser or producer located in any module that Perl
116knows about, allowing you to easily substitute your own.
117
118=cut
119
120# -------------------------------------------------------------------
121
16dc9970 122use strict;
123use Getopt::Long;
124use Pod::Usage;
125use SQL::Translator;
49e1eb70 126
16dc9970 127use vars qw( $VERSION );
c1addbe9 128$VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
64ad4e66 129
130my $from; # the original database
af2301d7 131my $to; # the destination database
64ad4e66 132my $help; # show POD and bail
133my $stdin; # whether to read STDIN for create script
134my $no_comments; # whether to put comments in out file
135my $show_warnings; # whether to show warnings from SQL::Translator
136my $add_drop_table; # whether to show warnings from SQL::Translator
64ad4e66 137my $debug; # whether to print debug info
138my $trace; # whether to print parser trace
139my $list; # list all parsers and producers
032bd3c7 140my $no_trim; # don't trim whitespace on xSV fields
141my $no_scan; # don't scan xSV fields for data types and sizes
64ad4e66 142my $field_separator; # for xSV files
143my $record_separator; # for xSV files
35d75351 144my $validate; # whether to validate the parsed document
5cb879e2 145my $imap_file; # filename where to place image map coords
146my $imap_url; # URL to use in making image map
1ea530d4 147my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
8ac2c35e 148my $template; # template to pass to TTSchema producer
3658d749 149my %tt_vars; # additional template vars to pass the TTSchema producer
d955ed82 150my %tt_conf; # additional template conf to pass the TTSchema producer
44598b94 151my $title; # title for HTML/POD producer
af2301d7 152my $add_prefix; # Use explicit namespace prefix (XML producer)
153my $prefix; # Set explicit namespace prefix (XML producer)
154my $newlines; # Add newlines around tags (XML producer)
155my $indent; # Number of indent chars for XML
156my $dsn; # DBI parser
157my $db_user; # DBI parser
158my $db_password; # DBI parser
4ae3a779 159my $show_version; # Show version and exit script
af2301d7 160my $skip;
161my $skiplike;
162my $dumper_db_user;
e8aa1b67 163my $dumper_db_pass;
164my $dumper_dsn;
165my $add_truncate;
16dc9970 166
16dc9970 167GetOptions(
e8aa1b67 168 'add-drop-table' => \$add_drop_table,
e8aa1b67 169 'd|debug' => \$debug,
e8aa1b67 170 'f|from|parser:s' => \$from,
171 'fs:s' => \$field_separator,
172 'h|help' => \$help,
173 'imap-file:s' => \$imap_file,
174 'imap-url:s' => \$imap_url,
175 't|to|producer:s' => \$to,
176 'l|list' => \$list,
177 'pretty!' => \$pretty,
178 'no-comments' => \$no_comments,
179 'no-scan' => \$no_scan,
180 'no-trim' => \$no_trim,
181 'rs:s' => \$record_separator,
182 'show-warnings' => \$show_warnings,
183 'template:s' => \$template,
3658d749 184 'tt-var=s' => \%tt_vars,
d955ed82 185 'tt-conf=s' => \%tt_conf,
e8aa1b67 186 'title:s' => \$title,
187 'trace' => \$trace,
188 'v|validate' => \$validate,
189 'dsn:s' => \$dsn,
190 'db-user:s' => \$db_user,
191 'db-password:s' => \$db_password,
192 'dumper-dsn:s' => \$dumper_dsn,
193 'dumper-db-user:s' => \$dumper_db_user,
194 'dumper-db-pass:s' => \$dumper_db_pass,
195 'skip:s' => \$skip,
196 'skiplike:s' => \$skiplike,
197 'add_truncate' => \$add_truncate,
af2301d7 198 'add-prefix' => \$add_prefix,
199 'prefix:s' => \$prefix,
200 'indent:s' => \$indent,
201 'newlines!' => \$newlines,
4ae3a779 202 'version' => \$show_version,
16dc9970 203) or pod2usage(2);
204
c1addbe9 205$from = 'DBI' if !defined $from && defined $dsn;
ec42851e 206my @files = @ARGV; # source files
b79348f4 207unless ( @files ) {
d4becbfd 208 if ( defined($from) && $from eq 'DBI' ) {
b79348f4 209 @files = ('!');
210 }
211 else {
212 @files = ('-');
213 }
214}
16dc9970 215
216pod2usage(1) if $help;
d529894e 217
4ae3a779 218if ( $show_version ) {
219 print "SQL::Translator v", $SQL::Translator::VERSION, "\n";
220 exit(0);
221}
222
44598b94 223my $translator = SQL::Translator->new(
224 debug => $debug || 0,
225 trace => $trace || 0,
226 no_comments => $no_comments || 0,
227 show_warnings => $show_warnings || 0,
228 add_drop_table => $add_drop_table || 0,
229 validate => $validate || 0,
230 parser_args => {
032bd3c7 231 trim_fields => $no_trim ? 0 : 1,
232 scan_fields => $no_scan ? 0 : 1,
64ad4e66 233 field_separator => $field_separator,
234 record_separator => $record_separator,
e107f4d2 235 dsn => $dsn,
236 db_user => $db_user,
237 db_password => $db_password,
5cb879e2 238 },
239 producer_args => {
240 imap_file => $imap_file,
241 imap_url => $imap_url,
1ea530d4 242 pretty => $pretty,
8ac2c35e 243 ttfile => $template,
3658d749 244 tt_vars => \%tt_vars,
d955ed82 245 tt_conf => \%tt_conf,
44598b94 246 title => $title,
e8aa1b67 247 dsn => $dumper_dsn,
248 db_user => $dumper_db_user,
249 db_password => $dumper_db_pass,
250 skip => $skip,
251 skiplike => $skiplike,
252 add_truncate => $add_truncate,
af2301d7 253 add_prefix => $add_prefix,
254 prefix => $prefix,
255 indent => $indent,
256 newlines => $newlines,
5cb879e2 257 },
d529894e 258);
259
260if ( $list ) {
261 my @parsers = $translator->list_parsers;
262 my @producers = $translator->list_producers;
263
264 for ( @parsers, @producers ) {
265 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
266 $_ = $1;
267 }
268 }
af2301d7 269
d529894e 270 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
271 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
272 print "\n";
273 exit(0);
274}
275
af2301d7 276pod2usage( msg => 'Please supply "from" and "to" arguments' )
515a6a58 277 unless $from && $to;
d529894e 278
783908a1 279$translator->parser($from);
280$translator->producer($to);
281
282for my $file (@files) {
b79348f4 283 my @args =
284 ($file eq '-') ? (data => \*STDIN) :
285 ($file eq '!') ? (data => '') :
286 (file => $file);
287
2cd6675e 288 my $output = $translator->translate(@args) or die
49e1eb70 289 "Error: " . $translator->error;
b79348f4 290
49e1eb70 291 print $output;
783908a1 292}
16dc9970 293
49e1eb70 294# ----------------------------------------------------
16dc9970 295# It is not all books that are as dull as their readers.
296# Henry David Thoreau
49e1eb70 297# ----------------------------------------------------
16dc9970 298
44598b94 299=pod
96844cae 300
16dc9970 301=head1 AUTHOR
302
515a6a58 303Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
304darren chamberlain E<lt>darren@cpan.orgE<gt>.
16dc9970 305
306=head1 SEE ALSO
307
515a6a58 308SQL::Translator, L<http://sqlfairy.sourceforge.net>.
16dc9970 309
310=cut