Added small transform to turn "XML-SQLFairy" into "XML::SQLFairy" so that
[dbsrgits/SQL-Translator.git] / bin / sql_translator.pl
CommitLineData
7a8e1f51 1#!/usr/bin/perl -w
16dc9970 2
49e1eb70 3# -------------------------------------------------------------------
1ea530d4 4# $Id: sql_translator.pl,v 1.12 2003-08-20 13:50:46 dlc 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 );
1ea530d4 32$VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
64ad4e66 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
64ad4e66 41my $debug; # whether to print debug info
42my $trace; # whether to print parser trace
43my $list; # list all parsers and producers
032bd3c7 44my $no_trim; # don't trim whitespace on xSV fields
45my $no_scan; # don't scan xSV fields for data types and sizes
64ad4e66 46my $field_separator; # for xSV files
47my $record_separator; # for xSV files
35d75351 48my $validate; # whether to validate the parsed document
5cb879e2 49my $imap_file; # filename where to place image map coords
50my $imap_url; # URL to use in making image map
1ea530d4 51my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
16dc9970 52
53#
54# Get options, explain how to use the script if necessary.
55#
56GetOptions(
d529894e 57 'f|from|parser:s' => \$from,
58 't|to|producer:s' => \$to,
783908a1 59 'h|help' => \$help,
d529894e 60 'l|list' => \$list,
61 'd|debug' => \$debug,
62 'trace' => \$trace,
63 'no-comments' => \$no_comments,
96844cae 64 'show-warnings' => \$show_warnings,
65 'add-drop-table' => \$add_drop_table,
35d75351 66 'v|validate' => \$validate,
032bd3c7 67 'no-trim' => \$no_trim,
68 'no-scan' => \$no_scan,
64ad4e66 69 'fs:s' => \$field_separator,
70 'rs:s' => \$record_separator,
5cb879e2 71 'imap-file:s' => \$imap_file,
72 'imap-url:s' => \$imap_url,
1ea530d4 73 'pretty!' => \$pretty,
16dc9970 74) or pod2usage(2);
75
d529894e 76my @files = @ARGV; # the create script(s) for the original db
16dc9970 77
78pod2usage(1) if $help;
d529894e 79
16dc9970 80#
81# If everything is OK, translate file(s).
82#
64ad4e66 83my $translator = SQL::Translator->new(
64ad4e66 84 debug => $debug || 0,
85 trace => $trace || 0,
86 no_comments => $no_comments || 0,
87 show_warnings => $show_warnings || 0,
88 add_drop_table => $add_drop_table || 0,
35d75351 89 validate => $validate || 0,
64ad4e66 90 parser_args => {
032bd3c7 91 trim_fields => $no_trim ? 0 : 1,
92 scan_fields => $no_scan ? 0 : 1,
64ad4e66 93 field_separator => $field_separator,
94 record_separator => $record_separator,
5cb879e2 95 },
96 producer_args => {
97 imap_file => $imap_file,
98 imap_url => $imap_url,
1ea530d4 99 pretty => $pretty,
5cb879e2 100 },
d529894e 101);
102
103if ( $list ) {
104 my @parsers = $translator->list_parsers;
105 my @producers = $translator->list_producers;
106
107 for ( @parsers, @producers ) {
108 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
109 $_ = $1;
110 }
111 }
112
113 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
114 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
115 print "\n";
116 exit(0);
117}
118
119pod2usage(2) unless $from && $to && @files;
120
783908a1 121$translator->parser($from);
122$translator->producer($to);
123
124for my $file (@files) {
1ea530d4 125 my $output = $translator->translate(file => $file) or die
49e1eb70 126 "Error: " . $translator->error;
127 print $output;
783908a1 128}
16dc9970 129
49e1eb70 130# ----------------------------------------------------
16dc9970 131# It is not all books that are as dull as their readers.
132# Henry David Thoreau
49e1eb70 133# ----------------------------------------------------
16dc9970 134
135=head1 NAME
136
49e1eb70 137sql_translator.pl - convert an SQL database schema
16dc9970 138
139=head1 SYNOPSIS
140
d529894e 141For help:
142
16dc9970 143 ./sql_translator.pl -h|--help
144
d529894e 145For a list of all parsers and producers:
146
147 ./sql_translator.pl -l|--list
148
149To translate a schema:
150
151 ./sql_translator.pl
152 -f|--from|--parser MySQL
153 -t|--to|--producer Oracle
154 [options]
155 file
16dc9970 156
157 Options:
158
64ad4e66 159 -d|--debug Print debug info
35d75351 160 -v|--validate Validate the schema
64ad4e66 161 --trace Print parser trace info
162 --no-comments Don't include comments in SQL output
163 --show-warnings Print to STDERR warnings of conflicts, etc.
164 --add-drop-table Add 'drop table' statements before creates
165
166 xSV Options:
167
64ad4e66 168 --fs The field separator
169 --rs The record separator
032bd3c7 170 --no-trim Don't trim whitespace on fields
171 --no-scan Don't scan fields for data types and sizes
172
5cb879e2 173 Diagram Options:
174
175 --imap-file Filename to put image map data
176 --imap-url URL to use for image map
177
16dc9970 178=head1 DESCRIPTION
179
d529894e 180This script is part of the SQL Fairy project
181(http://sqlfairy.sourceforge.net/). It will try to convert any
182database syntax for which it has a grammar into some other format it
183knows about.
16dc9970 184
96844cae 185If using "show-warnings," be sure to redirect STDERR to a separate file.
186In bash, you could do this:
187
188 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings file.sql \
189 1>out 2>err
190
16dc9970 191=head1 AUTHOR
192
d529894e 193Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
16dc9970 194
195=head1 SEE ALSO
196
d529894e 197SQL::Translator.
16dc9970 198
199=cut