5 use lib '/openils/lib/perl5/';
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenSRF::Utils::JSON;
14 use Time::HiRes qw/time/;
18 my ($config, $output, @auto, @order, @wipe) =
19 ('/openils/conf/opensrf_core.xml', 'pg_loader-output');
22 'config=s' => \$config,
23 'output=s' => \$output,
25 'autoprimary=s' => \@auto,
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 while ( my $rec = <> ) {
58 $row = OpenSRF::Utils::JSON->JSON2perl($rec);
61 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
65 my $class = $row->class_name;
66 my $hint = $row->json_hint;
68 next unless ( grep /$hint/, @order );
70 if (!$fieldcache{$hint}) {
71 my @cols = $row->real_fields;
72 if (grep { $_ eq $hint} @auto) {
73 @cols = grep { $_ ne $class->Identity } @cols;
77 { table => $class->Table,
78 sequence => $class->Sequence,
79 pkey => $class->Identity,
83 #XXX it burnnnsssessss
84 $fieldcache{$hint}{table} =~ s/\.full_rec/.real_full_rec/o if ($hint eq 'mfr');
86 my $fields = join(',', @{ $fieldcache{$hint}{fields} });
87 $main_out->print( "DELETE FROM $fieldcache{$hint}{table};\n" ) if (grep {$_ eq $hint } @wipe);
88 # Speed up loading of bib records
90 $main_out->print("\nSELECT reporter.disable_materialized_simple_record_trigger();\n");
92 $main_out->print( "COPY $fieldcache{$hint}{table} ($fields) FROM '$pwd/$output.$hint.sql';\n" );
96 my $line = [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
109 if ($hint eq 'bre' and $fieldcache{$hint}{fields}[$x] eq 'quality') {
115 $out_files{$hint}->print( join("\t", @data)."\n" );
117 if (!($count % 500)) {
118 print STDERR "\r$count\t". $count / (time - $starttime);
124 if (grep /^mfr$/, %out_files) {
125 $main_out->print("SELECT reporter.enable_materialized_simple_record_trigger();\n");
127 $main_out->print("-- COMMIT;\n\n");