3 # -------------------------------------------------------------------
4 # $Id: sqlt,v 1.2 2003-08-26 03:54:59 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kclar@cpan.org>,
7 # darren chamberlain <darren@cpan.org>
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 --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
61 --add-drop-table Add 'DROP TABLE' statements before creates
62 --no-comments Don't include comments in SQL output
64 Diagram Producer Options:
66 --imap-file Filename to put image map data
67 --imap-url URL to use for image map
69 HTML/POD Producer Options:
71 --pretty Use CGI::Pretty for the outpu
72 --title Title of schema
74 TTSchema Producer Options:
76 --template The path to the template
78 XML-SQLFairy Producer Options:
80 --emit-empty-tags Print empty tags for attributes
81 --attrib-values Use attributes instead of tags for
82 values of the schema objects
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
90 If using "show-warnings," be sure to redirect STDERR to a separate file.
91 In bash, you could do this:
93 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
96 You can specify a parser or producer located in any module that Perl
97 knows about, allowing you to easily substitute your own.
101 # -------------------------------------------------------------------
108 use vars qw( $VERSION );
109 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
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
118 my $debug; # whether to print debug info
119 my $trace; # whether to print parser trace
120 my $list; # list all parsers and producers
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
123 my $field_separator; # for xSV files
124 my $record_separator; # for xSV files
125 my $validate; # whether to validate the parsed document
126 my $imap_file; # filename where to place image map coords
127 my $imap_url; # URL to use in making image map
128 my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
129 my $template; # template to pass to TTSchema producer
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
135 'add-drop-table' => \$add_drop_table,
136 'attrib-values' => \$attrib_values,
137 'd|debug' => \$debug,
138 'emit_empty_tags' => \$emit_empty_tags,
139 'f|from|parser:s' => \$from,
140 'fs:s' => \$field_separator,
142 'imap-file:s' => \$imap_file,
143 'imap-url:s' => \$imap_url,
144 't|to|producer:s' => \$to,
146 'pretty!' => \$pretty,
147 'no-comments' => \$no_comments,
148 'no-scan' => \$no_scan,
149 'no-trim' => \$no_trim,
150 'rs:s' => \$record_separator,
151 'show-warnings' => \$show_warnings,
152 'template:s' => \$template,
153 'title:s' => \$title,
155 'v|validate' => \$validate,
158 my @files = @ARGV; # source files
160 pod2usage(1) if $help;
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,
170 trim_fields => $no_trim ? 0 : 1,
171 scan_fields => $no_scan ? 0 : 1,
172 field_separator => $field_separator,
173 record_separator => $record_separator,
176 imap_file => $imap_file,
177 imap_url => $imap_url,
181 emit_empty_tags => $emit_empty_tags,
182 attrib_values => $attrib_values,
187 my @parsers = $translator->list_parsers;
188 my @producers = $translator->list_producers;
190 for ( @parsers, @producers ) {
191 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
196 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
197 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
202 pod2usage(2) unless $from && $to && @files;
204 $translator->parser($from);
205 $translator->producer($to);
207 for my $file (@files) {
208 my $output = $translator->translate(file => $file) or die
209 "Error: " . $translator->error;
213 # ----------------------------------------------------
214 # It is not all books that are as dull as their readers.
215 # Henry David Thoreau
216 # ----------------------------------------------------
222 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
226 perl, SQL::Translator, Parse::RecDescent,
227 L<http://sqlfairy.sourceforge.net>.