Fixed broken --emit-empty-tags option.
[dbsrgits/SQL-Translator.git] / bin / sqlt
1 #!/usr/bin/perl -w
2 # vim: set ft=perl:
3
4 # -------------------------------------------------------------------
5 # $Id: sqlt,v 1.10 2003-10-19 17:01:25 grommit Exp $
6 # -------------------------------------------------------------------
7 # Copyright (C) 2002 Ken Y. Clark <kclar@cpan.org>,
8 #                    darren chamberlain <darren@cpan.org>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # 02111-1307  USA
23 # -------------------------------------------------------------------
24
25 =head1 NAME
26
27 sqlt - convert SQL schema using SQL::Translator
28
29 =head1 SYNOPSIS
30
31 For help:
32
33   sqlt -h|--help
34
35 For a list of all parsers and producers: 
36
37   sqlt -l|--list
38
39 To translate a schema:
40
41   sqlt -f|--from|--parser MySQL 
42        -t|--to|--producer Oracle 
43        [options] 
44        file [file2 ...]
45
46   General Options:
47
48     -d|--debug         Print debug info
49     -v|--validate      Validate the schema
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     --db-user          Database user
57     --db-password      Database password              
58
59   xSV Parser Options:
60
61     --fs               The field separator
62     --rs               The record separator
63     --no-trim          Don't trim whitespace on fields 
64     --no-scan          Don't scan fields for data types and sizes 
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
76   HTML/POD Producer Options:
77
78     --pretty           Use CGI::Pretty for the output
79     --title            Title of schema
80
81   TTSchema Producer Options:
82
83     --template         The path to the template
84
85   XML-SQLFairy Producer Options:
86
87     --emit-empty-tags  Print empty tags for attributes
88     --attrib-values    Use attributes instead of tags for 
89                        values of the schema objects
90
91 =head1 DESCRIPTION
92
93 This script is part of the SQL Fairy project.  It will try to convert
94 any source file for which it has a grammar into any format for which
95 it has a producer.
96
97 If using "show-warnings," be sure to redirect STDERR to a separate file.  
98 In bash, you could do this:
99
100     $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
101        file.sql 1>out 2>err
102
103 You can specify a parser or producer located in any module that Perl
104 knows about, allowing you to easily substitute your own.
105
106 =cut
107
108 # -------------------------------------------------------------------
109
110 use strict;
111 use Getopt::Long;
112 use Pod::Usage;
113 use SQL::Translator;
114
115 use vars qw( $VERSION );
116 $VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
117
118 my $from;             # the original database
119 my $to;               # the destination database 
120 my $help;             # show POD and bail
121 my $stdin;            # whether to read STDIN for create script
122 my $no_comments;      # whether to put comments in out file
123 my $show_warnings;    # whether to show warnings from SQL::Translator
124 my $add_drop_table;   # whether to show warnings from SQL::Translator
125 my $debug;            # whether to print debug info
126 my $trace;            # whether to print parser trace
127 my $list;             # list all parsers and producers
128 my $no_trim;          # don't trim whitespace on xSV fields
129 my $no_scan;          # don't scan xSV fields for data types and sizes
130 my $field_separator;  # for xSV files
131 my $record_separator; # for xSV files
132 my $validate;         # whether to validate the parsed document
133 my $imap_file;        # filename where to place image map coords
134 my $imap_url;         # URL to use in making image map
135 my $pretty;           # use CGI::Pretty instead of CGI (HTML producer)
136 my $template;         # template to pass to TTSchema producer
137 my $title;            # title for HTML/POD producer
138 my $emit_empty_tags;  # show empty XML tags
139 my $attrib_values;    # use XML attributes instead of tags
140 my $dsn;              # DBI parser 
141 my $db_user;          # DBI parser 
142 my $db_password;      # DBI parser 
143
144 GetOptions(
145     'add-drop-table'  => \$add_drop_table,
146     'attrib-values'   => \$attrib_values,
147     'd|debug'         => \$debug,
148     'emit-empty-tags' => \$emit_empty_tags,
149     'f|from|parser:s' => \$from,
150     'fs:s'            => \$field_separator,
151     'h|help'          => \$help,
152     'imap-file:s'     => \$imap_file,
153     'imap-url:s'      => \$imap_url,
154     't|to|producer:s' => \$to,
155     'l|list'          => \$list,
156     'pretty!'         => \$pretty,
157     'no-comments'     => \$no_comments,
158     'no-scan'         => \$no_scan,
159     'no-trim'         => \$no_trim,
160     'rs:s'            => \$record_separator,
161     'show-warnings'   => \$show_warnings,
162     'template:s'      => \$template,
163     'title:s'         => \$title,
164     'trace'           => \$trace,
165     'v|validate'      => \$validate,
166     'dsn:s'           => \$dsn,
167     'db-user:s'       => \$db_user,
168     'db-password:s'   => \$db_password,
169 ) or pod2usage(2);
170
171 my @files = @ARGV; # source files
172 unless ( @files ) {
173     if ( $from eq 'DBI' ) {
174         @files = ('!');
175     }
176     else {
177         @files = ('-');
178     }
179 }
180
181 pod2usage(1) if $help;
182
183 my $translator           =  SQL::Translator->new( 
184     debug                => $debug          ||  0,
185     trace                => $trace          ||  0,
186     no_comments          => $no_comments    ||  0,
187     show_warnings        => $show_warnings  ||  0,
188     add_drop_table       => $add_drop_table ||  0,
189     validate             => $validate       ||  0,
190     parser_args          => {
191         trim_fields      => $no_trim ? 0 : 1,
192         scan_fields      => $no_scan ? 0 : 1,
193         field_separator  => $field_separator,
194         record_separator => $record_separator,
195         dsn              => $dsn,
196         db_user          => $db_user,
197         db_password      => $db_password,
198     },
199     producer_args   => {
200         imap_file        => $imap_file,
201         imap_url         => $imap_url,
202         pretty           => $pretty,
203         ttfile           => $template,
204         title            => $title,
205         emit_empty_tags  => $emit_empty_tags,
206         attrib_values    => $attrib_values,
207     },
208 );
209
210 if ( $list ) {
211     my @parsers   = $translator->list_parsers;
212     my @producers = $translator->list_producers;
213
214     for ( @parsers, @producers ) {
215         if ( $_ =~ m/.+::(\w+)\.pm/ ) {
216             $_ = $1;
217         }
218     }
219     
220     print "\nParsers:\n",   map { "\t$_\n" } sort @parsers;
221     print "\nProducers:\n", map { "\t$_\n" } sort @producers;
222     print "\n";
223     exit(0);
224 }
225
226 pod2usage( msg => 'Please supply "from" and "to" arguments' ) 
227     unless $from && $to;
228
229 $translator->parser($from);
230 $translator->producer($to);
231
232 for my $file (@files) {
233     my @args = 
234         ($file eq '-') ? (data => \*STDIN) :
235         ($file eq '!') ? (data => '') :
236         (file => $file);
237
238     my $output = $translator->translate(@args) or die
239         "Error: " . $translator->error;
240
241     print $output;
242 }
243
244 # ----------------------------------------------------
245 # It is not all books that are as dull as their readers.
246 # Henry David Thoreau
247 # ----------------------------------------------------
248
249 =pod
250
251 =head1 AUTHOR
252
253 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
254 darren chamberlain E<lt>darren@cpan.orgE<gt>.
255
256 =head1 SEE ALSO
257
258 SQL::Translator, L<http://sqlfairy.sourceforge.net>.
259
260 =cut