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