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