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