5 use OpenSRF::EX qw/:try/;
6 use OpenSRF::Utils::SettingsClient;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenSRF::Utils::JSON;
11 use Time::HiRes qw/time/;
15 my ($config, $output, @auto, @order, @wipe, $quiet) =
16 ('/openils/conf/opensrf_core.xml');
19 GetOptions( 'config=s' => \$config,
20 'output=s' => \$output,
22 'autoprimary=s' => \@auto,
24 'nocommit|n' => \$nocommit,
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,
65 #XXX it burnnnsssessss
66 $fieldcache{$hint}{table} =~ s/\.full_rec/.real_full_rec/o if ($hint eq 'mfr');
69 push @{ $lineset{$hint} }, [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
71 if (!$quiet && !($count % 500)) {
72 print STDERR "\r$count\t". $count / (time - $starttime);
78 print STDERR "\nWriting file ...\n" if (!$quiet);
80 $output = '&STDOUT' unless ($output);
81 $output = FileHandle->new(">$output") if ($output);
83 binmode($output,'utf8');
85 $output->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
86 $output->print("BEGIN;\n\n");
88 my $after_commit = '';
90 # continue if there was no data for this table
91 next unless ($fieldcache{$h});
93 my $fields = join(',', @{ $fieldcache{$h}{fields} });
94 $output->print( "DELETE FROM $fieldcache{$h}{table};\n" ) if (grep {$_ eq $h } @wipe);
95 # Speed up loading of bib records
96 $output->print( "COPY $fieldcache{$h}{table} ($fields) FROM STDIN;\n" );
98 for my $line (@{ $lineset{$h} }) {
111 if ($h eq 'bre' and $fieldcache{$h}{fields}[$x] eq 'quality') {
112 $d = int($d) if ($d ne '\N');
117 $output->print( join("\t", @data)."\n" );
120 $output->print('\.'."\n\n");
123 $output->print("SELECT reporter.enable_materialized_simple_record_trigger();\n");
124 $output->print("SELECT reporter.disable_materialized_simple_record_trigger();\n");
127 $after_commit .= "SELECT setval('$fieldcache{$h}{sequence}'::TEXT, (SELECT MAX($fieldcache{$h}{pkey}) FROM $fieldcache{$h}{table}), TRUE);\n"
128 if (!grep { $_ eq $h} @auto);
131 $output->print("COMMIT;\n\n") unless $nocommit;
132 $output->print($after_commit);