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');
23 'config=s' => \$config,
24 'output=s' => \$output,
26 'autoprimary=s' => \@auto,
28 'nocommit=i' => \$nocommit,
37 OpenSRF::System->bootstrap_client( config_file => $config );
38 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
40 my $main_out = FileHandle->new(">$output.sql") if ($output);
42 binmode($main_out,'utf8');
44 $main_out->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
45 $main_out->print("BEGIN;\n\n");
49 $out_files{$h} = FileHandle->new(">$output.$h.sql");
50 binmode($out_files{$h},'utf8');
55 my $after_commit = '';
56 while ( my $rec = <> ) {
61 $row = OpenSRF::Utils::JSON->JSON2perl($rec);
64 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
68 my $class = $row->class_name;
69 my $hint = $row->json_hint;
71 next unless ( grep /$hint/, @order );
73 if (!$fieldcache{$hint}) {
74 my @cols = $row->real_fields;
75 if (grep { $_ eq $hint} @auto) {
76 @cols = grep { $_ ne $class->Identity } @cols;
80 { table => $class->Table,
81 sequence => $class->Sequence,
82 pkey => $class->Identity,
86 #XXX it burnnnsssessss
87 $fieldcache{$hint}{table} =~ s/\.full_rec/.real_full_rec/o if ($hint eq 'mfr');
89 my $fields = join(',', @{ $fieldcache{$hint}{fields} });
90 $main_out->print( "DELETE FROM $fieldcache{$hint}{table};\n" ) if (grep {$_ eq $hint } @wipe);
91 # Speed up loading of bib records
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') {
110 $d = int($d) if ($d ne '\N');
115 $out_files{$hint}->print( join("\t", @data)."\n" );
117 if (!($count % 500)) {
118 print STDERR "\r$count\t". $count / (time - $starttime);
124 for my $hint (@order) {
125 next if (grep { $_ eq $hint} @auto);
126 next unless ($fieldcache{$hint}{sequence});
127 $after_commit .= "SELECT setval('$fieldcache{$hint}{sequence}'::TEXT, (SELECT MAX($fieldcache{$hint}{pkey}) FROM $fieldcache{$hint}{table}), TRUE);\n";
130 if (grep /^mfr$/, %out_files) {
131 $main_out->print("SELECT reporter.enable_materialized_simple_record_trigger();\n");
132 $main_out->print("SELECT reporter.disable_materialized_simple_record_trigger();\n");
135 $main_out->print("COMMIT;\n\n") unless $nocommit;
136 $main_out->print($after_commit);