4 # -------------------------------------------------------------------
5 # Copyright (C) 2002-2009 SQLFairy Authors
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; version 2.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # -------------------------------------------------------------------
24 sqlt - convert SQL schema using SQL::Translator
32 For a list of all parsers and producers:
36 To translate a schema:
38 sqlt -f|--from|--parser MySQL
39 -t|--to|--producer Oracle
45 -d|--debug Print debug info
46 -v|--validate Validate the schema
47 --version Show the version of SQL::Translator
48 --trace Print parser trace info
49 --show-warnings Print warnings to STDERR
51 General Parser Options:
53 --skip Comma-separated list of tables to skip (only implemented in some parsers)
54 --ignore_opts Comma-separated list of table options to ignore
58 --dsn DSN for connecting to database
59 (see also --use-same-auth below)
60 --db-user Database user
61 --db-password Database password
65 --fs The field separator
66 --rs The record separator
67 --no-trim Don't trim whitespace on fields
68 --no-scan Don't scan fields for data types and sizes
72 --mysql-parser-version Target MySQL parser version for dealing with
73 /*! comments; default = 30000
75 MySQL Producer Options:
77 --mysql-version MySQL server version
79 General Producer Options
81 --producer-db-user Database user for producer
82 --producer-db-pass Database password for producer
83 --producer-dsn DSN for producer
84 --use-same-auth Use these DSN, user, password for producer output
88 --add-drop-table Add 'DROP TABLE' statements before creates
89 --quote-table-names Quote all table names in statements
90 --quote-field-names Qjuote all field names in statements
91 --no-comments Don't include comments in SQL output
93 PostgreSQL Producer Options:
95 --postgres-version PostgreSQL server version
97 Diagram Producer Options:
99 --imap-file Filename to put image map data
100 --imap-url URL to use for image map
102 Dumper Producer Options:
104 --skip Comma-separated list of tables to skip
105 --skiplike Regex for tables to skip
106 --add-truncate Add "TRUNCATE TABLE" statements for each table
108 HTML/POD Producer Options:
110 --pretty Use CGI::Pretty for the output
111 --title Title of schema
113 TTSchema Producer Options:
115 --template The path to the template
116 --tt-var var=value Pass extra variables to the template
117 --tt-conf option=value Pass extra config options to Template
119 XML-SQLFairy Producer Options:
121 --add-prefix Use an explicit namespace prefix of 'sqlf:'
122 --prefix=<p> Use the namespace prefix given as argument.
123 --no-newlines Write the XML as a single line.
124 --indent=<n> Use <n> characters of whitespace to indent the XML.
126 ClassDBI Producer Options:
128 --package Base package name for Class::DBI modules.
132 This script is part of the SQL Fairy project. It will try to convert
133 any source file for which it has a grammar into any format for which
136 If using "show-warnings," be sure to redirect STDERR to a separate file.
137 In bash, you could do this:
139 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
142 You can specify a parser or producer located in any module that Perl
143 knows about, allowing you to easily substitute your own.
147 # -------------------------------------------------------------------
154 use vars qw( $VERSION );
157 my $from; # the original database
158 my $to; # the destination database
159 my $help; # show POD and bail
160 my $stdin; # whether to read STDIN for create script
161 my $no_comments; # whether to put comments in out file
162 my $show_warnings; # whether to show warnings from SQL::Translator
163 my $add_drop_table; # whether to add "DROP table" statements
164 my $quote_table_names; # whether to quote table names
165 my $quote_field_names; # whether to quote field names
166 my $debug; # whether to print debug info
167 my $trace; # whether to print parser trace
168 my $list; # list all parsers and producers
169 my $no_trim; # don't trim whitespace on xSV fields
170 my $no_scan; # don't scan xSV fields for data types and sizes
171 my $field_separator; # for xSV files
172 my $record_separator; # for xSV files
173 my $validate; # whether to validate the parsed document
174 my $imap_file; # filename where to place image map coords
175 my $imap_url; # URL to use in making image map
176 my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
177 my $template; # template to pass to TTSchema producer
178 my %tt_vars; # additional template vars to pass the TTSchema producer
179 my %tt_conf; # additional template conf to pass the TTSchema producer
180 my $title; # title for HTML/POD producer
181 my $add_prefix; # Use explicit namespace prefix (XML producer)
182 my $prefix; # Set explicit namespace prefix (XML producer)
183 my $newlines; # Add newlines around tags (XML producer)
184 my $indent; # Number of indent chars for XML
185 my $package_name; # Base class name for ClassDBI
186 my $use_same_auth =0; # producer uses same DSN, user, password as parser
187 my $dsn; # DBI parser
188 my $db_user; # DBI parser
189 my $db_password; # DBI parser
190 my $show_version; # Show version and exit script
194 my $producer_db_user; # DSN for producer (e.g. Dumper, ClassDBI)
195 my $producer_db_password; # db_pass "
196 my $producer_dsn; # db_user "
198 my $mysql_parser_version; # MySQL parser arg for /*! comments
199 my $postgres_version; # PostgreSQL version
200 my $mysql_version; # MySQL version
203 'add-drop-table' => \$add_drop_table,
204 'quote_table_names' => \$quote_table_names,
205 'quote_field_names' => \$quote_field_names,
206 'd|debug' => \$debug,
207 'f|from|parser:s' => \$from,
208 'fs:s' => \$field_separator,
210 'imap-file:s' => \$imap_file,
211 'imap-url:s' => \$imap_url,
212 't|to|producer:s' => \$to,
214 'pretty!' => \$pretty,
215 'no-comments' => \$no_comments,
216 'no-scan' => \$no_scan,
217 'no-trim' => \$no_trim,
218 'rs:s' => \$record_separator,
219 'show-warnings' => \$show_warnings,
220 'template:s' => \$template,
221 'tt-var=s' => \%tt_vars,
222 'tt-conf=s' => \%tt_conf,
223 'title:s' => \$title,
225 'v|validate' => \$validate,
227 'db-user:s' => \$db_user,
228 'db-password:s' => \$db_password,
229 'producer-dsn:s' => \$producer_dsn,
230 'producer-db-user:s'=> \$producer_db_user,
231 'producer-db-pass:s'=> \$producer_db_password,
233 'skiplike:s' => \$skiplike,
234 'ignore_opts:s' => \$ignore_opts,
235 'add_truncate' => \$add_truncate,
236 'add-prefix' => \$add_prefix,
237 'prefix:s' => \$prefix,
238 'indent:s' => \$indent,
239 'newlines!' => \$newlines,
240 'package=s' => \$package_name,
241 'use-same-auth' => \$use_same_auth,
242 'version' => \$show_version,
243 'mysql-parser-version=i' => \$mysql_parser_version,
244 'postgres-version=f' => \$postgres_version,
245 'mysql-version=f' => \$mysql_version,
248 if ($use_same_auth) {
249 $producer_dsn = $dsn;
250 $producer_db_user = $db_user;
251 $producer_db_password = $db_password;
254 $from = 'DBI' if !defined $from && defined $dsn;
255 my @files = @ARGV; # source files
257 if ( defined($from) && $from eq 'DBI' ) {
265 pod2usage(1) if $help;
267 if ( $show_version ) {
268 print "SQL::Translator v", $SQL::Translator::VERSION, "\n";
272 my $translator = SQL::Translator->new(
273 debug => $debug || 0,
274 trace => $trace || 0,
275 no_comments => $no_comments || 0,
276 show_warnings => $show_warnings || 0,
277 add_drop_table => $add_drop_table || 0,
278 quote_table_names => $quote_table_names || 1,
279 quote_field_names => $quote_field_names || 1,
280 validate => $validate || 0,
282 trim_fields => $no_trim ? 0 : 1,
283 scan_fields => $no_scan ? 0 : 1,
284 field_separator => $field_separator,
285 record_separator => $record_separator,
288 db_password => $db_password,
289 mysql_parser_version => $mysql_parser_version,
291 ignore_opts => $ignore_opts,
294 imap_file => $imap_file,
295 imap_url => $imap_url,
298 tt_vars => \%tt_vars,
299 tt_conf => \%tt_conf,
301 dsn => $producer_dsn,
302 db_user => $producer_db_user,
303 db_password => $producer_db_password,
305 skiplike => $skiplike,
306 add_truncate => $add_truncate,
307 add_prefix => $add_prefix,
310 newlines => $newlines,
311 postgres_version => $postgres_version,
312 mysql_version => $mysql_version,
313 package_name => $package_name,
318 my @parsers = $translator->list_parsers;
319 my @producers = $translator->list_producers;
321 for ( @parsers, @producers ) {
322 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
327 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
328 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
333 pod2usage( msg => 'Please supply "from" and "to" arguments' )
336 $translator->parser($from);
337 $translator->producer($to);
339 for my $file (@files) {
341 ($file eq '-') ? (data => \*STDIN) :
342 ($file eq '!') ? (data => '') :
345 my $output = $translator->translate(@args) or die
346 "Error: " . $translator->error;
351 # ----------------------------------------------------
352 # It is not all books that are as dull as their readers.
353 # Henry David Thoreau
354 # ----------------------------------------------------
360 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
361 darren chamberlain E<lt>darren@cpan.orgE<gt>.
365 SQL::Translator, L<http://sqlfairy.sourceforge.net>.