3 # -------------------------------------------------------------------
4 # $Id: sqlt-dumper.pl,v 1.2 2003-06-24 21:00:24 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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.
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.
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
21 # -------------------------------------------------------------------
23 =head1 sqlt-dumper.pl - create a dumper script from a schema
27 ./sqlt-dumper.pl -d Oracle [options] schema.sql > dumper.pl
28 ./dumper.pl > data.sql
32 -h|--help Show help and exit
33 --add-truncate Add "TRUNCATE TABLE" statements for each table
34 --skip=t1[,t2] Skip tables in comma-separated list
35 -u|--user Database username
36 -p|--password Database password
41 This script uses SQL::Translator to parse the SQL schema and create a
42 Perl script that can connect to the database and dump the data as
43 INSERT statements a la mysqldump. If you enable "add-truncate" or
44 specify tables to "skip," then the generated dumper script will have
45 those hardcoded. However, these will also be options in the generated
46 dumper, so you can wait to specify these options when you dump your
47 database. The database username, password, and DSN can be hardcoded
48 into the generated script, or part of the DSN can be intuited from the
58 my ( $help, $db, $add_truncate, $skip, $db_user, $db_pass, $dsn );
61 'd|f|from|db=s' => \$db,
62 'add-truncate' => \$add_truncate,
64 'u|user:s' => \$db_user,
65 'p|password:s' => \$db_pass,
69 pod2usage(0) if $help;
70 pod2usage( 'No database driver specified' ) unless $db;
71 $db_user ||= 'username';
72 $db_pass ||= 'password';
75 my $file = shift @ARGV or pod2usage( -msg => 'No input file' );
77 my $t = SQL::Translator->new;
78 $t->parser( $db ) or die $t->error, "\n";
79 $t->filename( $file ) or die $t->error, "\n";
81 my %skip = map { $_, 1 } map { s/^\s+|\s+$//; $_ } split (/,/, $skip);
82 my $parser = $t->parser or die $t->error;
83 $parser->($t, $t->data);
84 my $schema = $t->schema;
92 # By sqlt-dumper.pl, part of the SQLFairy project
93 # For more info, see http://sqlfairy.sourceforge.net/
100 my ( \$help, \$add_truncate, \$skip );
102 'h|help' => \\\$help,
103 'add-truncate' => \\\$add_truncate,
104 'skip:s' => \\\$skip,
113 -h|--help Show help and exit
114 --add-truncate Add "TRUNCATE TABLE" statements
115 --skip=t1[,t2] Comma-separated list of tables to skip
121 my \%skip = map { \$_, 1 } map { s/^\\s+|\\s+\$//; \$_ } split (/,/, \$skip);
122 my \$db = DBI->connect('$dsn', '$db_user', '$db_pass');
126 for my $table ( $schema->get_tables ) {
127 my $table_name = $table->name;
128 next if $skip{ $table_name };
129 my ( @field_names, %types );
130 for my $field ( $table->get_fields ) {
131 $types{ $field->name } = $field->data_type =~ m/(char|str|long|text)/
132 ? 'string' : 'number';
133 push @field_names, $field->name;
137 "#\n# Table: $table_name\n#\n{\n",
138 " next if \$skip{'$table_name'};\n",
139 " print \"--\\n-- Data for table '$table_name'\\n--\\n\";\n\n",
140 " if ( \$add_truncate ) {\n",
141 " print \"TRUNCATE TABLE $table_name;\\n\";\n",
145 my $insert = "INSERT INTO $table_name (". join(', ', @field_names).
148 if ( $add_truncate ) {
149 $out .= " print \"TRUNCATE TABLE $table_name;\\n\";\n";
154 join("\n", map { " $_ => '$types{ $_ }'," } @field_names),
156 " my \$data = \$db->selectall_arrayref(\n",
157 " 'select ", join(', ', @field_names), " from $table_name',\n",
158 " { Columns => {} },\n",
160 " for my \$rec ( \@{ \$data } ) {\n",
162 " for my \$fld ( qw[", join(' ', @field_names), "] ) {\n",
163 " my \$val = \$rec->{ \$fld };\n",
164 " if ( \$types{ \$fld } eq 'string' ) {\n",
165 " \$val =~ s/'/\\'/g;\n",
166 " \$val = defined \$val ? qq['\$val'] : qq[''];\n",
169 " \$val = defined \$val ? \$val : 'NULL';\n",
171 " push \@vals, \$val;\n",
173 " print \"$insert\", join(', ', \@vals), \");\\n\";\n",