23054de5050977a3dd475a4d3de366422fbb3842
[dbsrgits/SQL-Translator.git] / script / sqlt-dumper
1 #!/usr/bin/env perl
2
3 =head1 NAME
4
5 sqlt-dumper - create a dumper script from a schema
6
7 =head1 SYNOPSIS
8
9   sqlt-dumper -d Oracle [options] schema.sql > dumper.pl
10
11   ./dumper.pl > data.sql
12
13   Options:
14
15     -h|--help         Show help and exit
16     --skip=t1[,t2]    Skip tables in comma-separated list
17     --skiplike=regex  Skip tables matching the regular expression
18     -u|--user         Database username
19     -p|--password     Database password
20     --dsn             DSN for DBI
21
22 =head1 DESCRIPTION
23
24 This script uses SQL::Translator to parse the SQL schema and create a
25 Perl script that can connect to the database and dump the data as
26 INSERT statements (a la mysqldump) or MySQL's LOAD FILE syntax.  You may
27 specify tables to "skip" (also using a "skiplike" regular expression)
28 and the generated dumper script will not have those tables.  However,
29 these will also be options in the generated dumper, so you can wait to
30 specify these options when you dump your database.  The database
31 username, password, and DSN can be hardcoded into the generated
32 script, or part of the DSN can be intuited from the "database"
33 argument.
34
35 =cut
36
37 # -------------------------------------------------------------------
38
39 use strict;
40 use warnings;
41 use Pod::Usage;
42 use Getopt::Long;
43 use SQL::Translator;
44 use File::Basename qw(basename);
45
46 use vars '$VERSION';
47 $VERSION = '1.59';
48
49 my ( $help, $db, $skip, $skiplike, $db_user, $db_pass, $dsn );
50 GetOptions(
51     'h|help'        => \$help,
52     'd|f|from|db=s' => \$db,
53     'skip:s'        => \$skip,
54     'skiplike:s'    => \$skiplike,
55     'u|user:s'      => \$db_user,
56     'p|password:s'  => \$db_pass,
57     'dsn:s'         => \$dsn,
58 ) or pod2usage;
59
60 pod2usage(0) if $help;
61 pod2usage( 'No database driver specified' ) unless $db;
62 $db_user ||= 'username';
63 $db_pass ||= 'password';
64 $dsn     ||= "dbi:$db:_";
65
66 my $file            = shift @ARGV or pod2usage( -msg => 'No input file' );
67 my $t               = SQL::Translator->new(
68     from            => $db,
69     to              => 'Dumper',
70     producer_args   => {
71         skip        => $skip,
72         skiplike    => $skiplike,
73         db_user     => $db_user,
74         db_password => $db_pass,
75         dsn         => $dsn,
76     }
77 );
78
79 print $t->translate( $file );
80
81 exit(0);
82
83 # -------------------------------------------------------------------
84
85 =pod
86
87 =head1 AUTHOR
88
89 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
90
91 =head1 SEE ALSO
92
93 perl, SQL::Translator, SQL::Translator::Producer::Dumper.
94
95 =cut