4 # -------------------------------------------------------------------
5 # $Id: sqlt,v 1.17 2004-11-26 00:34:09 grommit Exp $
6 # -------------------------------------------------------------------
7 # Copyright (C) 2002-4 SQLFairy Authors
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.
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.
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
22 # -------------------------------------------------------------------
26 sqlt - convert SQL schema using SQL::Translator
34 For a list of all parsers and producers:
38 To translate a schema:
40 sqlt -f|--from|--parser MySQL
41 -t|--to|--producer Oracle
47 -d|--debug Print debug info
48 -v|--validate Validate the schema
49 --version Show the version of SQL::Translator
50 --trace Print parser trace info
51 --show-warnings Print warnings to STDERR
55 --dsn DSN for connecting to database
56 --db-user Database user
57 --db-password Database password
61 --fs The field separator
62 --rs The record separator
63 --no-trim Don't trim whitespace on fields
64 --no-scan Don't scan fields for data types and sizes
68 --add-drop-table Add 'DROP TABLE' statements before creates
69 --no-comments Don't include comments in SQL output
71 Diagram Producer Options:
73 --imap-file Filename to put image map data
74 --imap-url URL to use for image map
76 Dumper Producer Options:
78 --skip Comma-separated list of tables to skip
79 --skiplike Regex for tables to skip
80 --dumper-db-user Database user for dumper script
81 --dumper-db-pass Database password for dumper script
82 --dumper-dsn DSN for dumper script
83 --add-truncate Add "TRUNCATE TABLE" statements for each table
85 HTML/POD Producer Options:
87 --pretty Use CGI::Pretty for the output
88 --title Title of schema
90 TTSchema Producer Options:
92 --template The path to the template
93 --tt-var var=value Pass extra variables to the template
94 --tt-conf option=value Pass extra config options to Template
96 XML-SQLFairy Producer Options:
98 --add-prefix Use an explicit namespace prefix of 'sqlf:'
99 --prefix=<p> Use the namespace prefix given as argument.
100 --no-newlines Write the XML as a single line.
101 --indent=<n> Use <n> characters of whitespace to indent the XML.
105 This script is part of the SQL Fairy project. It will try to convert
106 any source file for which it has a grammar into any format for which
109 If using "show-warnings," be sure to redirect STDERR to a separate file.
110 In bash, you could do this:
112 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
115 You can specify a parser or producer located in any module that Perl
116 knows about, allowing you to easily substitute your own.
120 # -------------------------------------------------------------------
127 use vars qw( $VERSION );
128 $VERSION = sprintf "%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/;
130 my $from; # the original database
131 my $to; # the destination database
132 my $help; # show POD and bail
133 my $stdin; # whether to read STDIN for create script
134 my $no_comments; # whether to put comments in out file
135 my $show_warnings; # whether to show warnings from SQL::Translator
136 my $add_drop_table; # whether to show warnings from SQL::Translator
137 my $debug; # whether to print debug info
138 my $trace; # whether to print parser trace
139 my $list; # list all parsers and producers
140 my $no_trim; # don't trim whitespace on xSV fields
141 my $no_scan; # don't scan xSV fields for data types and sizes
142 my $field_separator; # for xSV files
143 my $record_separator; # for xSV files
144 my $validate; # whether to validate the parsed document
145 my $imap_file; # filename where to place image map coords
146 my $imap_url; # URL to use in making image map
147 my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
148 my $template; # template to pass to TTSchema producer
149 my %tt_vars; # additional template vars to pass the TTSchema producer
150 my %tt_conf; # additional template conf to pass the TTSchema producer
151 my $title; # title for HTML/POD producer
152 my $add_prefix; # Use explicit namespace prefix (XML producer)
153 my $prefix; # Set explicit namespace prefix (XML producer)
154 my $newlines; # Add newlines around tags (XML producer)
155 my $indent; # Number of indent chars for XML
156 my $dsn; # DBI parser
157 my $db_user; # DBI parser
158 my $db_password; # DBI parser
159 my $show_version; # Show version and exit script
168 'add-drop-table' => \$add_drop_table,
169 'd|debug' => \$debug,
170 'f|from|parser:s' => \$from,
171 'fs:s' => \$field_separator,
173 'imap-file:s' => \$imap_file,
174 'imap-url:s' => \$imap_url,
175 't|to|producer:s' => \$to,
177 'pretty!' => \$pretty,
178 'no-comments' => \$no_comments,
179 'no-scan' => \$no_scan,
180 'no-trim' => \$no_trim,
181 'rs:s' => \$record_separator,
182 'show-warnings' => \$show_warnings,
183 'template:s' => \$template,
184 'tt-var=s' => \%tt_vars,
185 'tt-conf=s' => \%tt_conf,
186 'title:s' => \$title,
188 'v|validate' => \$validate,
190 'db-user:s' => \$db_user,
191 'db-password:s' => \$db_password,
192 'dumper-dsn:s' => \$dumper_dsn,
193 'dumper-db-user:s' => \$dumper_db_user,
194 'dumper-db-pass:s' => \$dumper_db_pass,
196 'skiplike:s' => \$skiplike,
197 'add_truncate' => \$add_truncate,
198 'add-prefix' => \$add_prefix,
199 'prefix:s' => \$prefix,
200 'indent:s' => \$indent,
201 'newlines!' => \$newlines,
202 'version' => \$show_version,
205 my @files = @ARGV; # source files
207 if ( defined($from) && $from eq 'DBI' ) {
215 pod2usage(1) if $help;
217 if ( $show_version ) {
218 print "SQL::Translator v", $SQL::Translator::VERSION, "\n";
222 my $translator = SQL::Translator->new(
223 debug => $debug || 0,
224 trace => $trace || 0,
225 no_comments => $no_comments || 0,
226 show_warnings => $show_warnings || 0,
227 add_drop_table => $add_drop_table || 0,
228 validate => $validate || 0,
230 trim_fields => $no_trim ? 0 : 1,
231 scan_fields => $no_scan ? 0 : 1,
232 field_separator => $field_separator,
233 record_separator => $record_separator,
236 db_password => $db_password,
239 imap_file => $imap_file,
240 imap_url => $imap_url,
243 tt_vars => \%tt_vars,
244 tt_conf => \%tt_conf,
247 db_user => $dumper_db_user,
248 db_password => $dumper_db_pass,
250 skiplike => $skiplike,
251 add_truncate => $add_truncate,
252 add_prefix => $add_prefix,
255 newlines => $newlines,
260 my @parsers = $translator->list_parsers;
261 my @producers = $translator->list_producers;
263 for ( @parsers, @producers ) {
264 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
269 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
270 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
275 pod2usage( msg => 'Please supply "from" and "to" arguments' )
278 $translator->parser($from);
279 $translator->producer($to);
281 for my $file (@files) {
283 ($file eq '-') ? (data => \*STDIN) :
284 ($file eq '!') ? (data => '') :
287 my $output = $translator->translate(@args) or die
288 "Error: " . $translator->error;
293 # ----------------------------------------------------
294 # It is not all books that are as dull as their readers.
295 # Henry David Thoreau
296 # ----------------------------------------------------
302 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
303 darren chamberlain E<lt>darren@cpan.orgE<gt>.
307 SQL::Translator, L<http://sqlfairy.sourceforge.net>.