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