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