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