Updated an example to make it happier.
[dbsrgits/SQL-Translator.git] / bin / sql_translator.pl
CommitLineData
7a8e1f51 1#!/usr/bin/perl -w
16dc9970 2
49e1eb70 3# -------------------------------------------------------------------
d529894e 4# $Id: sql_translator.pl,v 1.5 2002-11-22 03:03:40 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 );
d529894e 32$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
16dc9970 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
d529894e 39my $xlate; # user overrides for field translation
40my $debug; # whether to print debug info
41my $trace; # whether to print parser trace
42my $list; # list all parsers and producers
16dc9970 43
44#
45# Get options, explain how to use the script if necessary.
46#
47GetOptions(
d529894e 48 'f|from|parser:s' => \$from,
49 't|to|producer:s' => \$to,
783908a1 50 'h|help' => \$help,
d529894e 51 'l|list' => \$list,
52 'd|debug' => \$debug,
53 'trace' => \$trace,
54 'no-comments' => \$no_comments,
55 'xlate=s' => \$xlate,
16dc9970 56) or pod2usage(2);
57
d529894e 58my @files = @ARGV; # the create script(s) for the original db
16dc9970 59
60pod2usage(1) if $help;
d529894e 61
62if ( $xlate ) {
63 my @fields = split /,/, $xlate;
64 $xlate = {};
65 for my $field ( @fields ) {
66 my ( $from, $to ) = split(/\//, $field);
67 $xlate->{$from} = $to;
68 }
69}
16dc9970 70
71#
72# If everything is OK, translate file(s).
73#
d529894e 74my $translator = SQL::Translator->new(
75 xlate => $xlate || {},
76 debug => $debug,
77 trace => $trace,
78 no_comments => $no_comments,
79);
80
81if ( $list ) {
82 my @parsers = $translator->list_parsers;
83 my @producers = $translator->list_producers;
84
85 for ( @parsers, @producers ) {
86 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
87 $_ = $1;
88 }
89 }
90
91 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
92 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
93 print "\n";
94 exit(0);
95}
96
97pod2usage(2) unless $from && $to && @files;
98
783908a1 99$translator->parser($from);
100$translator->producer($to);
101
102for my $file (@files) {
49e1eb70 103 my $output = $translator->translate( $file ) or die
104 "Error: " . $translator->error;
105 print $output;
783908a1 106}
16dc9970 107
49e1eb70 108# ----------------------------------------------------
16dc9970 109# It is not all books that are as dull as their readers.
110# Henry David Thoreau
49e1eb70 111# ----------------------------------------------------
16dc9970 112
113=head1 NAME
114
49e1eb70 115sql_translator.pl - convert an SQL database schema
16dc9970 116
117=head1 SYNOPSIS
118
d529894e 119For help:
120
16dc9970 121 ./sql_translator.pl -h|--help
122
d529894e 123For a list of all parsers and producers:
124
125 ./sql_translator.pl -l|--list
126
127To translate a schema:
128
129 ./sql_translator.pl
130 -f|--from|--parser MySQL
131 -t|--to|--producer Oracle
132 [options]
133 file
16dc9970 134
135 Options:
136
d529894e 137 -d|--debug Print debug info
138 --trace Print parser trace info
139 --no-comments Don't include comments in SQL output
140 --xlate=foo/bar,baz/blech Overrides for field translation
16dc9970 141
142=head1 DESCRIPTION
143
d529894e 144This script is part of the SQL Fairy project
145(http://sqlfairy.sourceforge.net/). It will try to convert any
146database syntax for which it has a grammar into some other format it
147knows about.
16dc9970 148
149=head1 AUTHOR
150
d529894e 151Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
16dc9970 152
153=head1 SEE ALSO
154
d529894e 155SQL::Translator.
16dc9970 156
157=cut