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) =
18 ('/openils/conf/opensrf_core.xml');
21 'config=s' => \$config,
22 'output=s' => \$output,
24 'autoprimary=s' => \@auto,
31 OpenSRF::System->bootstrap_client( config_file => $config );
32 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
36 while ( my $rec = <> ) {
41 $row = OpenSRF::Utils::JSON->JSON2perl($rec);
44 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
48 my $class = $row->class_name;
49 my $hint = $row->json_hint;
51 if (!$lineset{$hint}) {
53 my @cols = $row->real_fields;
54 if (grep { $_ eq $hint} @auto) {
55 @cols = grep { $_ ne $class->Identity } @cols;
59 { table => $class->Table,
60 sequence => $class->Sequence,
61 pkey => $class->Identity,
66 push @{ $lineset{$hint} }, [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
68 if (!($count % 500)) {
69 print STDERR "\r$count\t". $count / (time - $starttime);
75 print STDERR "\nWriting file ...\n";
77 $output = '&STDOUT' unless ($output);
78 $output = FileHandle->new(">$output") if ($output);
80 binmode($output,'utf8');
82 $output->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
83 $output->print("BEGIN;\n\n");
86 # continue if there was no data for this table
87 next unless ($fieldcache{$h});
89 my $fields = join(',', @{ $fieldcache{$h}{fields} });
90 $output->print( "DELETE FROM $fieldcache{$h}{table};\n" ) if (grep {$_ eq $h } @wipe);
91 $output->print( "COPY $fieldcache{$h}{table} ($fields) FROM STDIN;\n" );
93 for my $line (@{ $lineset{$h} }) {
106 if ($h eq 'bre' and $fieldcache{$h}{fields}[$x] eq 'quality') {
112 $output->print( join("\t", @data)."\n" );
115 $output->print('\.'."\n\n");
117 $output->print("SELECT setval('$fieldcache{$h}{sequence}'::TEXT, (SELECT MAX($fieldcache{$h}{pkey}) FROM $fieldcache{$h}{table}), TRUE);\n\n")
118 if (!grep { $_ eq $h} @auto);
121 $output->print("COMMIT;\n\n");