X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=sql%2Fbase%2Fbase.sql;h=84c35f326478583da2dde0019cd47b846e3b02e3;hp=e6dc71ff91327adb03ff1f764f11a8f881673d39;hb=8783c6c281d0aca08c1b88d1067d80a24a513696;hpb=53a11b628416da91c21ecd98f868508f54e894ee diff --git a/sql/base/base.sql b/sql/base/base.sql index e6dc71f..84c35f3 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -117,7 +117,7 @@ CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$ END; PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.config;' ); PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);' ); - PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''production_tables'', ''asset.call_number,asset.call_number_prefix,asset.call_number_suffix,asset.copy_location,asset.copy,asset.stat_cat,asset.stat_cat_entry,asset.stat_cat_entry_copy_map,asset.copy_note,actor.usr,actor.card,actor.usr_address,actor.stat_cat,actor.stat_cat_entry,actor.stat_cat_entry_usr_map,actor.usr_note,actor.usr_standing_penalty,actor.usr_setting,action.circulation,action.hold_request,action.hold_notification,action.hold_request_note,action.hold_transit_copy,action.transit_copy,money.grocery,money.billing,money.cash_payment,money.forgive_payment,acq.provider,acq.provider_address,acq.provider_note,acq.provider_contact,acq.provider_contact_address,acq.fund,acq.fund_allocation,acq.fund_tag,acq.fund_tag_map,acq.funding_source,acq.funding_source_credit,acq.lineitem,acq.purchase_order,acq.po_item,acq.invoice,acq.invoice_item,acq.invoice_entry,acq.lineitem_detail,acq.fund_debit,acq.fund_transfer,acq.po_note,config.circ_matrix_matchpoint,config.circ_matrix_limit_set_map,config.hold_matrix_matchpoint,asset.copy_tag,asset.copy_tag_copy_map,config.copy_tag_type,serial.item,serial.item_note,serial.record_entry'' );' ); + PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''production_tables'', ''asset.call_number,asset.call_number_prefix,asset.call_number_suffix,asset.copy_location,asset.copy,asset.stat_cat,asset.stat_cat_entry,asset.stat_cat_entry_copy_map,asset.copy_note,actor.usr,actor.card,actor.usr_address,actor.stat_cat,actor.stat_cat_entry,actor.stat_cat_entry_usr_map,actor.usr_note,actor.usr_standing_penalty,actor.usr_setting,action.circulation,action.hold_request,action.hold_notification,action.hold_request_note,action.hold_transit_copy,action.transit_copy,money.grocery,money.billing,money.cash_payment,money.forgive_payment,acq.provider,acq.provider_address,acq.provider_note,acq.provider_contact,acq.provider_contact_address,acq.fund,acq.fund_allocation,acq.fund_tag,acq.fund_tag_map,acq.funding_source,acq.funding_source_credit,acq.lineitem,acq.purchase_order,acq.po_item,acq.invoice,acq.invoice_item,acq.invoice_entry,acq.lineitem_detail,acq.fund_debit,acq.fund_transfer,acq.po_note,config.circ_matrix_matchpoint,config.circ_matrix_limit_set_map,config.hold_matrix_matchpoint,asset.copy_tag,asset.copy_tag_copy_map,config.copy_tag_type,serial.item,serial.item_note,serial.record_entry,biblio.record_entry'' );' ); PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''country_code'', ''USA'' );' ); PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;' ); PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);' ); @@ -607,6 +607,22 @@ CREATE OR REPLACE FUNCTION migration_tools.parse_out_address (TEXT) RETURNS TEXT END; $$ LANGUAGE PLPGSQL STRICT VOLATILE; +CREATE OR REPLACE FUNCTION migration_tools.parse_out_address2 (TEXT) RETURNS TEXT AS $$ + my ($address) = @_; + + use Geo::StreetAddress::US; + + my $a = Geo::StreetAddress::US->parse_location($address); + + return [ + "$a->{number} $a->{prefix} $a->{street} $a->{type} $a->{suffix}" + ,"$a->{sec_unit_type} $a->{sec_unit_num}" + ,$a->{city} + ,$a->{state} + ,$a->{zip} + ]; +$$ LANGUAGE PLPERLU STABLE; + CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$ DECLARE n TEXT := o; @@ -1082,6 +1098,51 @@ CREATE OR REPLACE FUNCTION migration_tools.add_code39mod43_checkdigit (TEXT) RET return $barcode . $checkdigit; $$ LANGUAGE PLPERLU STRICT STABLE; +-- add_mod16_checkdigit +-- $barcode source barcode +-- +-- https://www.activebarcode.com/codes/checkdigit/modulo16.html + +CREATE OR REPLACE FUNCTION migration_tools.add_mod16_checkdigit (TEXT) RETURNS TEXT AS $$ + my $barcode = shift; + + my @digits = split //, $barcode; + my $total = 0; + foreach $digit (@digits) { + if ($digit =~ /[0-9]/) { $total += $digit; + } elsif ($digit eq '-') { $total += 10; + } elsif ($digit eq '$') { $total += 11; + } elsif ($digit eq ':') { $total += 12; + } elsif ($digit eq '/') { $total += 13; + } elsif ($digit eq '.') { $total += 14; + } elsif ($digit eq '+') { $total += 15; + } elsif ($digit eq 'A') { $total += 16; + } elsif ($digit eq 'B') { $total += 17; + } elsif ($digit eq 'C') { $total += 18; + } elsif ($digit eq 'D') { $total += 19; + } else { die "invalid digit <$digit>"; + } + } + my $remainder = $total % 16; + my $difference = 16 - $remainder; + my $checkdigit; + if ($difference < 10) { $checkdigit = $difference; + } elsif ($difference == 10) { $checkdigit = '-'; + } elsif ($difference == 11) { $checkdigit = '$'; + } elsif ($difference == 12) { $checkdigit = ':'; + } elsif ($difference == 13) { $checkdigit = '/'; + } elsif ($difference == 14) { $checkdigit = '.'; + } elsif ($difference == 15) { $checkdigit = '+'; + } elsif ($difference == 16) { $checkdigit = 'A'; + } elsif ($difference == 17) { $checkdigit = 'B'; + } elsif ($difference == 18) { $checkdigit = 'C'; + } elsif ($difference == 19) { $checkdigit = 'D'; + } else { die "error calculating checkdigit"; + } + + return $barcode . $checkdigit; +$$ LANGUAGE PLPERLU STRICT STABLE; + CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEXT AS $$ DECLARE phone TEXT := $1; @@ -2928,7 +2989,9 @@ CREATE OR REPLACE FUNCTION migration_tools.handle_not_migrate (TEXT,TEXT) RETURN EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' || ' SET x_migrate = CASE' || ' WHEN BTRIM(desired_not_migrate) = ''TRUE'' THEN FALSE' + || ' WHEN BTRIM(desired_not_migrate) = ''DNM'' THEN FALSE' || ' WHEN BTRIM(desired_not_migrate) = ''FALSE'' THEN TRUE' + || ' WHEN BTRIM(desired_not_migrate) = ''Migrate'' THEN TRUE' || ' WHEN BTRIM(desired_not_migrate) = '''' THEN TRUE' || ' END'; @@ -3239,8 +3302,8 @@ END $$ LANGUAGE plpgsql; -- alternate adding subfield 9 function in that it adds them to existing tags where the 856$u matches a correct value only -DROP FUNCTION IF EXISTS add_sf9(TEXT,TEXT,TEXT); -CREATE OR REPLACE FUNCTION add_sf9(marc TEXT, partial_u TEXT, new_9 TEXT) +DROP FUNCTION IF EXISTS migration_tools.add_sf9(TEXT,TEXT,TEXT); +CREATE OR REPLACE FUNCTION migration_tools.add_sf9(marc TEXT, partial_u TEXT, new_9 TEXT) RETURNS TEXT LANGUAGE plperlu AS $function$ @@ -3288,8 +3351,8 @@ return $marc_xml->as_xml_record(); $function$; -DROP FUNCTION IF EXISTS add_sf9(INTEGER, TEXT, TEXT); -CREATE OR REPLACE FUNCTION add_sf9(bib_id INTEGER, target_u_text TEXT, sf9_text TEXT) +DROP FUNCTION IF EXISTS migration_tools.add_sf9(BIGINT, TEXT, TEXT, REGCLASS); +CREATE OR REPLACE FUNCTION migration_tools.add_sf9(bib_id BIGINT, target_u_text TEXT, sf9_text TEXT, bib_table REGCLASS) RETURNS BOOLEAN AS $BODY$ DECLARE @@ -3298,14 +3361,15 @@ DECLARE r BOOLEAN; BEGIN - SELECT marc FROM biblio.record_entry WHERE id = bib_id INTO source_xml; + EXECUTE 'SELECT marc FROM ' || bib_table || ' WHERE id = ' || bib_id INTO source_xml; SELECT add_sf9(source_xml, target_u_text, sf9_text) INTO new_xml; r = FALSE; + new_xml = '$_$' || new_xml || '$_$'; IF new_xml != source_xml THEN - UPDATE biblio.record_entry SET marc = new_xml WHERE id = bib_id; + EXECUTE 'UPDATE ' || bib_table || ' SET marc = ' || new_xml || ' WHERE id = ' || bib_id; r = TRUE; END IF; @@ -3356,12 +3420,12 @@ CREATE OR REPLACE FUNCTION migration_tools.handle_item_barcode (TEXT,TEXT,TEXT,T IF btrim_desired THEN EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' - || ' SET x_item = id FROM asset_copy_legacy b' + || ' SET x_item = b.id FROM asset_copy_legacy b' || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name) || ') = BTRIM(b.' || quote_ident(main_column_name) || ')'; ELSE EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' - || ' SET x_item = id FROM asset_copy_legacy b' + || ' SET x_item = b.id FROM asset_copy_legacy b' || ' WHERE a.' || quote_ident(foreign_column_name) || ' = b.' || quote_ident(main_column_name); END IF; @@ -3417,12 +3481,12 @@ CREATE OR REPLACE FUNCTION migration_tools.handle_user_barcode (TEXT,TEXT,TEXT,T IF btrim_desired THEN EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' - || ' SET x_user = id FROM actor_usr_legacy b' + || ' SET x_user = b.id FROM actor_usr_legacy b' || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name) || ') = BTRIM(b.' || quote_ident(main_column_name) || ')'; ELSE EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' - || ' SET x_user = id FROM actor_usr_legacy b' + || ' SET x_user = b.id FROM actor_usr_legacy b' || ' WHERE a.' || quote_ident(foreign_column_name) || ' = b.' || quote_ident(main_column_name); END IF; @@ -3758,3 +3822,46 @@ CREATE OR REPLACE FUNCTION migration_tools.handle_asset_sc_and_sce (TEXT,TEXT,TE END; $$ LANGUAGE PLPGSQL STRICT VOLATILE; + +DROP FUNCTION IF EXISTS migration_tools.btrim_lcolumns(TEXT,TEXT); +CREATE OR REPLACE FUNCTION migration_tools.btrim_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN + LANGUAGE plpgsql +AS $function$ +DECLARE + c_name TEXT; +BEGIN + + FOR c_name IN SELECT column_name FROM information_schema.columns WHERE + table_name = t_name + AND table_schema = s_name + AND (data_type='text' OR data_type='character varying') + AND column_name like 'l_%' + LOOP + EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = BTRIM(' || c_name || ')'); + END LOOP; + + RETURN TRUE; +END +$function$; + +DROP FUNCTION IF EXISTS migration_tools.null_empty_lcolumns(TEXT,TEXT); +CREATE OR REPLACE FUNCTION migration_tools.null_empty_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN + LANGUAGE plpgsql +AS $function$ +DECLARE + c_name TEXT; +BEGIN + + FOR c_name IN SELECT column_name FROM information_schema.columns WHERE + table_name = t_name + AND table_schema = s_name + AND (data_type='text' OR data_type='character varying') + AND column_name like 'l_%' + LOOP + EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = NULL WHERE ' || c_name || ' = '''' '); + END LOOP; + + RETURN TRUE; +END +$function$; +