Typo fix.
[dbsrgits/SQL-Translator.git] / bin / sqlt
1 #!/usr/bin/perl -w
2
3 # -------------------------------------------------------------------
4 # $Id: sqlt,v 1.3 2003-09-03 14:52:50 dlc Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kclar@cpan.org>,
7 #                    darren chamberlain <darren@cpan.org>
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     --trace            Print parser trace info
50     --show-warnings    Print warnings to STDERR
51
52   xSV Parser Options:
53
54     --fs               The field separator
55     --rs               The record separator
56     --no-trim          Don't trim whitespace on fields 
57     --no-scan          Don't scan fields for data types and sizes 
58
59   DB Producer Options:
60
61     --add-drop-table   Add 'DROP TABLE' statements before creates
62     --no-comments      Don't include comments in SQL output
63
64   Diagram Producer Options:
65
66     --imap-file        Filename to put image map data
67     --imap-url         URL to use for image map
68
69   HTML/POD Producer Options:
70
71     --pretty           Use CGI::Pretty for the output
72     --title            Title of schema
73
74   TTSchema Producer Options:
75
76     --template         The path to the template
77
78   XML-SQLFairy Producer Options:
79
80     --emit-empty-tags  Print empty tags for attributes
81     --attrib-values    Use attributes instead of tags for 
82                        values of the schema objects
83
84 =head1 DESCRIPTION
85
86 This script is part of the SQL Fairy project.  It will try to convert
87 any source file for which it has a grammar into any format for which
88 it has a producer.
89
90 If using "show-warnings," be sure to redirect STDERR to a separate file.  
91 In bash, you could do this:
92
93     $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
94        file.sql 1>out 2>err
95
96 You can specify a parser or producer located in any module that Perl
97 knows about, allowing you to easily substitute your own.
98
99 =cut
100
101 # -------------------------------------------------------------------
102
103 use strict;
104 use Getopt::Long;
105 use Pod::Usage;
106 use SQL::Translator;
107
108 use vars qw( $VERSION );
109 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
110
111 my $from;             # the original database
112 my $to;               # the destination database 
113 my $help;             # show POD and bail
114 my $stdin;            # whether to read STDIN for create script
115 my $no_comments;      # whether to put comments in out file
116 my $show_warnings;    # whether to show warnings from SQL::Translator
117 my $add_drop_table;   # whether to show warnings from SQL::Translator
118 my $debug;            # whether to print debug info
119 my $trace;            # whether to print parser trace
120 my $list;             # list all parsers and producers
121 my $no_trim;          # don't trim whitespace on xSV fields
122 my $no_scan;          # don't scan xSV fields for data types and sizes
123 my $field_separator;  # for xSV files
124 my $record_separator; # for xSV files
125 my $validate;         # whether to validate the parsed document
126 my $imap_file;        # filename where to place image map coords
127 my $imap_url;         # URL to use in making image map
128 my $pretty;           # use CGI::Pretty instead of CGI (HTML producer)
129 my $template;         # template to pass to TTSchema producer
130 my $title;            # title for HTML/POD producer
131 my $emit_empty_tags;  # show empty XML tags
132 my $attrib_values;    # use XML attributes instead of tags
133
134 GetOptions(
135     'add-drop-table'  => \$add_drop_table,
136     'attrib-values'   => \$attrib_values,
137     'd|debug'         => \$debug,
138     'emit_empty_tags' => \$emit_empty_tags,
139     'f|from|parser:s' => \$from,
140     'fs:s'            => \$field_separator,
141     'h|help'          => \$help,
142     'imap-file:s'     => \$imap_file,
143     'imap-url:s'      => \$imap_url,
144     't|to|producer:s' => \$to,
145     'l|list'          => \$list,
146     'pretty!'         => \$pretty,
147     'no-comments'     => \$no_comments,
148     'no-scan'         => \$no_scan,
149     'no-trim'         => \$no_trim,
150     'rs:s'            => \$record_separator,
151     'show-warnings'   => \$show_warnings,
152     'template:s'      => \$template,
153     'title:s'         => \$title,
154     'trace'           => \$trace,
155     'v|validate'      => \$validate,
156 ) or pod2usage(2);
157
158 my @files = @ARGV; # source files
159
160 pod2usage(1) if $help;
161
162 my $translator           =  SQL::Translator->new( 
163     debug                => $debug          ||  0,
164     trace                => $trace          ||  0,
165     no_comments          => $no_comments    ||  0,
166     show_warnings        => $show_warnings  ||  0,
167     add_drop_table       => $add_drop_table ||  0,
168     validate             => $validate       ||  0,
169     parser_args          => {
170         trim_fields      => $no_trim ? 0 : 1,
171         scan_fields      => $no_scan ? 0 : 1,
172         field_separator  => $field_separator,
173         record_separator => $record_separator,
174     },
175     producer_args   => {
176         imap_file        => $imap_file,
177         imap_url         => $imap_url,
178         pretty           => $pretty,
179         ttfile           => $template,
180         title            => $title,
181         emit_empty_tags  => $emit_empty_tags,
182         attrib_values    => $attrib_values,
183     },
184 );
185
186 if ( $list ) {
187     my @parsers   = $translator->list_parsers;
188     my @producers = $translator->list_producers;
189
190     for ( @parsers, @producers ) {
191         if ( $_ =~ m/.+::(\w+)\.pm/ ) {
192             $_ = $1;
193         }
194     }
195     
196     print "\nParsers:\n",   map { "\t$_\n" } sort @parsers;
197     print "\nProducers:\n", map { "\t$_\n" } sort @producers;
198     print "\n";
199     exit(0);
200 }
201
202 pod2usage(2) unless $from && $to && @files;
203
204 $translator->parser($from);
205 $translator->producer($to);
206
207 for my $file (@files) {
208     my $output = $translator->translate(file => $file) or die
209         "Error: " . $translator->error;
210     print $output;
211 }
212
213 # ----------------------------------------------------
214 # It is not all books that are as dull as their readers.
215 # Henry David Thoreau
216 # ----------------------------------------------------
217
218 =pod
219
220 =head1 AUTHOR
221
222 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
223
224 =head1 SEE ALSO
225
226 perl, SQL::Translator, Parse::RecDescent, 
227 L<http://sqlfairy.sourceforge.net>.
228
229 =cut