Advertisement
Dyrcona

cwmars.delete_acnp.sql

Apr 3rd, 2024
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Copyright © 2022 C/W MARS, Inc.
  2. -- Author: Jason Stephenson <jstephenson@cwmars.org>
  3. --
  4. -- This program is free software; you can redistribute it and/or modify
  5. -- it under the terms of the GNU General Public License as published by
  6. -- the Free Software Foundation; either version 2 of the License, or
  7. -- (at your option) any later version.
  8. --
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. -- GNU General Public License for more details.
  13.  
  14. CREATE SCHEMA IF NOT EXISTS cwmars;
  15.  
  16. CREATE TYPE cwmars.delete_acnp_result_t AS (
  17.     id INTEGER,
  18.     count INTEGER
  19. );
  20.  
  21. CREATE FUNCTION cwmars.delete_acnp(acnp INTEGER, new_acnp INTEGER DEFAULT -1)
  22. RETURNS cwmars.delete_acnp_result_t  AS
  23. $$
  24. DECLARE
  25.     call_number_with_prefix asset.call_number%ROWTYPE;
  26.     call_number_with_new_prefix asset.call_number%ROWTYPE;
  27.     result cwmars.delete_acnp_result_t;
  28. BEGIN
  29.  
  30. result.count := 0;
  31. result.id := acnp;
  32.  
  33. FOR call_number_with_prefix IN
  34.     SELECT *
  35.     FROM asset.call_number
  36.     WHERE prefix = acnp
  37. LOOP
  38.     BEGIN
  39.         UPDATE asset.call_number
  40.         SET prefix = new_acnp
  41.         WHERE id = call_number_with_prefix.id;
  42.         -- If this works with no exception, then all is well.
  43.     EXCEPTION
  44.         WHEN unique_violation THEN
  45.             -- Find the corresponding call number that already exists
  46.             -- with the new_acnp
  47.             SELECT *
  48.             FROM asset.call_number
  49.             WHERE prefix = new_acnp
  50.             AND label = call_number_with_prefix.label
  51.             AND suffix = call_number_with_prefix.suffix
  52.             AND record = call_number_with_prefix.record
  53.             AND owning_lib = call_number_with_prefix.owning_lib
  54.             AND deleted = FALSE
  55.             INTO call_number_with_new_prefix;
  56.  
  57.             -- asset.*
  58.             UPDATE asset.uri_call_number_map
  59.             SET call_number = call_number_with_new_prefix.id
  60.             WHERE call_number = call_number_with_prefix.id;
  61.  
  62.             UPDATE asset.copy
  63.             SET call_number = call_number_with_new_prefix.id
  64.             WHERE call_number = call_number_with_prefix.id;
  65.  
  66.             -- container.*
  67.             UPDATE container.call_number_bucket_item
  68.             SET target_call_number = call_number_with_new_prefix.id
  69.             WHERE target_call_number = call_number_with_prefix.id;
  70.  
  71.             -- serial.*
  72.             UPDATE serial.distribution
  73.             SET bind_call_number = call_number_with_new_prefix.id
  74.             WHERE bind_call_number = call_number_with_prefix.id;
  75.  
  76.             UPDATE serial.distribution
  77.             SET receive_call_number = call_number_with_new_prefix.id
  78.             WHERE receive_call_number = call_number_with_prefix.id;
  79.  
  80.             UPDATE serial.unit
  81.             SET call_number = call_number_with_new_prefix.id
  82.             WHERE call_number = call_number_with_prefix.id;
  83.  
  84.             -- The call number with the old prefix is not needed anymore
  85.             UPDATE asset.call_number
  86.             SET prefix = new_acnp, deleted = TRUE
  87.             WHERE id = call_number_with_prefix.id;
  88.     END;
  89.     result.count := result.count + 1;
  90. END LOOP;
  91.  
  92. -- Don't need the old acnp
  93. DELETE FROM asset.call_number_prefix
  94. WHERE id = acnp;
  95.  
  96. RETURN result;
  97. END;
  98. $$
  99. LANGUAGE plpgsql;
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement