11# !/usr/bin/env perl
22
3- our $VERSION = " 1.08 " ;
3+ our $VERSION = " 1.09 " ;
44
55=head1 NAME
66
@@ -367,7 +367,7 @@ sub sample_table ($) {
367367 return Table-> new($opt {schema }, $sample_table );
368368}
369369
370- sub notice {
370+ sub notice (@) {
371371 return unless $opt {verbose };
372372 print STDERR join " " , @_ ;
373373}
@@ -443,20 +443,23 @@ if ($schema_oid && !$opt{force}) {
443443
444444$dbh -> do(qq{ SET client_min_messages = warning } ); # suppress notice messages
445445if ($opt {force }) {
446- notice( " Dropping sample schema $opt {schema}\n " ) ;
446+ notice " Dropping sample schema $opt {schema}\n " ;
447447 $dbh -> do(qq{ DROP SCHEMA IF EXISTS $opt {schema} CASCADE } );
448448}
449449
450450if ($opt {file }) {
451451 open STDOUT , ' >' , $opt {file } or croak " unable to redirect stdout: $! " ;
452452}
453453
454- unless ($opt {encoding }) {
455- ($opt {encoding }) = $dbh -> selectrow_array(qq{ SHOW server_encoding } );
456- }
454+ my ($server_encoding ) = $dbh -> selectrow_array(qq{ SHOW server_encoding } );
455+ notice " Server encoding is $server_encoding \n " ;
456+
457+ $opt {encoding } ||= $server_encoding ;
458+ notice " Client encoding is $opt {encoding}\n " ;
459+ binmode STDOUT , " :encoding($opt {encoding})" ;
457460
458461unless ($opt {' data-only' }) {
459- notice( " Exporting schema\n " ) ;
462+ notice " Exporting schema\n " ;
460463
461464 local $ENV {PGUSER } = $opt {db_user };
462465 local $ENV {PGDATABASE } = $opt {db_name };
@@ -471,7 +474,7 @@ unless ($opt{'data-only'}) {
471474# If running PostgreSQL 9.1 or later, use UNLOGGED tables
472475my $unlogged = $pg_version >= version-> declare(' 9.1' ) ? ' UNLOGGED' : ' ' ;
473476
474- notice( " Creating sample schema $opt {schema}\n " ) ;
477+ notice " Creating sample schema $opt {schema}\n " ;
475478$dbh -> do(qq{ CREATE SCHEMA $opt {schema} } );
476479my $created_schema = 1; # keep track that we actually did it; see END block
477480
@@ -487,7 +490,7 @@ foreach my $limit (grep { /\S/ } map { split /\s*,\s*/ } list($opt{limit})) {
487490
488491 push @limits , [$match , $rule ];
489492}
490- notice( " [limit] $_ ->[0] = $_ ->[1]\n " ) foreach @limits ;
493+ notice " [limit] $_ ->[0] = $_ ->[1]\n " foreach @limits ;
491494
492495# create copies of each table in a separate schema and insert no
493496# more than --limit rows initially.
@@ -510,7 +513,7 @@ while (my $row = lower_keys($sth->fetchrow_hashref)) {
510513 my $sample_table = sample_table($table );
511514 $sample_tables { $table } = $sample_table ;
512515
513- notice( " Creating table $sample_table " ) ;
516+ notice " Creating table $sample_table " ;
514517
515518 # find first matching limit rule
516519 my $where = ' TRUE' ;
@@ -544,7 +547,7 @@ while (my $row = lower_keys($sth->fetchrow_hashref)) {
544547 if ($opt {verbose }) {
545548 my ($num_rows ) =
546549 $dbh -> selectrow_array(qq{ SELECT count(*) FROM $sample_table } );
547- notice( " $num_rows \n " ) ;
550+ notice " $num_rows \n " ;
548551 }
549552}
550553
@@ -627,13 +630,13 @@ while ($num_rows) {
627630 # }
628631 # warn "\n\n";
629632
630- notice(
633+ notice
631634 " Copying $target_table ($target_cols ) rows referenced from " .
632635 " $sample_fk_table ($fk_cols )... "
633- ) ;
636+ ;
634637
635- my $count = $dbh -> do($query );
636- notice(( $count + 0) . " rows\n " ) ;
638+ my $count = $dbh -> do($query ) || 0 ;
639+ notice " $count rows\n " ;
637640
638641 $num_rows += $count ;
639642 }
@@ -668,7 +671,7 @@ SET escape_string_warning = off;
668671
669672EOF
670673
671- notice( " Exporting sequences\n " ) ;
674+ notice " Exporting sequences\n " ;
672675print " \n " ;
673676foreach my $name (sort keys %seq ) {
674677 my $constant = quote_constant($name );
@@ -687,7 +690,7 @@ foreach my $table (@tables) {
687690 my $sample_table = sample_table($table );
688691 if ($opt {verbose }) {
689692 my ($count ) = $dbh -> selectrow_array(" SELECT count(*) FROM $sample_table " );
690- notice( " Exporting data from $sample_table ($count )\n " ) ;
693+ notice " Exporting data from $sample_table ($count )\n " ;
691694 }
692695 print " COPY $table FROM stdin;\n " ;
693696 $dbh -> do(qq{ COPY $sample_table TO STDOUT } );
@@ -703,13 +706,13 @@ foreach my $table (@tables) {
703706}
704707print " \n " ;
705708
706- notice(" Done.\n " );
707- exit 0;
708-
709709END {
710710 # remove sample tables unless requested not to
711711 if ($created_schema && !$opt {keep }) {
712- notice( " Dropping sample schema $opt {schema}\n " ) ;
712+ notice " Dropping sample schema $opt {schema}\n " ;
713713 $dbh -> do(" DROP SCHEMA $opt {schema} CASCADE" );
714714 }
715+
716+ notice " Done.\n " ;
717+ exit 0;
715718}
0 commit comments