new faster version of mig-bibload
[migration-tools.git] / mig-bin / mig-loadbibs
1 #!/usr/bin/perl
2
3 ###############################################################################
4 =pod
5
6 =item B<loadbibs> --stage_file foo.mrc 
7
8 Takes a load of bibs from a binary marc file and loads them into mig staging table 
9 of bibio_record_entry.
10
11 Takes these optional arguments:
12
13 --append 
14
15 When used it does not drop the staging table and instead adds onto it.  
16
17 --source
18
19 Sets an x_source value on the staging table to the one supplied instead of the 
20 default of none.
21
22 --xml 
23
24 By default the program assumes a USMARC file.  This flag will identify it as 
25 a MARCXML file instead.
26
27 =back
28
29 =cut
30
31 ###############################################################################
32
33 use strict;
34 use warnings;
35
36 use DBI;
37 #binmode STDIN, ':bytes';
38 use Env qw(
39     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
40     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
41 );
42 use Pod::Usage;
43 use Switch;
44 use Cwd 'abs_path';
45 use FindBin;
46 use UNIVERSAL;
47 my $mig_bin = "$FindBin::Bin/";
48 use lib "$FindBin::Bin/";
49 use Mig;
50
51 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
52 pod2usage(-verbose => 1) if ! $ARGV[1];
53
54 my $next_arg_is_file = 0;
55 my $append = 0;
56 my $next_arg_is_source = 0;
57 my $next_arg_is_stage = 0;
58 my $stage_table = 'biblio_record_entry';
59 my $source = 'default';
60 my $file_is_xml = 0;
61 my $dbh = Mig::db_connect();
62 my $infile;
63 my $i = 0;
64 my $batch;
65 binmode STDIN, ':utf8';
66
67 #MARC::Charset->assume_unicode(1); 
68 MARC::Charset->ignore_errors(1);
69
70 foreach my $arg (@ARGV) {
71     if ($arg eq '--stage_file') {
72         $next_arg_is_file = 1;
73         next;
74     }
75     if ($next_arg_is_file) {
76         $infile = $arg;
77         $next_arg_is_file = 0;
78         next;
79     }
80     if ($arg eq '--source') {
81         $next_arg_is_source = 1;
82         next;
83     }
84     if ($next_arg_is_source) {
85         $source = $arg;
86         $next_arg_is_source = 0;
87         next;
88     }
89 }
90
91 my $bre_test = check_for_table($dbh,'biblio_record_entry');
92 if ($bre_test == 0) { create_child_bre($dbh); }
93
94 my $xmig_test = check_for_column($dbh,'biblio_record_entry','x_migrate');
95 if ($xmig_test == 0) { add_column($dbh,'biblio_record_entry','x_migrate','BOOLEAN DEFAULT TRUE');
96
97 my $xsource_test = check_for_column($dbh,'biblio_record_entry','x_source');
98 if ($xsource_test == 0) { add_column($dbh,'biblio_record_entry','x_source','TEXT');
99
100 my $last_xact;
101 if ($source) { $last_xact = "'$MIGSCHEMA $source'" } else { $last_xact = "'$MIGSCHEMA'"; }
102
103 #flatten out MARC XML FILE
104 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
105 $i = 0;
106 my $record;
107 while(my $line = <$xml>) {
108          if ($line =~ /^<\/?collection/) { next; }
109          chomp $line;
110          $record = $record . $line;
111          if ($line =~ /^<\/record/) {
112                 stage_record($dbh,$record,$last_xact); 
113                 $record = '';
114         }
115
116 close $xml;
117
118
119 #load the MARC XML FILE TO STAGING 
120 report_progress("Records staged", $i) if 0 != $i % 100;
121
122 print "Finis.\n";
123
124 # beyond here be functions 
125
126 sub create_child_bre {
127     my $dbh = shift;
128     $dbh->do("DO \$\$ 
129         DECLARE
130             t   BOOLEAN;
131         BEGIN
132         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
133         IF t = FALSE THEN
134             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
135         END IF;
136         END \$\$;");
137
138     return ();
139 }
140
141 sub abort {
142     my $msg = shift;
143     print STDERR "$0: $msg", "\n";
144     exit 1;
145 }
146
147 sub report_progress {
148     my ($msg, $counter) = @_;
149     if (defined $counter) {
150         print STDERR "$msg: $counter\n";
151     } else {
152         print STDERR "$msg\n";
153     }
154 }
155
156 sub stage_record {
157     my $dbh = shift;
158     my $record = shift;
159         my $last_xact = shift;
160         $record = '$_$' . $record . '$_$';
161     my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry (last_xact_id,marc) VALUES ($last_xact,$record);";
162     my $sth = $dbh->prepare($sql);
163     $sth->execute();
164         return;
165 }
166
167 sub check_for_table {
168     my $dbh = shift;
169     my $table = shift;
170     my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
171     my $sth = $dbh->prepare($sql);
172     $sth->execute();
173     my @sqlresult = $sth->fetchrow_array;
174     my $r = pop @sqlresult;
175     if ($r) { return $r; } else { return 0; }
176 }
177
178 sub check_for_column {
179     my $dbh = shift;
180     my $table = shift;
181         my $column = shift;
182     my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = $column;";
183     my $sth = $dbh->prepare($sql);
184     $sth->execute();
185     my @sqlresult = $sth->fetchrow_array;
186     my $r = pop @sqlresult;
187     if ($r) { return $r; } else { return 0; }
188 }
189
190 sub add_column {
191     my $dbh = shift;
192     my $table = shift;
193     my $column = shift;
194         my $column_type = shift;
195     my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $COLUMN $COLUMN_TYPE;";
196         my $r = check_for_column($dbh,$table,$column);
197         if ($r == 0) { abort('failed to create column'; } else { return $r; }
198 }
199