[open-ils-commits] r15768 - trunk/Open-ILS/src/sql/Pg (miker)
svn at svn.open-ils.org
svn at svn.open-ils.org
Tue Mar 9 13:55:17 EST 2010
Author: miker
Date: 2010-03-09 13:55:13 -0500 (Tue, 09 Mar 2010)
New Revision: 15768
Modified:
trunk/Open-ILS/src/sql/Pg/012.schema.vandelay.sql
Log:
teach vandelay.strip_field() how to handle lists of complex field specs (ie: 100d,245c,650); add vandelay.add_field() which will transfer fields from an incoming record to an incumbent record; add vandelay.replace_field() and vandelay.preserve_field(), which are wrappers that use strip and add
Modified: trunk/Open-ILS/src/sql/Pg/012.schema.vandelay.sql
===================================================================
--- trunk/Open-ILS/src/sql/Pg/012.schema.vandelay.sql 2010-03-09 17:22:52 UTC (rev 15767)
+++ trunk/Open-ILS/src/sql/Pg/012.schema.vandelay.sql 2010-03-09 18:55:13 UTC (rev 15768)
@@ -129,17 +129,83 @@
CONSTRAINT vand_import_bib_trash_fields_idx UNIQUE (owner,field)
);
+CREATE OR REPLACE FUNCTION vandelay.add_field ( incumbent_xml TEXT, incoming_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+
+ use MARC::Record;
+ use MARC::File::XML;
+
+ my $incumbent_xml = shift;
+ my $incoming_xml = shift;
+ my $field_spec = shift;
+ $field_spec =~ s/\s+//g;
+
+ my $incumbent_r = MARC::Record->new_from_xml( $incumbent_xml );
+ my $incoming_r = MARC::Record->new_from_xml( $incoming_xml );
+
+ return $incumbent_xml unless ($incumbent_r && $incoming_r);
+
+ my @field_list = split(',' $field_spec);
+
+ my %fields;
+ for my $f (@field_list) {
+ if ($f =~ /^(.{3})(.*)$/) {
+ $fields{$1} = [ split('', $2) ];
+ }
+ }
+
+ for my $f ( keys %fields) {
+ if ( @{$fields{$f}} ) {
+ for my $from_field ($incoming_r->field( $f )) {
+ for my $to_field ($incumbent_r->field( $f )) {
+ my @new_sf = map { ($_ => $from_field->subfield($_)) } @{$fields{$f}};
+ $to_field->add_subfields( @new_sf );
+ }
+ }
+ } else {
+ my @new_fields = map { $_->clone } $incoming_r->field( $f );
+ $incumbent_r->insert_fields_ordered( @new_fields );
+ }
+ }
+
+ $incumbent_xml = $incumbent_r->as_xml_record;
+ $incumbent_xml =~ s/^<\?.+?\?>$//mo;
+ $incumbent_xml =~ s/\n//sgo;
+ $incumbent_xml =~ s/>\s+</></sgo;
+
+ return $incumbent_xml;
+
+$_$ LANGUAGE PLPERLU;
+
CREATE OR REPLACE FUNCTION vandelay.strip_field ( xml TEXT, field TEXT ) RETURNS TEXT AS $_$
use MARC::Record;
use MARC::File::XML;
my $xml = shift;
+ my $r = MARC::Record->new_from_xml( $xml );
+
+ return $xml unless ($r);
+
my $field_spec = shift;
+ $field_spec =~ s/\s+//g;
- my $r = MARC::Record->new_from_xml( $xml );
- $r->delete_field( $_ ) for ( $r->field( $field_spec ) );
+ my @field_list = split(',' $field_spec);
+ my %fields;
+ for my $f (@field_list) {
+ if ($f =~ /^(.{3})(.*)$/) {
+ $fields{$1} = [ split('', $2) ];
+ }
+ }
+
+ for my $f ( keys %fields) {
+ if ( @{$fields{$f}} ) {
+ $_->delete_subfield(code => $fields{$f}) for ($r->field( $f ));
+ } else {
+ $r->delete_field( $_ ) for ( $r->field( $f ) );
+ }
+ }
+
$xml = $r->as_xml_record;
$xml =~ s/^<\?.+?\?>$//mo;
$xml =~ s/\n//sgo;
@@ -149,7 +215,14 @@
$_$ LANGUAGE PLPERLU;
+CREATE OR REPLACE FUNCTION vandelay.replace_field ( incumbent_xml TEXT, incoming_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+ SELECT vandelay.add_field( vandelay.strip_field( $1, $3), $2, $3 );
+$_$ LANGUAGE SQL;
+CREATE OR REPLACE FUNCTION vandelay.preserve_field ( incumbent_xml TEXT, incoming_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+ SELECT vandelay.add_field( vandelay.strip_field( $2, $3), $1, $3 );
+$_$ LANGUAGE SQL;
+
CREATE OR REPLACE FUNCTION vandelay.ingest_items ( import_id BIGINT, attr_def_id BIGINT ) RETURNS SETOF vandelay.import_item AS $$
DECLARE
More information about the open-ils-commits
mailing list