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 # -------------------------------------------------------------------
155 use vars qw( $VERSION );
158 my $from; # the original database
159 my $to; # the destination database
160 my $help; # show POD and bail
161 my $stdin; # whether to read STDIN for create script
162 my $no_comments; # whether to put comments in out file
163 my $show_warnings; # whether to show warnings from SQL::Translator
164 my $add_drop_table; # whether to add "DROP table" statements
165 my $quote_table_names; # whether to quote table names
166 my $quote_field_names; # whether to quote field names
167 my $debug; # whether to print debug info
168 my $trace; # whether to print parser trace
169 my $list; # list all parsers and producers
170 my $no_trim; # don't trim whitespace on xSV fields
171 my $no_scan; # don't scan xSV fields for data types and sizes
172 my $field_separator; # for xSV files
173 my $record_separator; # for xSV files
174 my $validate; # whether to validate the parsed document
175 my $imap_file; # filename where to place image map coords
176 my $imap_url; # URL to use in making image map
177 my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
178 my $template; # template to pass to TTSchema producer
179 my %tt_vars; # additional template vars to pass the TTSchema producer
180 my %tt_conf; # additional template conf to pass the TTSchema producer
181 my $title; # title for HTML/POD producer
182 my $add_prefix; # Use explicit namespace prefix (XML producer)
183 my $prefix; # Set explicit namespace prefix (XML producer)
184 my $newlines; # Add newlines around tags (XML producer)
185 my $indent; # Number of indent chars for XML
186 my $package_name; # Base class name for ClassDBI
187 my $use_same_auth =0; # producer uses same DSN, user, password as parser
188 my $dsn; # DBI parser
189 my $db_user; # DBI parser
190 my $db_password; # DBI parser
191 my $show_version; # Show version and exit script
195 my $producer_db_user; # DSN for producer (e.g. Dumper, ClassDBI)
196 my $producer_db_password; # db_pass "
197 my $producer_dsn; # db_user "
199 my $mysql_parser_version; # MySQL parser arg for /*! comments
200 my $postgres_version; # PostgreSQL version
201 my $mysql_version; # MySQL version
204 'add-drop-table' => \$add_drop_table,
205 'quote_table_names' => \$quote_table_names,
206 'quote_field_names' => \$quote_field_names,
207 'd|debug' => \$debug,
208 'f|from|parser:s' => \$from,
209 'fs:s' => \$field_separator,
211 'imap-file:s' => \$imap_file,
212 'imap-url:s' => \$imap_url,
213 't|to|producer:s' => \$to,
215 'pretty!' => \$pretty,
216 'no-comments' => \$no_comments,
217 'no-scan' => \$no_scan,
218 'no-trim' => \$no_trim,
219 'rs:s' => \$record_separator,
220 'show-warnings' => \$show_warnings,
221 'template:s' => \$template,
222 'tt-var=s' => \%tt_vars,
223 'tt-conf=s' => \%tt_conf,
224 'title:s' => \$title,
226 'v|validate' => \$validate,
228 'db-user:s' => \$db_user,
229 'db-password:s' => \$db_password,
230 'producer-dsn:s' => \$producer_dsn,
231 'producer-db-user:s'=> \$producer_db_user,
232 'producer-db-pass:s'=> \$producer_db_password,
234 'skiplike:s' => \$skiplike,
235 'ignore_opts:s' => \$ignore_opts,
236 'add_truncate' => \$add_truncate,
237 'add-prefix' => \$add_prefix,
238 'prefix:s' => \$prefix,
239 'indent:s' => \$indent,
240 'newlines!' => \$newlines,
241 'package=s' => \$package_name,
242 'use-same-auth' => \$use_same_auth,
243 'version' => \$show_version,
244 'mysql-parser-version=i' => \$mysql_parser_version,
245 'postgres-version=f' => \$postgres_version,
246 'mysql-version=f' => \$mysql_version,
249 if ($use_same_auth) {
250 $producer_dsn = $dsn;
251 $producer_db_user = $db_user;
252 $producer_db_password = $db_password;
255 $from = 'DBI' if !defined $from && defined $dsn;
256 my @files = @ARGV; # source files
258 if ( defined($from) && $from eq 'DBI' ) {
266 pod2usage(1) if $help;
268 if ( $show_version ) {
269 print "SQL::Translator v", $SQL::Translator::VERSION, "\n";
273 my $translator = SQL::Translator->new(
274 debug => $debug || 0,
275 trace => $trace || 0,
276 no_comments => $no_comments || 0,
277 show_warnings => $show_warnings || 0,
278 add_drop_table => $add_drop_table || 0,
279 quote_table_names => $quote_table_names || 1,
280 quote_field_names => $quote_field_names || 1,
281 validate => $validate || 0,
283 trim_fields => $no_trim ? 0 : 1,
284 scan_fields => $no_scan ? 0 : 1,
285 field_separator => $field_separator,
286 record_separator => $record_separator,
289 db_password => $db_password,
290 mysql_parser_version => $mysql_parser_version,
292 ignore_opts => $ignore_opts,
295 imap_file => $imap_file,
296 imap_url => $imap_url,
299 tt_vars => \%tt_vars,
300 tt_conf => \%tt_conf,
302 dsn => $producer_dsn,
303 db_user => $producer_db_user,
304 db_password => $producer_db_password,
306 skiplike => $skiplike,
307 add_truncate => $add_truncate,
308 add_prefix => $add_prefix,
311 newlines => $newlines,
312 postgres_version => $postgres_version,
313 mysql_version => $mysql_version,
314 package_name => $package_name,
319 my @parsers = $translator->list_parsers;
320 my @producers = $translator->list_producers;
322 for ( @parsers, @producers ) {
323 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
328 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
329 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
334 pod2usage( msg => 'Please supply "from" and "to" arguments' )
337 $translator->parser($from);
338 $translator->producer($to);
340 for my $file (@files) {
342 ($file eq '-') ? (data => \*STDIN) :
343 ($file eq '!') ? (data => '') :
346 my $output = $translator->translate(@args) or die
347 "Error: " . $translator->error;
352 # ----------------------------------------------------
353 # It is not all books that are as dull as their readers.
354 # Henry David Thoreau
355 # ----------------------------------------------------
361 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
362 darren chamberlain E<lt>darren@cpan.orgE<gt>.
366 SQL::Translator, L<http://sqlfairy.sourceforge.net>.