Advertisement
roganhamby

Weeding Postgres Function

Dec 22nd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. DROP FUNCTION rogan.weeding(INT);
  3.  
  4.  
  5. CREATE OR REPLACE FUNCTION rogan.weeding(shelf_id INT)
  6.   RETURNS TABLE (
  7.         "count"                                 INT8,
  8.         barcode                                 TEXT,
  9.         "shelving location"                     TEXT,
  10.         "call number"                           TEXT,
  11.         title                                   TEXT,
  12.         "last circulation"                      DATE
  13.         ) AS
  14. $$
  15. BEGIN
  16.  
  17. DECLARE     weeding_age     INTERVAL,
  18.             dispersal_max   INTEGER,
  19.             weed_org        INTEGER
  20.  
  21.  
  22.  
  23. SELECT INTO weed_org acl.owning_lib FROM asset.copy_location acl WHERE acl.id = shelf_id;
  24.  
  25.  
  26. SELECT INTO weeding_age rous.value
  27. FROM rogan.org_unit_setting rous
  28. JOIN actor.org_unit_ancestors_distance(weed_org) d ON (d.id = rous.org_unit)
  29. WHERE NAME = 'weeding_age'
  30. ORDER BY d.distance
  31. LIMIT 1
  32. ;
  33.  
  34. SELECT INTO dispersal_max rous.value
  35. FROM rogan.org_unit_setting rous
  36. JOIN actor.org_unit_ancestors_distance(weed_org) d ON (d.id = rous.org_unit)
  37. WHERE NAME = 'dispersal_max'
  38. ORDER BY d.distance
  39. LIMIT 1
  40. ;
  41.  
  42.  
  43. create temp table dispersal_counts on commit drop as
  44. select count(rdc.id) as d_count, rdc.copy_id
  45. from rogan.dispersed_copies rdc
  46. group by 2
  47. ;
  48.  
  49.  
  50. CREATE TEMP TABLE weeds ON COMMIT DROP AS
  51. SELECT COUNT(acirc.id) as circ_count, ac.id as copy_id, ac.barcode AS barcode, acl.NAME AS shelving_location,
  52. acn.label AS call_number, msr.title, date(MAX(acirc.xact_start)) AS last_circulation
  53. FROM asset.COPY ac
  54. JOIN asset.copy_location acl ON ac.LOCATION = acl.id
  55. LEFT JOIN ACTION.circulation acirc ON acirc.target_copy = ac.id
  56. JOIN asset.call_number acn ON acn.id = ac.call_number
  57. JOIN reporter.materialized_simple_record msr ON msr.id = acn.record
  58. JOIN actor.org_unit child ON child.id = ac.circ_lib
  59. WHERE acl.id = shelf_id
  60. AND acirc.xact_start IS NULL
  61. AND date(ac.create_date) <= NOW() - weeding_age
  62. AND ac.deleted = FALSE
  63. AND ac.status = 0
  64. GROUP BY 2, 3, 4, 5, 6
  65. UNION ALL
  66. SELECT COUNT(acirc.id) as circ_count, ac.id as copy_id, ac.barcode AS barcode, acl.NAME AS shelving_location,
  67. acn.label AS call_number, msr.title, date(MAX(acirc.xact_start)) AS last_circulation
  68. FROM asset.COPY ac
  69. JOIN asset.copy_location acl ON ac.LOCATION = acl.id
  70. LEFT JOIN ACTION.circulation acirc ON acirc.target_copy = ac.id
  71. JOIN asset.call_number acn ON acn.id = ac.call_number
  72. JOIN reporter.materialized_simple_record msr ON msr.id = acn.record
  73. JOIN actor.org_unit child ON child.id = ac.circ_lib
  74. WHERE acl.id = shelf_id
  75. AND date(ac.create_date) <= NOW() - weeding_age
  76. AND ac.deleted = FALSE
  77. AND ac.status = 0
  78. GROUP BY 2, 3, 4, 5, 6
  79. HAVING MAX(acirc.xact_start) < NOW() - weeding_age
  80. ORDER BY 4
  81. ;
  82.  
  83. delete from weeds where copy_id in
  84.     ( select w.copy_id
  85.     from weeds w
  86.     join dispersal_counts dc on dc.copy_id = w.copy_id
  87.     where dc.d_count >= dispersal_max);
  88.  
  89.  
  90. RETURN QUERY
  91. SELECT circ_count, barcode, shelving_location, call_number, title, last_circulation
  92. FROM weeds;
  93.  
  94.  
  95. END;
  96. $$  
  97. LANGUAGE plpgsql;
  98.  
  99. -- adult fiction
  100. SELECT * FROM extend_reporter.weeding(1002);
  101. -- ready reference, much better on test server
  102. SELECT * FROM extend_reporter.weeding(1036);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement