Commit | Line | Data |
7a8e1f51 |
1 | #!/usr/bin/perl -w |
e107f4d2 |
2 | # vim: set ft=perl: |
16dc9970 |
3 | |
49e1eb70 |
4 | # ------------------------------------------------------------------- |
b7ea3cc2 |
5 | # $Id: sqlt,v 1.22 2007-03-21 15:21:31 duality72 Exp $ |
49e1eb70 |
6 | # ------------------------------------------------------------------- |
daf4f623 |
7 | # Copyright (C) 2002-4 SQLFairy Authors |
783908a1 |
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 | |
44598b94 |
24 | =head1 NAME |
25 | |
26 | sqlt - convert SQL schema using SQL::Translator |
27 | |
28 | =head1 SYNOPSIS |
29 | |
30 | For help: |
31 | |
32 | sqlt -h|--help |
33 | |
af2301d7 |
34 | For a list of all parsers and producers: |
44598b94 |
35 | |
36 | sqlt -l|--list |
37 | |
38 | To translate a schema: |
39 | |
af2301d7 |
40 | sqlt -f|--from|--parser MySQL |
41 | -t|--to|--producer Oracle |
42 | [options] |
44598b94 |
43 | file [file2 ...] |
44 | |
45 | General Options: |
46 | |
47 | -d|--debug Print debug info |
48 | -v|--validate Validate the schema |
4ae3a779 |
49 | --version Show the version of SQL::Translator |
44598b94 |
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 |
4330d04e |
56 | (see also --use-same-auth below) |
e107f4d2 |
57 | --db-user Database user |
af2301d7 |
58 | --db-password Database password |
e107f4d2 |
59 | |
44598b94 |
60 | xSV Parser Options: |
61 | |
62 | --fs The field separator |
63 | --rs The record separator |
af2301d7 |
64 | --no-trim Don't trim whitespace on fields |
65 | --no-scan Don't scan fields for data types and sizes |
44598b94 |
66 | |
b7ea3cc2 |
67 | MySQL Parser Options: |
68 | |
69 | --mysql-parser-version Target MySQL parser version for dealing with |
70 | /*! comments; default = 30000 |
71 | |
4330d04e |
72 | General Producer Options |
73 | |
74 | --producer-db-user Database user for producer |
75 | --producer-db-pass Database password for producer |
76 | --producer-dsn DSN for producer |
77 | --use-same-auth Use these DSN, user, password for producer output |
78 | |
44598b94 |
79 | DB Producer Options: |
80 | |
81 | --add-drop-table Add 'DROP TABLE' statements before creates |
216af0c9 |
82 | --quote-table-names Quote all table names in statements |
83 | --quote-field-names Qjuote all field names in statements |
44598b94 |
84 | --no-comments Don't include comments in SQL output |
85 | |
86 | Diagram Producer Options: |
87 | |
88 | --imap-file Filename to put image map data |
89 | --imap-url URL to use for image map |
90 | |
e8aa1b67 |
91 | Dumper Producer Options: |
92 | |
93 | --skip Comma-separated list of tables to skip |
94 | --skiplike Regex for tables to skip |
e8aa1b67 |
95 | --add-truncate Add "TRUNCATE TABLE" statements for each table |
96 | |
44598b94 |
97 | HTML/POD Producer Options: |
98 | |
1ae0e8cb |
99 | --pretty Use CGI::Pretty for the output |
44598b94 |
100 | --title Title of schema |
101 | |
102 | TTSchema Producer Options: |
103 | |
d955ed82 |
104 | --template The path to the template |
105 | --tt-var var=value Pass extra variables to the template |
106 | --tt-conf option=value Pass extra config options to Template |
44598b94 |
107 | |
108 | XML-SQLFairy Producer Options: |
109 | |
af2301d7 |
110 | --add-prefix Use an explicit namespace prefix of 'sqlf:' |
111 | --prefix=<p> Use the namespace prefix given as argument. |
112 | --no-newlines Write the XML as a single line. |
113 | --indent=<n> Use <n> characters of whitespace to indent the XML. |
44598b94 |
114 | |
4330d04e |
115 | ClassDBI Producer Options: |
116 | |
117 | --package Base package name for Class::DBI modules. |
118 | |
44598b94 |
119 | =head1 DESCRIPTION |
120 | |
121 | This script is part of the SQL Fairy project. It will try to convert |
122 | any source file for which it has a grammar into any format for which |
123 | it has a producer. |
124 | |
af2301d7 |
125 | If using "show-warnings," be sure to redirect STDERR to a separate file. |
44598b94 |
126 | In bash, you could do this: |
127 | |
128 | $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \ |
129 | file.sql 1>out 2>err |
130 | |
131 | You can specify a parser or producer located in any module that Perl |
132 | knows about, allowing you to easily substitute your own. |
133 | |
134 | =cut |
135 | |
136 | # ------------------------------------------------------------------- |
137 | |
16dc9970 |
138 | use strict; |
139 | use Getopt::Long; |
140 | use Pod::Usage; |
141 | use SQL::Translator; |
49e1eb70 |
142 | |
16dc9970 |
143 | use vars qw( $VERSION ); |
b7ea3cc2 |
144 | $VERSION = sprintf "%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/; |
64ad4e66 |
145 | |
146 | my $from; # the original database |
af2301d7 |
147 | my $to; # the destination database |
64ad4e66 |
148 | my $help; # show POD and bail |
149 | my $stdin; # whether to read STDIN for create script |
150 | my $no_comments; # whether to put comments in out file |
151 | my $show_warnings; # whether to show warnings from SQL::Translator |
216af0c9 |
152 | my $add_drop_table; # whether to add "DROP table" statements |
153 | my $quote_table_names; # whether to quote table names |
154 | my $quote_field_names; # whether to quote field names |
64ad4e66 |
155 | my $debug; # whether to print debug info |
156 | my $trace; # whether to print parser trace |
157 | my $list; # list all parsers and producers |
032bd3c7 |
158 | my $no_trim; # don't trim whitespace on xSV fields |
159 | my $no_scan; # don't scan xSV fields for data types and sizes |
64ad4e66 |
160 | my $field_separator; # for xSV files |
161 | my $record_separator; # for xSV files |
35d75351 |
162 | my $validate; # whether to validate the parsed document |
5cb879e2 |
163 | my $imap_file; # filename where to place image map coords |
164 | my $imap_url; # URL to use in making image map |
1ea530d4 |
165 | my $pretty; # use CGI::Pretty instead of CGI (HTML producer) |
8ac2c35e |
166 | my $template; # template to pass to TTSchema producer |
3658d749 |
167 | my %tt_vars; # additional template vars to pass the TTSchema producer |
d955ed82 |
168 | my %tt_conf; # additional template conf to pass the TTSchema producer |
44598b94 |
169 | my $title; # title for HTML/POD producer |
af2301d7 |
170 | my $add_prefix; # Use explicit namespace prefix (XML producer) |
171 | my $prefix; # Set explicit namespace prefix (XML producer) |
172 | my $newlines; # Add newlines around tags (XML producer) |
173 | my $indent; # Number of indent chars for XML |
4330d04e |
174 | my $package_name; # Base class name for ClassDBI |
175 | my $use_same_auth =0; # producer uses same DSN, user, password as parser |
af2301d7 |
176 | my $dsn; # DBI parser |
177 | my $db_user; # DBI parser |
178 | my $db_password; # DBI parser |
4ae3a779 |
179 | my $show_version; # Show version and exit script |
af2301d7 |
180 | my $skip; |
181 | my $skiplike; |
4330d04e |
182 | my $producer_db_user; # DSN for producer (e.g. Dumper, ClassDBI) |
183 | my $producer_db_password; # db_pass " |
184 | my $producer_dsn; # db_user " |
e8aa1b67 |
185 | my $add_truncate; |
b7ea3cc2 |
186 | my $mysql_parser_version; # MySQL parser arg for /*! comments |
16dc9970 |
187 | |
16dc9970 |
188 | GetOptions( |
e8aa1b67 |
189 | 'add-drop-table' => \$add_drop_table, |
216af0c9 |
190 | 'quote_table_names' => \$quote_table_names, |
191 | 'quote_field_names' => \$quote_field_names, |
e8aa1b67 |
192 | 'd|debug' => \$debug, |
e8aa1b67 |
193 | 'f|from|parser:s' => \$from, |
194 | 'fs:s' => \$field_separator, |
195 | 'h|help' => \$help, |
196 | 'imap-file:s' => \$imap_file, |
197 | 'imap-url:s' => \$imap_url, |
198 | 't|to|producer:s' => \$to, |
199 | 'l|list' => \$list, |
200 | 'pretty!' => \$pretty, |
201 | 'no-comments' => \$no_comments, |
202 | 'no-scan' => \$no_scan, |
203 | 'no-trim' => \$no_trim, |
204 | 'rs:s' => \$record_separator, |
205 | 'show-warnings' => \$show_warnings, |
206 | 'template:s' => \$template, |
3658d749 |
207 | 'tt-var=s' => \%tt_vars, |
d955ed82 |
208 | 'tt-conf=s' => \%tt_conf, |
e8aa1b67 |
209 | 'title:s' => \$title, |
210 | 'trace' => \$trace, |
211 | 'v|validate' => \$validate, |
212 | 'dsn:s' => \$dsn, |
213 | 'db-user:s' => \$db_user, |
214 | 'db-password:s' => \$db_password, |
4330d04e |
215 | 'producer-dsn:s' => \$producer_dsn, |
216 | 'producer-db-user:s'=> \$producer_db_user, |
217 | 'producer-db-pass:s'=> \$producer_db_password, |
e8aa1b67 |
218 | 'skip:s' => \$skip, |
219 | 'skiplike:s' => \$skiplike, |
220 | 'add_truncate' => \$add_truncate, |
af2301d7 |
221 | 'add-prefix' => \$add_prefix, |
222 | 'prefix:s' => \$prefix, |
223 | 'indent:s' => \$indent, |
224 | 'newlines!' => \$newlines, |
4330d04e |
225 | 'package=s' => \$package_name, |
226 | 'use-same-auth' => \$use_same_auth, |
4ae3a779 |
227 | 'version' => \$show_version, |
b7ea3cc2 |
228 | 'mysql-parser-version=i' => \$mysql_parser_version, |
16dc9970 |
229 | ) or pod2usage(2); |
230 | |
4330d04e |
231 | if ($use_same_auth) { |
232 | $producer_dsn = $dsn; |
233 | $producer_db_user = $db_user; |
234 | $producer_db_password = $db_password; |
235 | } |
236 | |
c1addbe9 |
237 | $from = 'DBI' if !defined $from && defined $dsn; |
ec42851e |
238 | my @files = @ARGV; # source files |
b79348f4 |
239 | unless ( @files ) { |
d4becbfd |
240 | if ( defined($from) && $from eq 'DBI' ) { |
b79348f4 |
241 | @files = ('!'); |
242 | } |
243 | else { |
244 | @files = ('-'); |
245 | } |
246 | } |
16dc9970 |
247 | |
248 | pod2usage(1) if $help; |
d529894e |
249 | |
4ae3a779 |
250 | if ( $show_version ) { |
251 | print "SQL::Translator v", $SQL::Translator::VERSION, "\n"; |
252 | exit(0); |
253 | } |
254 | |
44598b94 |
255 | my $translator = SQL::Translator->new( |
256 | debug => $debug || 0, |
257 | trace => $trace || 0, |
258 | no_comments => $no_comments || 0, |
259 | show_warnings => $show_warnings || 0, |
260 | add_drop_table => $add_drop_table || 0, |
216af0c9 |
261 | quote_table_names => $quote_table_names || 1, |
262 | quote_field_names => $quote_field_names || 1, |
44598b94 |
263 | validate => $validate || 0, |
264 | parser_args => { |
032bd3c7 |
265 | trim_fields => $no_trim ? 0 : 1, |
266 | scan_fields => $no_scan ? 0 : 1, |
64ad4e66 |
267 | field_separator => $field_separator, |
268 | record_separator => $record_separator, |
e107f4d2 |
269 | dsn => $dsn, |
270 | db_user => $db_user, |
271 | db_password => $db_password, |
b7ea3cc2 |
272 | mysql_parser_version => $mysql_parser_version, |
5cb879e2 |
273 | }, |
274 | producer_args => { |
275 | imap_file => $imap_file, |
276 | imap_url => $imap_url, |
1ea530d4 |
277 | pretty => $pretty, |
8ac2c35e |
278 | ttfile => $template, |
3658d749 |
279 | tt_vars => \%tt_vars, |
d955ed82 |
280 | tt_conf => \%tt_conf, |
44598b94 |
281 | title => $title, |
4330d04e |
282 | dsn => $producer_dsn, |
283 | db_user => $producer_db_user, |
284 | db_password => $producer_db_password, |
e8aa1b67 |
285 | skip => $skip, |
286 | skiplike => $skiplike, |
287 | add_truncate => $add_truncate, |
af2301d7 |
288 | add_prefix => $add_prefix, |
289 | prefix => $prefix, |
290 | indent => $indent, |
291 | newlines => $newlines, |
4330d04e |
292 | package_name => $package_name, |
5cb879e2 |
293 | }, |
d529894e |
294 | ); |
295 | |
296 | if ( $list ) { |
297 | my @parsers = $translator->list_parsers; |
298 | my @producers = $translator->list_producers; |
299 | |
300 | for ( @parsers, @producers ) { |
301 | if ( $_ =~ m/.+::(\w+)\.pm/ ) { |
302 | $_ = $1; |
303 | } |
304 | } |
af2301d7 |
305 | |
d529894e |
306 | print "\nParsers:\n", map { "\t$_\n" } sort @parsers; |
307 | print "\nProducers:\n", map { "\t$_\n" } sort @producers; |
308 | print "\n"; |
309 | exit(0); |
310 | } |
311 | |
af2301d7 |
312 | pod2usage( msg => 'Please supply "from" and "to" arguments' ) |
515a6a58 |
313 | unless $from && $to; |
d529894e |
314 | |
783908a1 |
315 | $translator->parser($from); |
316 | $translator->producer($to); |
317 | |
318 | for my $file (@files) { |
b79348f4 |
319 | my @args = |
320 | ($file eq '-') ? (data => \*STDIN) : |
321 | ($file eq '!') ? (data => '') : |
322 | (file => $file); |
323 | |
2cd6675e |
324 | my $output = $translator->translate(@args) or die |
49e1eb70 |
325 | "Error: " . $translator->error; |
b79348f4 |
326 | |
49e1eb70 |
327 | print $output; |
783908a1 |
328 | } |
16dc9970 |
329 | |
49e1eb70 |
330 | # ---------------------------------------------------- |
16dc9970 |
331 | # It is not all books that are as dull as their readers. |
332 | # Henry David Thoreau |
49e1eb70 |
333 | # ---------------------------------------------------- |
16dc9970 |
334 | |
44598b94 |
335 | =pod |
96844cae |
336 | |
16dc9970 |
337 | =head1 AUTHOR |
338 | |
9a64caf3 |
339 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>, |
515a6a58 |
340 | darren chamberlain E<lt>darren@cpan.orgE<gt>. |
16dc9970 |
341 | |
342 | =head1 SEE ALSO |
343 | |
515a6a58 |
344 | SQL::Translator, L<http://sqlfairy.sourceforge.net>. |
16dc9970 |
345 | |
346 | =cut |