6 use OpenSRF::EX qw/:try/;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenSRF::Utils::JSON;
12 use Time::HiRes qw/time/;
16 my ($config, $output, @auto, @order, @wipe) =
17 ('@sysconfdir@/opensrf_core.xml', 'pg_loader-output');
21 'config=s' => \$config,
22 'output=s' => \$output,
24 'autoprimary=s' => \@auto,
26 'nocommit=i' => \$nocommit,
35 OpenSRF::System->bootstrap_client( config_file => $config );
36 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
38 my $main_out = FileHandle->new(">$output.sql") if ($output);
40 binmode($main_out,'utf8');
42 $main_out->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
43 $main_out->print("BEGIN;\n\n");
47 $out_files{$h} = FileHandle->new(">$output.$h.sql");
48 binmode($out_files{$h},'utf8');
53 my $after_commit = '';
54 while ( my $rec = <> ) {
59 $row = OpenSRF::Utils::JSON->JSON2perl($rec);
62 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
66 my $class = $row->class_name;
67 my $hint = $row->json_hint;
69 next unless ( grep /$hint/, @order );
71 if (!$fieldcache{$hint}) {
72 my @cols = $row->real_fields;
73 if (grep { $_ eq $hint} @auto) {
74 @cols = grep { $_ ne $class->Identity } @cols;
78 { table => $class->Table,
79 sequence => $class->Sequence,
80 pkey => $class->Identity,
84 #XXX it burnnnsssessss
85 $fieldcache{$hint}{table} =~ s/\.full_rec/.real_full_rec/o if ($hint eq 'mfr');
87 my $fields = join(',', @{ $fieldcache{$hint}{fields} });
88 $main_out->print( "DELETE FROM $fieldcache{$hint}{table};\n" ) if (grep {$_ eq $hint } @wipe);
89 # Speed up loading of bib records
90 $main_out->print( "COPY $fieldcache{$hint}{table} ($fields) FROM '$pwd/$output.$hint.sql';\n" );
94 my $line = [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
107 if ($hint eq 'bre' and $fieldcache{$hint}{fields}[$x] eq 'quality') {
108 $d = int($d) if ($d ne '\N');
113 $out_files{$hint}->print( join("\t", @data)."\n" );
115 if (!($count % 500)) {
116 print STDERR "\r$count\t". $count / (time - $starttime);
122 for my $hint (@order) {
123 next if (grep { $_ eq $hint} @auto);
124 next unless ($fieldcache{$hint}{sequence});
125 $after_commit .= "SELECT setval('$fieldcache{$hint}{sequence}'::TEXT, (SELECT MAX($fieldcache{$hint}{pkey}) FROM $fieldcache{$hint}{table}), TRUE);\n";
128 if (grep /^mfr$/, %out_files) {
129 $main_out->print("SELECT reporter.enable_materialized_simple_record_trigger();\n");
130 $main_out->print("SELECT reporter.disable_materialized_simple_record_trigger();\n");
133 $main_out->print("COMMIT;\n\n") unless $nocommit;
134 $main_out->print($after_commit);