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