Fixed name, perl.
[dbsrgits/SQL-Translator.git] / bin / sqlt
CommitLineData
7a8e1f51 1#!/usr/bin/perl -w
e107f4d2 2# vim: set ft=perl:
16dc9970 3
49e1eb70 4# -------------------------------------------------------------------
478f608d 5# Copyright (C) 2002-2009 SQLFairy Authors
783908a1 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# -------------------------------------------------------------------
16dc9970 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)
f7f81963 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
90 --quote-field-names Qjuote 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;
150use Getopt::Long;
151use Pod::Usage;
152use SQL::Translator;
49e1eb70 153
da06ac74 154use vars qw( $VERSION );
4ab3763d 155$VERSION = '1.59';
da06ac74 156
64ad4e66 157my $from; # the original database
af2301d7 158my $to; # the destination database
64ad4e66 159my $help; # show POD and bail
160my $stdin; # whether to read STDIN for create script
161my $no_comments; # whether to put comments in out file
162my $show_warnings; # whether to show warnings from SQL::Translator
216af0c9 163my $add_drop_table; # whether to add "DROP table" statements
164my $quote_table_names; # whether to quote table names
165my $quote_field_names; # whether to quote field names
64ad4e66 166my $debug; # whether to print debug info
167my $trace; # whether to print parser trace
168my $list; # list all parsers and producers
032bd3c7 169my $no_trim; # don't trim whitespace on xSV fields
170my $no_scan; # don't scan xSV fields for data types and sizes
64ad4e66 171my $field_separator; # for xSV files
172my $record_separator; # for xSV files
35d75351 173my $validate; # whether to validate the parsed document
5cb879e2 174my $imap_file; # filename where to place image map coords
175my $imap_url; # URL to use in making image map
1ea530d4 176my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
8ac2c35e 177my $template; # template to pass to TTSchema producer
3658d749 178my %tt_vars; # additional template vars to pass the TTSchema producer
d955ed82 179my %tt_conf; # additional template conf to pass the TTSchema producer
44598b94 180my $title; # title for HTML/POD producer
af2301d7 181my $add_prefix; # Use explicit namespace prefix (XML producer)
182my $prefix; # Set explicit namespace prefix (XML producer)
183my $newlines; # Add newlines around tags (XML producer)
184my $indent; # Number of indent chars for XML
4330d04e 185my $package_name; # Base class name for ClassDBI
186my $use_same_auth =0; # producer uses same DSN, user, password as parser
af2301d7 187my $dsn; # DBI parser
188my $db_user; # DBI parser
189my $db_password; # DBI parser
4ae3a779 190my $show_version; # Show version and exit script
af2301d7 191my $skip;
192my $skiplike;
f7f81963 193my $ignore_opts;
4330d04e 194my $producer_db_user; # DSN for producer (e.g. Dumper, ClassDBI)
195my $producer_db_password; # db_pass "
196my $producer_dsn; # db_user "
e8aa1b67 197my $add_truncate;
b7ea3cc2 198my $mysql_parser_version; # MySQL parser arg for /*! comments
5342f5c1 199my $postgres_version; # PostgreSQL version
ca1f9923 200my $mysql_version; # MySQL version
16dc9970 201
16dc9970 202GetOptions(
e8aa1b67 203 'add-drop-table' => \$add_drop_table,
216af0c9 204 'quote_table_names' => \$quote_table_names,
205 'quote_field_names' => \$quote_field_names,
e8aa1b67 206 'd|debug' => \$debug,
e8aa1b67 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,
3658d749 221 'tt-var=s' => \%tt_vars,
d955ed82 222 'tt-conf=s' => \%tt_conf,
e8aa1b67 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,
4330d04e 229 'producer-dsn:s' => \$producer_dsn,
230 'producer-db-user:s'=> \$producer_db_user,
231 'producer-db-pass:s'=> \$producer_db_password,
e8aa1b67 232 'skip:s' => \$skip,
233 'skiplike:s' => \$skiplike,
f7f81963 234 'ignore_opts:s' => \$ignore_opts,
e8aa1b67 235 'add_truncate' => \$add_truncate,
af2301d7 236 'add-prefix' => \$add_prefix,
237 'prefix:s' => \$prefix,
238 'indent:s' => \$indent,
239 'newlines!' => \$newlines,
4330d04e 240 'package=s' => \$package_name,
241 'use-same-auth' => \$use_same_auth,
4ae3a779 242 'version' => \$show_version,
b7ea3cc2 243 'mysql-parser-version=i' => \$mysql_parser_version,
5342f5c1 244 'postgres-version=f' => \$postgres_version,
ca1f9923 245 'mysql-version=f' => \$mysql_version,
16dc9970 246) or pod2usage(2);
247
4330d04e 248if ($use_same_auth) {
249 $producer_dsn = $dsn;
250 $producer_db_user = $db_user;
251 $producer_db_password = $db_password;
252}
253
c1addbe9 254$from = 'DBI' if !defined $from && defined $dsn;
ec42851e 255my @files = @ARGV; # source files
b79348f4 256unless ( @files ) {
d4becbfd 257 if ( defined($from) && $from eq 'DBI' ) {
b79348f4 258 @files = ('!');
259 }
260 else {
261 @files = ('-');
262 }
263}
16dc9970 264
265pod2usage(1) if $help;
d529894e 266
4ae3a779 267if ( $show_version ) {
268 print "SQL::Translator v", $SQL::Translator::VERSION, "\n";
269 exit(0);
270}
271
44598b94 272my $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,
216af0c9 278 quote_table_names => $quote_table_names || 1,
279 quote_field_names => $quote_field_names || 1,
44598b94 280 validate => $validate || 0,
281 parser_args => {
032bd3c7 282 trim_fields => $no_trim ? 0 : 1,
283 scan_fields => $no_scan ? 0 : 1,
64ad4e66 284 field_separator => $field_separator,
285 record_separator => $record_separator,
e107f4d2 286 dsn => $dsn,
287 db_user => $db_user,
288 db_password => $db_password,
b7ea3cc2 289 mysql_parser_version => $mysql_parser_version,
7d89539d 290 skip => $skip,
f7f81963 291 ignore_opts => $ignore_opts,
5cb879e2 292 },
293 producer_args => {
294 imap_file => $imap_file,
295 imap_url => $imap_url,
1ea530d4 296 pretty => $pretty,
8ac2c35e 297 ttfile => $template,
3658d749 298 tt_vars => \%tt_vars,
d955ed82 299 tt_conf => \%tt_conf,
44598b94 300 title => $title,
4330d04e 301 dsn => $producer_dsn,
302 db_user => $producer_db_user,
303 db_password => $producer_db_password,
e8aa1b67 304 skip => $skip,
305 skiplike => $skiplike,
306 add_truncate => $add_truncate,
af2301d7 307 add_prefix => $add_prefix,
308 prefix => $prefix,
309 indent => $indent,
310 newlines => $newlines,
5342f5c1 311 postgres_version => $postgres_version,
ca1f9923 312 mysql_version => $mysql_version,
4330d04e 313 package_name => $package_name,
5cb879e2 314 },
d529894e 315);
316
317if ( $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 }
af2301d7 326
d529894e 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
af2301d7 333pod2usage( msg => 'Please supply "from" and "to" arguments' )
515a6a58 334 unless $from && $to;
d529894e 335
783908a1 336$translator->parser($from);
337$translator->producer($to);
338
339for my $file (@files) {
b79348f4 340 my @args =
341 ($file eq '-') ? (data => \*STDIN) :
342 ($file eq '!') ? (data => '') :
343 (file => $file);
344
2cd6675e 345 my $output = $translator->translate(@args) or die
49e1eb70 346 "Error: " . $translator->error;
b79348f4 347
49e1eb70 348 print $output;
783908a1 349}
16dc9970 350
49e1eb70 351# ----------------------------------------------------
16dc9970 352# It is not all books that are as dull as their readers.
353# Henry David Thoreau
49e1eb70 354# ----------------------------------------------------
16dc9970 355
44598b94 356=pod
96844cae 357
16dc9970 358=head1 AUTHOR
359
9a64caf3 360Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
515a6a58 361darren chamberlain E<lt>darren@cpan.orgE<gt>.
16dc9970 362
363=head1 SEE ALSO
364
515a6a58 365SQL::Translator, L<http://sqlfairy.sourceforge.net>.
16dc9970 366
367=cut