3 ###############################################################################
6 =item B<loadbibs> --stage_file foo.mrc
8 Takes a load of bibs from a binary marc file and loads them into mig staging table
11 Takes these optional arguments:
15 When used it does not drop the staging table and instead adds onto it.
19 Sets an x_source value on the staging table to the one supplied instead of the
24 By default the program assumes a USMARC file. This flag will identify it as
25 a MARCXML file instead.
31 ###############################################################################
37 #binmode STDIN, ':bytes';
39 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
40 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
47 my $mig_bin = "$FindBin::Bin/";
48 use lib "$FindBin::Bin/";
51 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
52 pod2usage(-verbose => 1) if ! $ARGV[1];
54 my $next_arg_is_file = 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';
61 my $dbh = Mig::db_connect();
65 binmode STDIN, ':utf8';
67 #MARC::Charset->assume_unicode(1);
68 MARC::Charset->ignore_errors(1);
70 foreach my $arg (@ARGV) {
71 if ($arg eq '--stage_file') {
72 $next_arg_is_file = 1;
75 if ($next_arg_is_file) {
77 $next_arg_is_file = 0;
80 if ($arg eq '--source') {
81 $next_arg_is_source = 1;
84 if ($next_arg_is_source) {
86 $next_arg_is_source = 0;
91 my $bre_test = check_for_table($dbh,'biblio_record_entry');
92 if ($bre_test == 0) { create_child_bre($dbh); }
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');
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');
101 if ($source) { $last_xact = "'$MIGSCHEMA $source'" } else { $last_xact = "'$MIGSCHEMA'"; }
103 #flatten out MARC XML FILE
104 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
107 while(my $line = <$xml>) {
108 if ($line =~ /^<\/?collection/) { next; }
110 $record = $record . $line;
111 if ($line =~ /^<\/record/) {
112 stage_record($dbh,$record,$last_xact);
119 #load the MARC XML FILE TO STAGING
120 report_progress("Records staged", $i) if 0 != $i % 100;
124 # beyond here be functions
126 sub create_child_bre {
132 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
134 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
143 print STDERR "$0: $msg", "\n";
147 sub report_progress {
148 my ($msg, $counter) = @_;
149 if (defined $counter) {
150 print STDERR "$msg: $counter\n";
152 print STDERR "$msg\n";
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);
167 sub check_for_table {
170 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
171 my $sth = $dbh->prepare($sql);
173 my @sqlresult = $sth->fetchrow_array;
174 my $r = pop @sqlresult;
175 if ($r) { return $r; } else { return 0; }
178 sub check_for_column {
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);
185 my @sqlresult = $sth->fetchrow_array;
186 my $r = pop @sqlresult;
187 if ($r) { return $r; } else { return 0; }
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; }