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