Advertisement
cmptrwz

Populate Bucket

Jul 18th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION evergreen.mvlc_populate_bucket(inbucket integer, root_ou integer, checkattrs hstore[], time_period interval DEFAULT '50 years'::interval, maxbibs integer DEFAULT 10000) RETURNS VOID AS
  2. $FUNC$
  3. DECLARE
  4. BEGIN
  5. -- Does the bookbag exist?
  6. PERFORM * FROM container.biblio_record_entry_bucket WHERE id = inbucket;
  7. IF NOT FOUND THEN
  8. RETURN;
  9. END IF;
  10. -- Does the OU exist?
  11. PERFORM * FROM actor.org_unit WHERE id = root_ou;
  12. IF NOT FOUND THEN
  13. RETURN;
  14. END IF;
  15. -- Remove old bucket items
  16. DELETE FROM container.biblio_record_entry_bucket_item WHERE bucket = inbucket;
  17. -- Populate the bucket
  18. INSERT INTO container.biblio_record_entry_bucket_item (bucket, target_biblio_record_entry)
  19. SELECT inbucket, mra.id
  20. FROM metabib.record_attr mra
  21. JOIN asset.call_number acn ON mra.id = acn.record
  22. JOIN asset.copy acp ON acp.call_number = acn.id
  23. JOIN (SELECT unnest(checkattrs) as inattrs) x ON true
  24. WHERE attrs @> inattrs
  25. AND acp.circ_lib IN (SELECT id FROM actor.org_unit_descendants(root_ou))
  26. GROUP BY mra.id
  27. HAVING AGE(MIN(acp.active_date)) < time_period
  28. ORDER BY MIN(acp.active_date) DESC
  29. LIMIT maxbibs;
  30. END;
  31. $FUNC$ LANGUAGE plpgsql;
  32.  
  33. -- Example usage
  34. SELECT evergreen.mvlc_populate_bucket(208942, 1, ARRAY['item_type=>a'::hstore], '30 days'::INTERVAL);
  35. SELECT evergreen.mvlc_populate_bucket(208943, 1, ARRAY['item_type=>g'::hstore], '30 days'::INTERVAL);
  36. SELECT evergreen.mvlc_populate_bucket(208944, 1, ARRAY['item_type=>a, item_form=>d'::hstore], '30 days'::INTERVAL);
  37. SELECT evergreen.mvlc_populate_bucket(208945, 1, ARRAY['item_type=>j'::hstore], '30 days'::INTERVAL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement