Commit | Line | Data |
7a8e1f51 |
1 | #!/usr/bin/perl -w |
e107f4d2 |
2 | # vim: set ft=perl: |
16dc9970 |
3 | |
49e1eb70 |
4 | # ------------------------------------------------------------------- |
d4becbfd |
5 | # $Id: sqlt,v 1.11 2004-02-03 13:27:21 dlc Exp $ |
49e1eb70 |
6 | # ------------------------------------------------------------------- |
44598b94 |
7 | # Copyright (C) 2002 Ken Y. Clark <kclar@cpan.org>, |
783908a1 |
8 | # darren chamberlain <darren@cpan.org> |
9 | # |
10 | # This program is free software; you can redistribute it and/or |
11 | # modify it under the terms of the GNU General Public License as |
12 | # published by the Free Software Foundation; version 2. |
13 | # |
14 | # This program is distributed in the hope that it will be useful, but |
15 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | # General Public License for more details. |
18 | # |
19 | # You should have received a copy of the GNU General Public License |
20 | # along with this program; if not, write to the Free Software |
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
22 | # 02111-1307 USA |
23 | # ------------------------------------------------------------------- |
16dc9970 |
24 | |
44598b94 |
25 | =head1 NAME |
26 | |
27 | sqlt - convert SQL schema using SQL::Translator |
28 | |
29 | =head1 SYNOPSIS |
30 | |
31 | For help: |
32 | |
33 | sqlt -h|--help |
34 | |
35 | For a list of all parsers and producers: |
36 | |
37 | sqlt -l|--list |
38 | |
39 | To translate a schema: |
40 | |
41 | sqlt -f|--from|--parser MySQL |
42 | -t|--to|--producer Oracle |
43 | [options] |
44 | file [file2 ...] |
45 | |
46 | General Options: |
47 | |
48 | -d|--debug Print debug info |
49 | -v|--validate Validate the schema |
50 | --trace Print parser trace info |
51 | --show-warnings Print warnings to STDERR |
52 | |
e107f4d2 |
53 | DBI Parser Options: |
54 | |
55 | --dsn DSN for connecting to database |
56 | --db-user Database user |
57 | --db-password Database password |
58 | |
44598b94 |
59 | xSV Parser Options: |
60 | |
61 | --fs The field separator |
62 | --rs The record separator |
63 | --no-trim Don't trim whitespace on fields |
64 | --no-scan Don't scan fields for data types and sizes |
65 | |
66 | DB Producer Options: |
67 | |
68 | --add-drop-table Add 'DROP TABLE' statements before creates |
69 | --no-comments Don't include comments in SQL output |
70 | |
71 | Diagram Producer Options: |
72 | |
73 | --imap-file Filename to put image map data |
74 | --imap-url URL to use for image map |
75 | |
76 | HTML/POD Producer Options: |
77 | |
1ae0e8cb |
78 | --pretty Use CGI::Pretty for the output |
44598b94 |
79 | --title Title of schema |
80 | |
81 | TTSchema Producer Options: |
82 | |
83 | --template The path to the template |
84 | |
85 | XML-SQLFairy Producer Options: |
86 | |
87 | --emit-empty-tags Print empty tags for attributes |
88 | --attrib-values Use attributes instead of tags for |
89 | values of the schema objects |
90 | |
91 | =head1 DESCRIPTION |
92 | |
93 | This script is part of the SQL Fairy project. It will try to convert |
94 | any source file for which it has a grammar into any format for which |
95 | it has a producer. |
96 | |
97 | If using "show-warnings," be sure to redirect STDERR to a separate file. |
98 | In bash, you could do this: |
99 | |
100 | $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \ |
101 | file.sql 1>out 2>err |
102 | |
103 | You can specify a parser or producer located in any module that Perl |
104 | knows about, allowing you to easily substitute your own. |
105 | |
106 | =cut |
107 | |
108 | # ------------------------------------------------------------------- |
109 | |
16dc9970 |
110 | use strict; |
111 | use Getopt::Long; |
112 | use Pod::Usage; |
113 | use SQL::Translator; |
49e1eb70 |
114 | |
16dc9970 |
115 | use vars qw( $VERSION ); |
d4becbfd |
116 | $VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/; |
64ad4e66 |
117 | |
118 | my $from; # the original database |
119 | my $to; # the destination database |
120 | my $help; # show POD and bail |
121 | my $stdin; # whether to read STDIN for create script |
122 | my $no_comments; # whether to put comments in out file |
123 | my $show_warnings; # whether to show warnings from SQL::Translator |
124 | my $add_drop_table; # whether to show warnings from SQL::Translator |
64ad4e66 |
125 | my $debug; # whether to print debug info |
126 | my $trace; # whether to print parser trace |
127 | my $list; # list all parsers and producers |
032bd3c7 |
128 | my $no_trim; # don't trim whitespace on xSV fields |
129 | my $no_scan; # don't scan xSV fields for data types and sizes |
64ad4e66 |
130 | my $field_separator; # for xSV files |
131 | my $record_separator; # for xSV files |
35d75351 |
132 | my $validate; # whether to validate the parsed document |
5cb879e2 |
133 | my $imap_file; # filename where to place image map coords |
134 | my $imap_url; # URL to use in making image map |
1ea530d4 |
135 | my $pretty; # use CGI::Pretty instead of CGI (HTML producer) |
8ac2c35e |
136 | my $template; # template to pass to TTSchema producer |
44598b94 |
137 | my $title; # title for HTML/POD producer |
138 | my $emit_empty_tags; # show empty XML tags |
139 | my $attrib_values; # use XML attributes instead of tags |
e107f4d2 |
140 | my $dsn; # DBI parser |
141 | my $db_user; # DBI parser |
142 | my $db_password; # DBI parser |
16dc9970 |
143 | |
16dc9970 |
144 | GetOptions( |
44598b94 |
145 | 'add-drop-table' => \$add_drop_table, |
146 | 'attrib-values' => \$attrib_values, |
147 | 'd|debug' => \$debug, |
cccff7c2 |
148 | 'emit-empty-tags' => \$emit_empty_tags, |
d529894e |
149 | 'f|from|parser:s' => \$from, |
44598b94 |
150 | 'fs:s' => \$field_separator, |
783908a1 |
151 | 'h|help' => \$help, |
44598b94 |
152 | 'imap-file:s' => \$imap_file, |
153 | 'imap-url:s' => \$imap_url, |
154 | 't|to|producer:s' => \$to, |
d529894e |
155 | 'l|list' => \$list, |
44598b94 |
156 | 'pretty!' => \$pretty, |
d529894e |
157 | 'no-comments' => \$no_comments, |
032bd3c7 |
158 | 'no-scan' => \$no_scan, |
44598b94 |
159 | 'no-trim' => \$no_trim, |
64ad4e66 |
160 | 'rs:s' => \$record_separator, |
44598b94 |
161 | 'show-warnings' => \$show_warnings, |
8ac2c35e |
162 | 'template:s' => \$template, |
44598b94 |
163 | 'title:s' => \$title, |
164 | 'trace' => \$trace, |
165 | 'v|validate' => \$validate, |
e107f4d2 |
166 | 'dsn:s' => \$dsn, |
167 | 'db-user:s' => \$db_user, |
168 | 'db-password:s' => \$db_password, |
16dc9970 |
169 | ) or pod2usage(2); |
170 | |
ec42851e |
171 | my @files = @ARGV; # source files |
b79348f4 |
172 | unless ( @files ) { |
d4becbfd |
173 | if ( defined($from) && $from eq 'DBI' ) { |
b79348f4 |
174 | @files = ('!'); |
175 | } |
176 | else { |
177 | @files = ('-'); |
178 | } |
179 | } |
16dc9970 |
180 | |
181 | pod2usage(1) if $help; |
d529894e |
182 | |
44598b94 |
183 | my $translator = SQL::Translator->new( |
184 | debug => $debug || 0, |
185 | trace => $trace || 0, |
186 | no_comments => $no_comments || 0, |
187 | show_warnings => $show_warnings || 0, |
188 | add_drop_table => $add_drop_table || 0, |
189 | validate => $validate || 0, |
190 | parser_args => { |
032bd3c7 |
191 | trim_fields => $no_trim ? 0 : 1, |
192 | scan_fields => $no_scan ? 0 : 1, |
64ad4e66 |
193 | field_separator => $field_separator, |
194 | record_separator => $record_separator, |
e107f4d2 |
195 | dsn => $dsn, |
196 | db_user => $db_user, |
197 | db_password => $db_password, |
5cb879e2 |
198 | }, |
199 | producer_args => { |
200 | imap_file => $imap_file, |
201 | imap_url => $imap_url, |
1ea530d4 |
202 | pretty => $pretty, |
8ac2c35e |
203 | ttfile => $template, |
44598b94 |
204 | title => $title, |
205 | emit_empty_tags => $emit_empty_tags, |
206 | attrib_values => $attrib_values, |
5cb879e2 |
207 | }, |
d529894e |
208 | ); |
209 | |
210 | if ( $list ) { |
211 | my @parsers = $translator->list_parsers; |
212 | my @producers = $translator->list_producers; |
213 | |
214 | for ( @parsers, @producers ) { |
215 | if ( $_ =~ m/.+::(\w+)\.pm/ ) { |
216 | $_ = $1; |
217 | } |
218 | } |
219 | |
220 | print "\nParsers:\n", map { "\t$_\n" } sort @parsers; |
221 | print "\nProducers:\n", map { "\t$_\n" } sort @producers; |
222 | print "\n"; |
223 | exit(0); |
224 | } |
225 | |
515a6a58 |
226 | pod2usage( msg => 'Please supply "from" and "to" arguments' ) |
227 | unless $from && $to; |
d529894e |
228 | |
783908a1 |
229 | $translator->parser($from); |
230 | $translator->producer($to); |
231 | |
232 | for my $file (@files) { |
b79348f4 |
233 | my @args = |
234 | ($file eq '-') ? (data => \*STDIN) : |
235 | ($file eq '!') ? (data => '') : |
236 | (file => $file); |
237 | |
2cd6675e |
238 | my $output = $translator->translate(@args) or die |
49e1eb70 |
239 | "Error: " . $translator->error; |
b79348f4 |
240 | |
49e1eb70 |
241 | print $output; |
783908a1 |
242 | } |
16dc9970 |
243 | |
49e1eb70 |
244 | # ---------------------------------------------------- |
16dc9970 |
245 | # It is not all books that are as dull as their readers. |
246 | # Henry David Thoreau |
49e1eb70 |
247 | # ---------------------------------------------------- |
16dc9970 |
248 | |
44598b94 |
249 | =pod |
96844cae |
250 | |
16dc9970 |
251 | =head1 AUTHOR |
252 | |
515a6a58 |
253 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
254 | darren chamberlain E<lt>darren@cpan.orgE<gt>. |
16dc9970 |
255 | |
256 | =head1 SEE ALSO |
257 | |
515a6a58 |
258 | SQL::Translator, L<http://sqlfairy.sourceforge.net>. |
16dc9970 |
259 | |
260 | =cut |