3 # -------------------------------------------------------------------
4 # $Id: sqlt-dumper.pl,v 1.3 2003-08-21 00:29:57 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 # -------------------------------------------------------------------
25 sqlt-dumper.pl - create a dumper script from a schema
29 ./sqlt-dumper.pl -d Oracle [options] schema.sql > dumper.pl
30 ./dumper.pl > data.sql
34 -h|--help Show help and exit
35 --add-truncate Add "TRUNCATE TABLE" statements for each table
36 --skip=t1[,t2] Skip tables in comma-separated list
37 -u|--user Database username
38 -p|--password Database password
43 This script uses SQL::Translator to parse the SQL schema and create a
44 Perl script that can connect to the database and dump the data as
45 INSERT statements a la mysqldump. If you enable "add-truncate" or
46 specify tables to "skip," then the generated dumper script will have
47 those hardcoded. However, these will also be options in the generated
48 dumper, so you can wait to specify these options when you dump your
49 database. The database username, password, and DSN can be hardcoded
50 into the generated script, or part of the DSN can be intuited from the
60 my ( $help, $db, $add_truncate, $skip, $db_user, $db_pass, $dsn );
63 'd|f|from|db=s' => \$db,
64 'add-truncate' => \$add_truncate,
66 'u|user:s' => \$db_user,
67 'p|password:s' => \$db_pass,
71 pod2usage(0) if $help;
72 pod2usage( 'No database driver specified' ) unless $db;
73 $db_user ||= 'username';
74 $db_pass ||= 'password';
77 my $file = shift @ARGV or pod2usage( -msg => 'No input file' );
79 my $t = SQL::Translator->new;
80 $t->parser( $db ) or die $t->error, "\n";
81 $t->filename( $file ) or die $t->error, "\n";
83 my %skip = map { $_, 1 } map { s/^\s+|\s+$//; $_ } split (/,/, $skip);
84 my $parser = $t->parser or die $t->error;
85 $parser->($t, $t->data);
86 my $schema = $t->schema;
94 # By sqlt-dumper.pl, part of the SQLFairy project
95 # For more info, see http://sqlfairy.sourceforge.net/
102 my ( \$help, \$add_truncate, \$skip );
104 'h|help' => \\\$help,
105 'add-truncate' => \\\$add_truncate,
106 'skip:s' => \\\$skip,
115 -h|--help Show help and exit
116 --add-truncate Add "TRUNCATE TABLE" statements
117 --skip=t1[,t2] Comma-separated list of tables to skip
123 my \%skip = map { \$_, 1 } map { s/^\\s+|\\s+\$//; \$_ } split (/,/, \$skip);
124 my \$db = DBI->connect('$dsn', '$db_user', '$db_pass');
128 for my $table ( $schema->get_tables ) {
129 my $table_name = $table->name;
130 next if $skip{ $table_name };
131 my ( @field_names, %types );
132 for my $field ( $table->get_fields ) {
133 $types{ $field->name } = $field->data_type =~ m/(char|str|long|text)/
134 ? 'string' : 'number';
135 push @field_names, $field->name;
139 "#\n# Table: $table_name\n#\n{\n",
140 " next if \$skip{'$table_name'};\n",
141 " print \"--\\n-- Data for table '$table_name'\\n--\\n\";\n\n",
142 " if ( \$add_truncate ) {\n",
143 " print \"TRUNCATE TABLE $table_name;\\n\";\n",
147 my $insert = "INSERT INTO $table_name (". join(', ', @field_names).
150 if ( $add_truncate ) {
151 $out .= " print \"TRUNCATE TABLE $table_name;\\n\";\n";
156 join("\n", map { " $_ => '$types{ $_ }'," } @field_names),
158 " my \$data = \$db->selectall_arrayref(\n",
159 " 'select ", join(', ', @field_names), " from $table_name',\n",
160 " { Columns => {} },\n",
162 " for my \$rec ( \@{ \$data } ) {\n",
164 " for my \$fld ( qw[", join(' ', @field_names), "] ) {\n",
165 " my \$val = \$rec->{ \$fld };\n",
166 " if ( \$types{ \$fld } eq 'string' ) {\n",
167 " \$val =~ s/'/\\'/g;\n",
168 " \$val = defined \$val ? qq['\$val'] : qq[''];\n",
171 " \$val = defined \$val ? \$val : 'NULL';\n",
173 " push \@vals, \$val;\n",
175 " print \"$insert\", join(', ', \@vals), \");\\n\";\n",