4 # -------------------------------------------------------------------
5 # $Id: sqlt,v 1.12 2004-02-06 17:48:16 kycl4rk 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 --trace Print parser trace info
50 --show-warnings Print warnings to STDERR
54 --dsn DSN for connecting to database
55 --db-user Database user
56 --db-password Database password
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
67 --add-drop-table Add 'DROP TABLE' statements before creates
68 --no-comments Don't include comments in SQL output
70 Diagram Producer Options:
72 --imap-file Filename to put image map data
73 --imap-url URL to use for image map
75 HTML/POD Producer Options:
77 --pretty Use CGI::Pretty for the output
78 --title Title of schema
80 TTSchema Producer Options:
82 --template The path to the template
84 XML-SQLFairy Producer Options:
86 --emit-empty-tags Print empty tags for attributes
87 --attrib-values Use attributes instead of tags for
88 values of the schema objects
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
96 If using "show-warnings," be sure to redirect STDERR to a separate file.
97 In bash, you could do this:
99 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
102 You can specify a parser or producer located in any module that Perl
103 knows about, allowing you to easily substitute your own.
107 # -------------------------------------------------------------------
114 use vars qw( $VERSION );
115 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
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
124 my $debug; # whether to print debug info
125 my $trace; # whether to print parser trace
126 my $list; # list all parsers and producers
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
129 my $field_separator; # for xSV files
130 my $record_separator; # for xSV files
131 my $validate; # whether to validate the parsed document
132 my $imap_file; # filename where to place image map coords
133 my $imap_url; # URL to use in making image map
134 my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
135 my $template; # template to pass to TTSchema producer
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
139 my $dsn; # DBI parser
140 my $db_user; # DBI parser
141 my $db_password; # DBI parser
144 'add-drop-table' => \$add_drop_table,
145 'attrib-values' => \$attrib_values,
146 'd|debug' => \$debug,
147 'emit-empty-tags' => \$emit_empty_tags,
148 'f|from|parser:s' => \$from,
149 'fs:s' => \$field_separator,
151 'imap-file:s' => \$imap_file,
152 'imap-url:s' => \$imap_url,
153 't|to|producer:s' => \$to,
155 'pretty!' => \$pretty,
156 'no-comments' => \$no_comments,
157 'no-scan' => \$no_scan,
158 'no-trim' => \$no_trim,
159 'rs:s' => \$record_separator,
160 'show-warnings' => \$show_warnings,
161 'template:s' => \$template,
162 'title:s' => \$title,
164 'v|validate' => \$validate,
166 'db-user:s' => \$db_user,
167 'db-password:s' => \$db_password,
170 my @files = @ARGV; # source files
172 if ( defined($from) && $from eq 'DBI' ) {
180 pod2usage(1) if $help;
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,
190 trim_fields => $no_trim ? 0 : 1,
191 scan_fields => $no_scan ? 0 : 1,
192 field_separator => $field_separator,
193 record_separator => $record_separator,
196 db_password => $db_password,
199 imap_file => $imap_file,
200 imap_url => $imap_url,
204 emit_empty_tags => $emit_empty_tags,
205 attrib_values => $attrib_values,
210 my @parsers = $translator->list_parsers;
211 my @producers = $translator->list_producers;
213 for ( @parsers, @producers ) {
214 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
219 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
220 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
225 pod2usage( msg => 'Please supply "from" and "to" arguments' )
228 $translator->parser($from);
229 $translator->producer($to);
231 for my $file (@files) {
233 ($file eq '-') ? (data => \*STDIN) :
234 ($file eq '!') ? (data => '') :
237 my $output = $translator->translate(@args) or die
238 "Error: " . $translator->error;
243 # ----------------------------------------------------
244 # It is not all books that are as dull as their readers.
245 # Henry David Thoreau
246 # ----------------------------------------------------
252 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
253 darren chamberlain E<lt>darren@cpan.orgE<gt>.
257 SQL::Translator, L<http://sqlfairy.sourceforge.net>.