Uses Test::SQL::Translator.pm
[dbsrgits/SQL-Translator.git] / bin / sqlt
1 #!/usr/bin/perl -w
2 # vim: set ft=perl:
3
4 # -------------------------------------------------------------------
5 # $Id: sqlt,v 1.12 2004-02-06 17:48:16 kycl4rk Exp $
6 # -------------------------------------------------------------------
7 # Copyright (C) 2002-4 SQLFairy Authors
8 #
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.
12 #
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.
17 #
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
21 # 02111-1307  USA
22 # -------------------------------------------------------------------
23
24 =head1 NAME
25
26 sqlt - convert SQL schema using SQL::Translator
27
28 =head1 SYNOPSIS
29
30 For help:
31
32   sqlt -h|--help
33
34 For a list of all parsers and producers: 
35
36   sqlt -l|--list
37
38 To translate a schema:
39
40   sqlt -f|--from|--parser MySQL 
41        -t|--to|--producer Oracle 
42        [options] 
43        file [file2 ...]
44
45   General Options:
46
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
51
52   DBI Parser Options:
53
54     --dsn              DSN for connecting to database
55     --db-user          Database user
56     --db-password      Database password              
57
58   xSV Parser Options:
59
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 
64
65   DB Producer Options:
66
67     --add-drop-table   Add 'DROP TABLE' statements before creates
68     --no-comments      Don't include comments in SQL output
69
70   Diagram Producer Options:
71
72     --imap-file        Filename to put image map data
73     --imap-url         URL to use for image map
74
75   HTML/POD Producer Options:
76
77     --pretty           Use CGI::Pretty for the output
78     --title            Title of schema
79
80   TTSchema Producer Options:
81
82     --template         The path to the template
83
84   XML-SQLFairy Producer Options:
85
86     --emit-empty-tags  Print empty tags for attributes
87     --attrib-values    Use attributes instead of tags for 
88                        values of the schema objects
89
90 =head1 DESCRIPTION
91
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
94 it has a producer.
95
96 If using "show-warnings," be sure to redirect STDERR to a separate file.  
97 In bash, you could do this:
98
99     $ sql_translator.pl -f MySQL -t PostgreSQL --show-warnings \
100        file.sql 1>out 2>err
101
102 You can specify a parser or producer located in any module that Perl
103 knows about, allowing you to easily substitute your own.
104
105 =cut
106
107 # -------------------------------------------------------------------
108
109 use strict;
110 use Getopt::Long;
111 use Pod::Usage;
112 use SQL::Translator;
113
114 use vars qw( $VERSION );
115 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
116
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 
142
143 GetOptions(
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,
150     'h|help'          => \$help,
151     'imap-file:s'     => \$imap_file,
152     'imap-url:s'      => \$imap_url,
153     't|to|producer:s' => \$to,
154     'l|list'          => \$list,
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,
163     'trace'           => \$trace,
164     'v|validate'      => \$validate,
165     'dsn:s'           => \$dsn,
166     'db-user:s'       => \$db_user,
167     'db-password:s'   => \$db_password,
168 ) or pod2usage(2);
169
170 my @files = @ARGV; # source files
171 unless ( @files ) {
172     if ( defined($from) && $from eq 'DBI' ) {
173         @files = ('!');
174     }
175     else {
176         @files = ('-');
177     }
178 }
179
180 pod2usage(1) if $help;
181
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,
189     parser_args          => {
190         trim_fields      => $no_trim ? 0 : 1,
191         scan_fields      => $no_scan ? 0 : 1,
192         field_separator  => $field_separator,
193         record_separator => $record_separator,
194         dsn              => $dsn,
195         db_user          => $db_user,
196         db_password      => $db_password,
197     },
198     producer_args   => {
199         imap_file        => $imap_file,
200         imap_url         => $imap_url,
201         pretty           => $pretty,
202         ttfile           => $template,
203         title            => $title,
204         emit_empty_tags  => $emit_empty_tags,
205         attrib_values    => $attrib_values,
206     },
207 );
208
209 if ( $list ) {
210     my @parsers   = $translator->list_parsers;
211     my @producers = $translator->list_producers;
212
213     for ( @parsers, @producers ) {
214         if ( $_ =~ m/.+::(\w+)\.pm/ ) {
215             $_ = $1;
216         }
217     }
218     
219     print "\nParsers:\n",   map { "\t$_\n" } sort @parsers;
220     print "\nProducers:\n", map { "\t$_\n" } sort @producers;
221     print "\n";
222     exit(0);
223 }
224
225 pod2usage( msg => 'Please supply "from" and "to" arguments' ) 
226     unless $from && $to;
227
228 $translator->parser($from);
229 $translator->producer($to);
230
231 for my $file (@files) {
232     my @args = 
233         ($file eq '-') ? (data => \*STDIN) :
234         ($file eq '!') ? (data => '') :
235         (file => $file);
236
237     my $output = $translator->translate(@args) or die
238         "Error: " . $translator->error;
239
240     print $output;
241 }
242
243 # ----------------------------------------------------
244 # It is not all books that are as dull as their readers.
245 # Henry David Thoreau
246 # ----------------------------------------------------
247
248 =pod
249
250 =head1 AUTHOR
251
252 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
253 darren chamberlain E<lt>darren@cpan.orgE<gt>.
254
255 =head1 SEE ALSO
256
257 SQL::Translator, L<http://sqlfairy.sourceforge.net>.
258
259 =cut