Fix to size of float field.
[dbsrgits/SQL-Translator.git] / bin / sqlt
CommitLineData
7a8e1f51 1#!/usr/bin/perl -w
e107f4d2 2# vim: set ft=perl:
16dc9970 3
49e1eb70 4# -------------------------------------------------------------------
2542703f 5# $Id: sqlt,v 1.5 2003-10-04 01:22:46 kycl4rk Exp $
49e1eb70 6# -------------------------------------------------------------------
44598b94 7# Copyright (C) 2002 Ken Y. Clark <kclar@cpan.org>,
783908a1 8# darren chamberlain <darren@cpan.org>
9#
10# This program is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License as
12# published by the Free Software Foundation; version 2.
13#
14# This program is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22# 02111-1307 USA
23# -------------------------------------------------------------------
16dc9970 24
44598b94 25=head1 NAME
26
27sqlt - convert SQL schema using SQL::Translator
28
29=head1 SYNOPSIS
30
31For help:
32
33 sqlt -h|--help
34
35For a list of all parsers and producers:
36
37 sqlt -l|--list
38
39To translate a schema:
40
41 sqlt -f|--from|--parser MySQL
42 -t|--to|--producer Oracle
43 [options]
44 file [file2 ...]
45
46 General Options:
47
48 -d|--debug Print debug info
49 -v|--validate Validate the schema
50 --trace Print parser trace info
51 --show-warnings Print warnings to STDERR
52
e107f4d2 53 DBI Parser Options:
54
55 --dsn DSN for connecting to database
56 --db-user Database user
57 --db-password Database password
58
44598b94 59 xSV Parser Options:
60
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
65
66 DB Producer Options:
67
68 --add-drop-table Add 'DROP TABLE' statements before creates
69 --no-comments Don't include comments in SQL output
70
71 Diagram Producer Options:
72
73 --imap-file Filename to put image map data
74 --imap-url URL to use for image map
75
76 HTML/POD Producer Options:
77
1ae0e8cb 78 --pretty Use CGI::Pretty for the output
44598b94 79 --title Title of schema
80
81 TTSchema Producer Options:
82
83 --template The path to the template
84
85 XML-SQLFairy Producer Options:
86
87 --emit-empty-tags Print empty tags for attributes
88 --attrib-values Use attributes instead of tags for
89 values of the schema objects
90
91=head1 DESCRIPTION
92
93This script is part of the SQL Fairy project. It will try to convert
94any source file for which it has a grammar into any format for which
95it has a producer.
96
97If using "show-warnings," be sure to redirect STDERR to a separate file.
98In bash, you could do this:
99
100 $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
101 file.sql 1>out 2>err
102
103You can specify a parser or producer located in any module that Perl
104knows about, allowing you to easily substitute your own.
105
106=cut
107
108# -------------------------------------------------------------------
109
16dc9970 110use strict;
111use Getopt::Long;
112use Pod::Usage;
113use SQL::Translator;
49e1eb70 114
16dc9970 115use vars qw( $VERSION );
2542703f 116$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
64ad4e66 117
118my $from; # the original database
119my $to; # the destination database
120my $help; # show POD and bail
121my $stdin; # whether to read STDIN for create script
122my $no_comments; # whether to put comments in out file
123my $show_warnings; # whether to show warnings from SQL::Translator
124my $add_drop_table; # whether to show warnings from SQL::Translator
64ad4e66 125my $debug; # whether to print debug info
126my $trace; # whether to print parser trace
127my $list; # list all parsers and producers
032bd3c7 128my $no_trim; # don't trim whitespace on xSV fields
129my $no_scan; # don't scan xSV fields for data types and sizes
64ad4e66 130my $field_separator; # for xSV files
131my $record_separator; # for xSV files
35d75351 132my $validate; # whether to validate the parsed document
5cb879e2 133my $imap_file; # filename where to place image map coords
134my $imap_url; # URL to use in making image map
1ea530d4 135my $pretty; # use CGI::Pretty instead of CGI (HTML producer)
8ac2c35e 136my $template; # template to pass to TTSchema producer
44598b94 137my $title; # title for HTML/POD producer
138my $emit_empty_tags; # show empty XML tags
139my $attrib_values; # use XML attributes instead of tags
e107f4d2 140my $dsn; # DBI parser
141my $db_user; # DBI parser
142my $db_password; # DBI parser
16dc9970 143
16dc9970 144GetOptions(
44598b94 145 'add-drop-table' => \$add_drop_table,
146 'attrib-values' => \$attrib_values,
147 'd|debug' => \$debug,
148 'emit_empty_tags' => \$emit_empty_tags,
d529894e 149 'f|from|parser:s' => \$from,
44598b94 150 'fs:s' => \$field_separator,
783908a1 151 'h|help' => \$help,
44598b94 152 'imap-file:s' => \$imap_file,
153 'imap-url:s' => \$imap_url,
154 't|to|producer:s' => \$to,
d529894e 155 'l|list' => \$list,
44598b94 156 'pretty!' => \$pretty,
d529894e 157 'no-comments' => \$no_comments,
032bd3c7 158 'no-scan' => \$no_scan,
44598b94 159 'no-trim' => \$no_trim,
64ad4e66 160 'rs:s' => \$record_separator,
44598b94 161 'show-warnings' => \$show_warnings,
8ac2c35e 162 'template:s' => \$template,
44598b94 163 'title:s' => \$title,
164 'trace' => \$trace,
165 'v|validate' => \$validate,
e107f4d2 166 'dsn:s' => \$dsn,
167 'db-user:s' => \$db_user,
168 'db-password:s' => \$db_password,
16dc9970 169) or pod2usage(2);
170
44598b94 171my @files = @ARGV; # source files
16dc9970 172
173pod2usage(1) if $help;
d529894e 174
44598b94 175my $translator = SQL::Translator->new(
176 debug => $debug || 0,
177 trace => $trace || 0,
178 no_comments => $no_comments || 0,
179 show_warnings => $show_warnings || 0,
180 add_drop_table => $add_drop_table || 0,
181 validate => $validate || 0,
182 parser_args => {
032bd3c7 183 trim_fields => $no_trim ? 0 : 1,
184 scan_fields => $no_scan ? 0 : 1,
64ad4e66 185 field_separator => $field_separator,
186 record_separator => $record_separator,
e107f4d2 187 dsn => $dsn,
188 db_user => $db_user,
189 db_password => $db_password,
5cb879e2 190 },
191 producer_args => {
192 imap_file => $imap_file,
193 imap_url => $imap_url,
1ea530d4 194 pretty => $pretty,
8ac2c35e 195 ttfile => $template,
44598b94 196 title => $title,
197 emit_empty_tags => $emit_empty_tags,
198 attrib_values => $attrib_values,
5cb879e2 199 },
d529894e 200);
201
202if ( $list ) {
203 my @parsers = $translator->list_parsers;
204 my @producers = $translator->list_producers;
205
206 for ( @parsers, @producers ) {
207 if ( $_ =~ m/.+::(\w+)\.pm/ ) {
208 $_ = $1;
209 }
210 }
211
212 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
213 print "\nProducers:\n", map { "\t$_\n" } sort @producers;
214 print "\n";
215 exit(0);
216}
217
2542703f 218pod2usage( msg => 'Please supply "from" and "to" arguments' ) unless $from && $to;
d529894e 219
783908a1 220$translator->parser($from);
221$translator->producer($to);
222
223for my $file (@files) {
1ea530d4 224 my $output = $translator->translate(file => $file) or die
49e1eb70 225 "Error: " . $translator->error;
226 print $output;
783908a1 227}
16dc9970 228
49e1eb70 229# ----------------------------------------------------
16dc9970 230# It is not all books that are as dull as their readers.
231# Henry David Thoreau
49e1eb70 232# ----------------------------------------------------
16dc9970 233
44598b94 234=pod
96844cae 235
16dc9970 236=head1 AUTHOR
237
44598b94 238Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
16dc9970 239
240=head1 SEE ALSO
241
44598b94 242perl, SQL::Translator, Parse::RecDescent,
243L<http://sqlfairy.sourceforge.net>.
16dc9970 244
245=cut