3 # -------------------------------------------------------------------
4 # $Id: sqlt-dumper,v 1.3 2004-02-06 17:48:16 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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 - create a dumper script from a schema
29 sqlt-dumper -d Oracle [options] schema.sql > dumper.pl
31 ./dumper.pl > data.sql
35 -h|--help Show help and exit
36 --add-truncate Add "TRUNCATE TABLE" statements for each table
37 --skip=t1[,t2] Skip tables in comma-separated list
38 -u|--user Database username
39 -p|--password Database password
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. If you enable "add-truncate" or
47 specify tables to "skip," then the generated dumper script will have
48 those hardcoded. However, these will also be options in the generated
49 dumper, so you can wait to specify these options when you dump your
50 database. The database username, password, and DSN can be hardcoded
51 into the generated script, or part of the DSN can be intuited from the
56 # -------------------------------------------------------------------
62 use File::Basename qw(basename);
65 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
67 my ( $help, $db, $add_truncate, $skip, $db_user, $db_pass, $dsn );
70 'd|f|from|db=s' => \$db,
71 'add-truncate' => \$add_truncate,
73 'u|user:s' => \$db_user,
74 'p|password:s' => \$db_pass,
78 pod2usage(0) if $help;
79 pod2usage( 'No database driver specified' ) unless $db;
80 $db_user ||= 'username';
81 $db_pass ||= 'password';
84 my $file = shift @ARGV or pod2usage( -msg => 'No input file' );
86 my $t = SQL::Translator->new;
87 $t->parser( $db ) or die $t->error, "\n";
88 $t->filename( $file ) or die $t->error, "\n";
90 my %skip = map { $_, 1 } map { s/^\s+|\s+$//; $_ } split (/,/, $skip);
91 my $parser = $t->parser or die $t->error;
92 $parser->($t, $t->data);
93 my $schema = $t->schema;
95 my $prog = basename $0;
102 # By $prog, part of the SQLFairy project
103 # For more info, see http://sqlfairy.sourceforge.net/
110 my ( \$help, \$add_truncate, \$skip );
112 'h|help' => \\\$help,
113 'add-truncate' => \\\$add_truncate,
114 'skip:s' => \\\$skip,
123 -h|--help Show help and exit
124 --add-truncate Add "TRUNCATE TABLE" statements
125 --skip=t1[,t2] Comma-separated list of tables to skip
131 my \%skip = map { \$_, 1 } map { s/^\\s+|\\s+\$//; \$_ } split (/,/, \$skip);
132 my \$db = DBI->connect('$dsn', '$db_user', '$db_pass');
136 for my $table ( $schema->get_tables ) {
137 my $table_name = $table->name;
138 next if $skip{ $table_name };
139 my ( @field_names, %types );
140 for my $field ( $table->get_fields ) {
141 $types{ $field->name } = $field->data_type =~ m/(char|str|long|text)/
142 ? 'string' : 'number';
143 push @field_names, $field->name;
147 "#\n# Table: $table_name\n#\n{\n",
148 " next if \$skip{'$table_name'};\n",
149 " print \"--\\n-- Data for table '$table_name'\\n--\\n\";\n\n",
150 " if ( \$add_truncate ) {\n",
151 " print \"TRUNCATE TABLE $table_name;\\n\";\n",
155 my $insert = "INSERT INTO $table_name (". join(', ', @field_names).
158 if ( $add_truncate ) {
159 $out .= " print \"TRUNCATE TABLE $table_name;\\n\";\n";
164 join("\n", map { " $_ => '$types{ $_ }'," } @field_names),
166 " my \$data = \$db->selectall_arrayref(\n",
167 " 'select ", join(', ', @field_names), " from $table_name',\n",
168 " { Columns => {} },\n",
170 " for my \$rec ( \@{ \$data } ) {\n",
172 " for my \$fld ( qw[", join(' ', @field_names), "] ) {\n",
173 " my \$val = \$rec->{ \$fld };\n",
174 " if ( \$types{ \$fld } eq 'string' ) {\n",
175 " \$val =~ s/'/\\'/g;\n",
176 " \$val = defined \$val ? qq['\$val'] : qq[''];\n",
179 " \$val = defined \$val ? \$val : 'NULL';\n",
181 " push \@vals, \$val;\n",
183 " print \"$insert\", join(', ', \@vals), \");\\n\";\n",
193 # -------------------------------------------------------------------
199 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
203 perl, SQL::Translator.