Guest User

Untitled

a guest
Apr 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION spot_items_trig () RETURNS TRIGGER AS $end$
  2.  
  3. # get important data out of the TD dict
  4. record = TD["new"]
  5.  
  6. if TD["event"] == "INSERT":
  7.  
  8. return None
  9.  
  10. # handle UPDATE
  11. if record["stat_active"] or record["stat_in_auction"]:
  12.  
  13. # item is active or in auction, so make sure the zone is active
  14. plan = plpy.prepare("""
  15. UPDATE
  16. adult.zones
  17. SET
  18. stat_active = true
  19. WHERE
  20. zone_id = $1
  21. """,
  22. ["int8"])
  23.  
  24. plpy.execute(plan, [record["zone_id"]])
  25.  
  26. else:
  27.  
  28. # item isn't active and isn't in an auction, so if the rest of the zone
  29. # items aren't active and aren't in an auction, we can set the zone to inactive
  30. plan = plpy.prepare("""
  31. SELECT
  32. 1
  33. FROM
  34. adult.gallery_spot_items
  35. WHERE
  36. zone_id = $1
  37. AND
  38. (stat_active OR stat_in_auction)
  39. """,
  40. ["int8"])
  41.  
  42. items = plpy.execute(plan, [record["zone_id"]])
  43.  
  44. if len(items) == 0:
  45.  
  46. # no other active or auctioned items, let's inactivate the zone
  47. plan = plpy.prepare("""
  48. UPDATE
  49. adult.zones
  50. SET
  51. stat_active = false
  52. WHERE
  53. zone_id = $1
  54. """,
  55. ["int8"])
  56.  
  57. plpy.execute(plan, [record["zone_id"]])
  58.  
  59. return None
  60.  
  61. $end$ LANGUAGE plpythonu;
Add Comment
Please, Sign In to add comment