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