4 use lib '/openils/lib/perl5/';
7 use OpenSRF::EX qw/:try/;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenILS::Utils::Fieldmapper;
10 use OpenSRF::Utils::JSON;
13 use Time::HiRes qw/time/;
17 my ($config, $output, @auto, @order, @wipe, $quiet) =
18 ('/openils/conf/opensrf_core.xml');
21 'config=s' => \$config,
22 'output=s' => \$output,
24 'autoprimary=s' => \@auto,
32 OpenSRF::System->bootstrap_client( config_file => $config );
33 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
37 while ( my $rec = <> ) {
42 $row = OpenSRF::Utils::JSON->JSON2perl($rec);
45 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
49 my $class = $row->class_name;
50 my $hint = $row->json_hint;
52 if (!$lineset{$hint}) {
54 my @cols = $row->real_fields;
55 if (grep { $_ eq $hint} @auto) {
56 @cols = grep { $_ ne $class->Identity } @cols;
60 { table => $class->Table,
61 sequence => $class->Sequence,
62 pkey => $class->Identity,
67 push @{ $lineset{$hint} }, [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
69 if (!$quiet && !($count % 500)) {
70 print STDERR "\r$count\t". $count / (time - $starttime);
76 print STDERR "\nWriting file ...\n" if (!$quiet);
78 $output = '&STDOUT' unless ($output);
79 $output = FileHandle->new(">$output") if ($output);
81 binmode($output,'utf8');
83 $output->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
84 $output->print("BEGIN;\n\n");
87 # continue if there was no data for this table
88 next unless ($fieldcache{$h});
90 my $fields = join(',', @{ $fieldcache{$h}{fields} });
91 $output->print( "DELETE FROM $fieldcache{$h}{table};\n" ) if (grep {$_ eq $h } @wipe);
92 $output->print( "COPY $fieldcache{$h}{table} ($fields) FROM STDIN;\n" );
94 for my $line (@{ $lineset{$h} }) {
107 if ($h eq 'bre' and $fieldcache{$h}{fields}[$x] eq 'quality') {
113 $output->print( join("\t", @data)."\n" );
116 $output->print('\.'."\n\n");
118 $output->print("SELECT setval('$fieldcache{$h}{sequence}'::TEXT, (SELECT MAX($fieldcache{$h}{pkey}) FROM $fieldcache{$h}{table}), TRUE);\n\n")
119 if (!grep { $_ eq $h} @auto);
122 $output->print("COMMIT;\n\n");