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