Changed getting of version from main module, added exe file.
[dbsrgits/SQL-Translator.git] / bin / sql_translator.pl
1 #!/usr/bin/perl -w
2
3 # -------------------------------------------------------------------
4 # $Id: sql_translator.pl,v 1.9 2003-05-12 14:48:43 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
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 use strict;
25 use Getopt::Long;
26 use Pod::Usage;
27 use SQL::Translator;
28
29 use Data::Dumper;
30
31 use vars qw( $VERSION );
32 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
33
34 my $from;             # the original database
35 my $to;               # the destination database 
36 my $help;             # show POD and bail
37 my $stdin;            # whether to read STDIN for create script
38 my $no_comments;      # whether to put comments in out file
39 my $show_warnings;    # whether to show warnings from SQL::Translator
40 my $add_drop_table;   # whether to show warnings from SQL::Translator
41 my $xlate;            # user overrides for field translation
42 my $debug;            # whether to print debug info
43 my $trace;            # whether to print parser trace
44 my $list;             # list all parsers and producers
45 my $no_trim;          # don't trim whitespace on xSV fields
46 my $no_scan;          # don't scan xSV fields for data types and sizes
47 my $field_separator;  # for xSV files
48 my $record_separator; # for xSV files
49
50 #
51 # Get options, explain how to use the script if necessary.
52 #
53 GetOptions(
54     'f|from|parser:s' => \$from,
55     't|to|producer:s' => \$to,
56     'h|help'          => \$help,
57     'l|list'          => \$list,
58     'd|debug'         => \$debug,
59     'trace'           => \$trace,
60     'no-comments'     => \$no_comments,
61     'show-warnings'   => \$show_warnings,
62     'add-drop-table'  => \$add_drop_table,
63     'no-trim'         => \$no_trim,
64     'no-scan'         => \$no_scan,
65     'fs:s'            => \$field_separator,
66     'rs:s'            => \$record_separator,
67 ) or pod2usage(2);
68
69 my @files = @ARGV; # the create script(s) for the original db
70
71 pod2usage(1) if $help;
72
73 if ( $xlate ) {
74     my @fields = split /,/, $xlate;
75     $xlate     = {}; 
76     for my $field ( @fields ) {
77         my ( $from, $to ) = split(/\//, $field);
78         $xlate->{$from} = $to;
79     }
80 }
81
82 #
83 # If everything is OK, translate file(s).
84 #
85 my $translator      =  SQL::Translator->new( 
86     xlate           => $xlate          || {},
87     debug           => $debug          ||  0,
88     trace           => $trace          ||  0,
89     no_comments     => $no_comments    ||  0,
90     show_warnings   => $show_warnings  ||  0,
91     add_drop_table  => $add_drop_table ||  0,
92     parser_args     => {
93         trim_fields      => $no_trim ? 0 : 1,
94         scan_fields      => $no_scan ? 0 : 1,
95         field_separator  => $field_separator,
96         record_separator => $record_separator,
97     }
98 );
99
100 if ( $list ) {
101     my @parsers   = $translator->list_parsers;
102     my @producers = $translator->list_producers;
103
104     for ( @parsers, @producers ) {
105         if ( $_ =~ m/.+::(\w+)\.pm/ ) {
106             $_ = $1;
107         }
108     }
109     
110     print "\nParsers:\n",   map { "\t$_\n" } sort @parsers;
111     print "\nProducers:\n", map { "\t$_\n" } sort @producers;
112     print "\n";
113     exit(0);
114 }
115
116 pod2usage(2) unless $from && $to && @files;
117
118 $translator->parser($from);
119 $translator->producer($to);
120
121 for my $file (@files) {
122     my $output = $translator->translate( $file ) or die
123         "Error: " . $translator->error;
124     print $output;
125 }
126
127 # ----------------------------------------------------
128 # It is not all books that are as dull as their readers.
129 # Henry David Thoreau
130 # ----------------------------------------------------
131
132 =head1 NAME
133
134 sql_translator.pl - convert an SQL database schema
135
136 =head1 SYNOPSIS
137
138 For help:
139
140   ./sql_translator.pl -h|--help
141
142 For a list of all parsers and producers: 
143
144   ./sql_translator.pl -l|--list
145
146 To translate a schema:
147
148   ./sql_translator.pl 
149         -f|--from|--parser MySQL 
150         -t|--to|--producer Oracle 
151         [options] 
152         file
153
154   Options:
155
156     -d|--debug            Print debug info
157     --trace               Print parser trace info
158     --no-comments         Don't include comments in SQL output
159     --show-warnings       Print to STDERR warnings of conflicts, etc.
160     --add-drop-table      Add 'drop table' statements before creates
161
162   xSV Options:
163
164     --fs                  The field separator
165     --rs                  The record separator
166     --no-trim             Don't trim whitespace on fields 
167     --no-scan             Don't scan fields for data types and sizes 
168
169 =head1 DESCRIPTION
170
171 This script is part of the SQL Fairy project
172 (http://sqlfairy.sourceforge.net/).  It will try to convert any
173 database syntax for which it has a grammar into some other format it
174 knows about.
175
176 If using "show-warnings," be sure to redirect STDERR to a separate file.  
177 In bash, you could do this:
178
179     $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings file.sql \
180        1>out 2>err
181
182 =head1 AUTHOR
183
184 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
185
186 =head1 SEE ALSO
187
188 SQL::Translator.
189
190 =cut