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