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