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