[open-ils-commits] r18654 - in branches/rel_1_6/Open-ILS/src/sql/Pg: . upgrade (miker)
svn at svn.open-ils.org
svn at svn.open-ils.org
Mon Nov 8 11:28:50 EST 2010
Author: miker
Date: 2010-11-08 11:28:47 -0500 (Mon, 08 Nov 2010)
New Revision: 18654
Added:
branches/rel_1_6/Open-ILS/src/sql/Pg/upgrade/0461.schema.vandelay.replace_field.sql
Modified:
branches/rel_1_6/Open-ILS/src/sql/Pg/012.schema.vandelay.sql
Log:
Backporting r18652: Teach vandelay.replace_field to be a little smarter by allowing simple cases of both replacing and regexp-testing the same subfield
Modified: branches/rel_1_6/Open-ILS/src/sql/Pg/012.schema.vandelay.sql
===================================================================
--- branches/rel_1_6/Open-ILS/src/sql/Pg/012.schema.vandelay.sql 2010-11-08 16:24:47 UTC (rev 18653)
+++ branches/rel_1_6/Open-ILS/src/sql/Pg/012.schema.vandelay.sql 2010-11-08 16:28:47 UTC (rev 18654)
@@ -188,7 +188,7 @@
$_$ LANGUAGE PLPERLU;
-CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT, force_add INT ) RETURNS TEXT AS $_$
use MARC::Record;
use MARC::File::XML (BinaryEncoding => 'UTF-8');
@@ -197,6 +197,7 @@
my $target_xml = shift;
my $source_xml = shift;
my $field_spec = shift;
+ my $force_add = shift || 0;
my $target_r = MARC::Record->new_from_xml( $target_xml );
my $source_r = MARC::Record->new_from_xml( $source_xml );
@@ -232,7 +233,7 @@
for my $from_field ($source_r->field( $f )) {
my @tos = $target_r->field( $f );
if (!@tos) {
- next if (exists($fields{$f}{match}));
+ next if (exists($fields{$f}{match}) and !$force_add);
my @new_fields = map { $_->clone } $source_r->field( $f );
$target_r->insert_fields_ordered( @new_fields );
} else {
@@ -260,10 +261,28 @@
$_$ LANGUAGE PLPERLU;
-CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
- SELECT vandelay.add_field( vandelay.strip_field( $1, $3), $2, $3 );
+CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+ SELECT vandelay.add_field( $1, $2 $3, 0 );
$_$ LANGUAGE SQL;
+CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+DECLARE
+ xml_output TEXT;
+BEGIN
+ xml_output := vandelay.strip_field( target_xml, field);
+
+ IF xml_output <> target_xml AND field ~ E'~' THEN
+ -- we removed something, and there was a regexp restriction in the field definition, so proceed
+ xml_output := vandelay.add_field( xml_output, source_xml, field, 1 );
+ ELSIF field !~ E'~' THEN
+ -- No regexp restriction, add the field
+ xml_output := vandelay.add_field( xml_output, source_xml, field, 0 );
+ END IF;
+
+ RETURN xml_output;
+END;
+$_$ LANGUAGE PLPGSQL;
+
CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_xml TEXT, source_xml TEXT, add_rule TEXT, replace_preserve_rule TEXT, strip_rule TEXT ) RETURNS TEXT AS $_$
SELECT vandelay.replace_field( vandelay.add_field( vandelay.strip_field( $1, $5) , $2, $3 ), $2, $4);
$_$ LANGUAGE SQL;
Copied: branches/rel_1_6/Open-ILS/src/sql/Pg/upgrade/0461.schema.vandelay.replace_field.sql (from rev 18652, trunk/Open-ILS/src/sql/Pg/upgrade/0461.schema.vandelay.replace_field.sql)
===================================================================
--- branches/rel_1_6/Open-ILS/src/sql/Pg/upgrade/0461.schema.vandelay.replace_field.sql (rev 0)
+++ branches/rel_1_6/Open-ILS/src/sql/Pg/upgrade/0461.schema.vandelay.replace_field.sql 2010-11-08 16:28:47 UTC (rev 18654)
@@ -0,0 +1,101 @@
+
+BEGIN;
+
+INSERT INTO config.upgrade_log (version) VALUES ('0461'); --miker
+
+CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT, force_add INT ) RETURNS TEXT AS $_$
+
+ use MARC::Record;
+ use MARC::File::XML (BinaryEncoding => 'UTF-8');
+ use strict;
+
+ my $target_xml = shift;
+ my $source_xml = shift;
+ my $field_spec = shift;
+ my $force_add = shift || 0;
+
+ my $target_r = MARC::Record->new_from_xml( $target_xml );
+ my $source_r = MARC::Record->new_from_xml( $source_xml );
+
+ return $target_xml unless ($target_r && $source_r);
+
+ my @field_list = split(',', $field_spec);
+
+ my %fields;
+ for my $f (@field_list) {
+ $f =~ s/^\s*//; $f =~ s/\s*$//;
+ if ($f =~ /^(.{3})(\w*)(?:\[([^]]*)\])?$/) {
+ my $field = $1;
+ $field =~ s/\s+//;
+ my $sf = $2;
+ $sf =~ s/\s+//;
+ my $match = $3;
+ $match =~ s/^\s*//; $match =~ s/\s*$//;
+ $fields{$field} = { sf => [ split('', $sf) ] };
+ if ($match) {
+ my ($msf,$mre) = split('~', $match);
+ if (length($msf) > 0 and length($mre) > 0) {
+ $msf =~ s/^\s*//; $msf =~ s/\s*$//;
+ $mre =~ s/^\s*//; $mre =~ s/\s*$//;
+ $fields{$field}{match} = { sf => $msf, re => qr/$mre/ };
+ }
+ }
+ }
+ }
+
+ for my $f ( keys %fields) {
+ if ( @{$fields{$f}{sf}} ) {
+ for my $from_field ($source_r->field( $f )) {
+ my @tos = $target_r->field( $f );
+ if (!@tos) {
+ next if (exists($fields{$f}{match}) and !$force_add);
+ my @new_fields = map { $_->clone } $source_r->field( $f );
+ $target_r->insert_fields_ordered( @new_fields );
+ } else {
+ for my $to_field (@tos) {
+ if (exists($fields{$f}{match})) {
+ next unless (grep { $_ =~ $fields{$f}{match}{re} } $to_field->subfield($fields{$f}{match}{sf}));
+ }
+ my @new_sf = map { ($_ => $from_field->subfield($_)) } @{$fields{$f}{sf}};
+ $to_field->add_subfields( @new_sf );
+ }
+ }
+ }
+ } else {
+ my @new_fields = map { $_->clone } $source_r->field( $f );
+ $target_r->insert_fields_ordered( @new_fields );
+ }
+ }
+
+ $target_xml = $target_r->as_xml_record;
+ $target_xml =~ s/^<\?.+?\?>$//mo;
+ $target_xml =~ s/\n//sgo;
+ $target_xml =~ s/>\s+</></sgo;
+
+ return $target_xml;
+
+$_$ LANGUAGE PLPERLU;
+
+CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+ SELECT vandelay.add_field( $1, $2 $3, 0 );
+$_$ LANGUAGE SQL;
+
+CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+DECLARE
+ xml_output TEXT;
+BEGIN
+ xml_output := vandelay.strip_field( target_xml, field);
+
+ IF xml_output <> target_xml AND field ~ E'~' THEN
+ -- we removed something, and there was a regexp restriction in the field definition, so proceed
+ xml_output := vandelay.add_field( xml_output, source_xml, field, 1 );
+ ELSIF field !~ E'~' THEN
+ -- No regexp restriction, add the field
+ xml_output := vandelay.add_field( xml_output, source_xml, field, 0 );
+ END IF;
+
+ RETURN xml_output;
+END;
+$_$ LANGUAGE PLPGSQL;
+
+COMMIT;
More information about the open-ils-commits
mailing list