85be62fe4dfe9f67dd6db27894755e62f5a94a24
[dbsrgits/SQL-Translator.git] / bin / sqlt-dumper
1 #!/usr/bin/perl
2
3 # -------------------------------------------------------------------
4 # $Id: sqlt-dumper 1440 2009-01-17 16:31:57Z jawnsy $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =head1 NAME
24
25 sqlt-dumper - create a dumper script from a schema
26
27 =head1 SYNOPSIS
28
29   sqlt-dumper -d Oracle [options] schema.sql > dumper.pl
30
31   ./dumper.pl > data.sql
32
33   Options:
34
35     -h|--help         Show help and exit
36     --skip=t1[,t2]    Skip tables in comma-separated list
37     --skiplike=regex  Skip tables matching the regular expression
38     -u|--user         Database username
39     -p|--password     Database password
40     --dsn             DSN for DBI
41
42 =head1 DESCRIPTION
43
44 This script uses SQL::Translator to parse the SQL schema and create a
45 Perl script that can connect to the database and dump the data as
46 INSERT statements (a la mysqldump) or MySQL's LOAD FILE syntax.  You may
47 specify tables to "skip" (also using a "skiplike" regular expression)
48 and the generated dumper script will not have those tables.  However,
49 these will also be options in the generated dumper, so you can wait to
50 specify these options when you dump your database.  The database
51 username, password, and DSN can be hardcoded into the generated
52 script, or part of the DSN can be intuited from the "database"
53 argument.
54
55 =cut
56
57 # -------------------------------------------------------------------
58
59 use strict;
60 use Pod::Usage;
61 use Getopt::Long;
62 use SQL::Translator;
63 use File::Basename qw(basename);
64
65 use vars '$VERSION';
66 $VERSION = '1.99';
67
68 my ( $help, $db, $skip, $skiplike, $db_user, $db_pass, $dsn );
69 GetOptions(
70     'h|help'        => \$help,
71     'd|f|from|db=s' => \$db,
72     'skip:s'        => \$skip,
73     'skiplike:s'    => \$skiplike,
74     'u|user:s'      => \$db_user,
75     'p|password:s'  => \$db_pass,
76     'dsn:s'         => \$dsn,
77 ) or pod2usage;
78
79 pod2usage(0) if $help;
80 pod2usage( 'No database driver specified' ) unless $db;
81 $db_user ||= 'username';
82 $db_pass ||= 'password';
83 $dsn     ||= "dbi:$db:_";
84
85 my $file            = shift @ARGV or pod2usage( -msg => 'No input file' );
86 my $t               = SQL::Translator->new(
87     from            => $db,
88     to              => 'Dumper',
89     producer_args   => {
90         skip        => $skip,
91         skiplike    => $skiplike,
92         db_user     => $db_user,
93         db_password => $db_pass,
94         dsn         => $dsn,
95     }
96 );
97
98 print $t->translate( $file );
99
100 exit(0);
101
102 # -------------------------------------------------------------------
103
104 =pod
105
106 =head1 AUTHOR
107
108 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
109
110 =head1 SEE ALSO
111
112 perl, SQL::Translator, SQL::Translator::Producer::Dumper.
113
114 =cut