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