Added "version" argument to show SQLT::VERSION.
[dbsrgits/SQL-Translator.git] / bin / sqlt
1 #!/usr/bin/perl -w
2 # vim: set ft=perl:
3
4 # -------------------------------------------------------------------
5 # $Id: sqlt,v 1.15 2004-08-12 16:18:00 kycl4rk 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     --db-user          Database user
57     --db-password      Database password
58
59   xSV Parser Options:
60
61     --fs               The field separator
62     --rs               The record separator
63     --no-trim          Don't trim whitespace on fields
64     --no-scan          Don't scan fields for data types and sizes
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
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
85   HTML/POD Producer Options:
86
87     --pretty           Use CGI::Pretty for the output
88     --title            Title of schema
89
90   TTSchema Producer Options:
91
92     --template         The path to the template
93
94   XML-SQLFairy Producer Options:
95
96     --add-prefix       Use an explicit namespace prefix of 'sqlf:'
97     --prefix=<p>       Use the namespace prefix given as argument.
98     --no-newlines      Write the XML as a single line.
99     --indent=<n>       Use <n> characters of whitespace to indent the XML.
100
101 =head1 DESCRIPTION
102
103 This script is part of the SQL Fairy project.  It will try to convert
104 any source file for which it has a grammar into any format for which
105 it has a producer.
106
107 If using "show-warnings," be sure to redirect STDERR to a separate file.
108 In bash, you could do this:
109
110     $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
111        file.sql 1>out 2>err
112
113 You can specify a parser or producer located in any module that Perl
114 knows about, allowing you to easily substitute your own.
115
116 =cut
117
118 # -------------------------------------------------------------------
119
120 use strict;
121 use Getopt::Long;
122 use Pod::Usage;
123 use SQL::Translator;
124
125 use vars qw( $VERSION );
126 $VERSION = sprintf "%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/;
127
128 my $from;             # the original database
129 my $to;               # the destination database
130 my $help;             # show POD and bail
131 my $stdin;            # whether to read STDIN for create script
132 my $no_comments;      # whether to put comments in out file
133 my $show_warnings;    # whether to show warnings from SQL::Translator
134 my $add_drop_table;   # whether to show warnings from SQL::Translator
135 my $debug;            # whether to print debug info
136 my $trace;            # whether to print parser trace
137 my $list;             # list all parsers and producers
138 my $no_trim;          # don't trim whitespace on xSV fields
139 my $no_scan;          # don't scan xSV fields for data types and sizes
140 my $field_separator;  # for xSV files
141 my $record_separator; # for xSV files
142 my $validate;         # whether to validate the parsed document
143 my $imap_file;        # filename where to place image map coords
144 my $imap_url;         # URL to use in making image map
145 my $pretty;           # use CGI::Pretty instead of CGI (HTML producer)
146 my $template;         # template to pass to TTSchema producer
147 my $title;            # title for HTML/POD producer
148 my $add_prefix;       # Use explicit namespace prefix (XML producer)
149 my $prefix;           # Set explicit namespace prefix (XML producer)
150 my $newlines;         # Add newlines around tags (XML producer)
151 my $indent;           # Number of indent chars for XML
152 my $dsn;              # DBI parser
153 my $db_user;          # DBI parser
154 my $db_password;      # DBI parser
155 my $show_version;     # Show version and exit script
156 my $skip;
157 my $skiplike;
158 my $dumper_db_user;
159 my $dumper_db_pass;
160 my $dumper_dsn;
161 my $add_truncate;
162
163 GetOptions(
164     'add-drop-table'   => \$add_drop_table,
165     'd|debug'          => \$debug,
166     'f|from|parser:s'  => \$from,
167     'fs:s'             => \$field_separator,
168     'h|help'           => \$help,
169     'imap-file:s'      => \$imap_file,
170     'imap-url:s'       => \$imap_url,
171     't|to|producer:s'  => \$to,
172     'l|list'           => \$list,
173     'pretty!'          => \$pretty,
174     'no-comments'      => \$no_comments,
175     'no-scan'          => \$no_scan,
176     'no-trim'          => \$no_trim,
177     'rs:s'             => \$record_separator,
178     'show-warnings'    => \$show_warnings,
179     'template:s'       => \$template,
180     'title:s'          => \$title,
181     'trace'            => \$trace,
182     'v|validate'       => \$validate,
183     'dsn:s'            => \$dsn,
184     'db-user:s'        => \$db_user,
185     'db-password:s'    => \$db_password,
186     'dumper-dsn:s'     => \$dumper_dsn,
187     'dumper-db-user:s' => \$dumper_db_user,
188     'dumper-db-pass:s' => \$dumper_db_pass,
189     'skip:s'           => \$skip,
190     'skiplike:s'       => \$skiplike,
191     'add_truncate'     => \$add_truncate,
192     'add-prefix'       => \$add_prefix,
193     'prefix:s'         => \$prefix,
194     'indent:s'         => \$indent,
195     'newlines!'        => \$newlines,
196     'version'          => \$show_version,
197 ) or pod2usage(2);
198
199 my @files = @ARGV; # source files
200 unless ( @files ) {
201     if ( defined($from) && $from eq 'DBI' ) {
202         @files = ('!');
203     }
204     else {
205         @files = ('-');
206     }
207 }
208
209 pod2usage(1) if $help;
210
211 if ( $show_version ) {
212     print "SQL::Translator v", $SQL::Translator::VERSION, "\n";
213     exit(0);
214 }
215
216 my $translator           =  SQL::Translator->new( 
217     debug                => $debug          ||  0,
218     trace                => $trace          ||  0,
219     no_comments          => $no_comments    ||  0,
220     show_warnings        => $show_warnings  ||  0,
221     add_drop_table       => $add_drop_table ||  0,
222     validate             => $validate       ||  0,
223     parser_args          => {
224         trim_fields      => $no_trim ? 0 : 1,
225         scan_fields      => $no_scan ? 0 : 1,
226         field_separator  => $field_separator,
227         record_separator => $record_separator,
228         dsn              => $dsn,
229         db_user          => $db_user,
230         db_password      => $db_password,
231     },
232     producer_args   => {
233         imap_file        => $imap_file,
234         imap_url         => $imap_url,
235         pretty           => $pretty,
236         ttfile           => $template,
237         title            => $title,
238         dsn              => $dumper_dsn,
239         db_user          => $dumper_db_user,
240         db_password      => $dumper_db_pass,
241         skip             => $skip,
242         skiplike         => $skiplike,
243         add_truncate     => $add_truncate,
244         add_prefix       => $add_prefix,
245         prefix           => $prefix,
246         indent           => $indent,
247         newlines         => $newlines,
248     },
249 );
250
251 if ( $list ) {
252     my @parsers   = $translator->list_parsers;
253     my @producers = $translator->list_producers;
254
255     for ( @parsers, @producers ) {
256         if ( $_ =~ m/.+::(\w+)\.pm/ ) {
257             $_ = $1;
258         }
259     }
260
261     print "\nParsers:\n",   map { "\t$_\n" } sort @parsers;
262     print "\nProducers:\n", map { "\t$_\n" } sort @producers;
263     print "\n";
264     exit(0);
265 }
266
267 pod2usage( msg => 'Please supply "from" and "to" arguments' )
268     unless $from && $to;
269
270 $translator->parser($from);
271 $translator->producer($to);
272
273 for my $file (@files) {
274     my @args = 
275         ($file eq '-') ? (data => \*STDIN) :
276         ($file eq '!') ? (data => '') :
277         (file => $file);
278
279     my $output = $translator->translate(@args) or die
280         "Error: " . $translator->error;
281
282     print $output;
283 }
284
285 # ----------------------------------------------------
286 # It is not all books that are as dull as their readers.
287 # Henry David Thoreau
288 # ----------------------------------------------------
289
290 =pod
291
292 =head1 AUTHOR
293
294 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
295 darren chamberlain E<lt>darren@cpan.orgE<gt>.
296
297 =head1 SEE ALSO
298
299 SQL::Translator, L<http://sqlfairy.sourceforge.net>.
300
301 =cut