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