Guest User

Untitled

a guest
Feb 13th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import psycopg2
  5.  
  6. try:
  7. #~ conn = psycopg2.connect("dbname='iii' host='sierra-db.library.org' port=1032 user='USERNAME_HERE' password='PASSWORD_HERE'")
  8.  
  9. except:
  10. print("unable to connect to the database")
  11. conn.close()
  12. conn = None
  13. sys.exit(1)
  14.  
  15. # sql statement (in the heredoc type style)
  16. sql = """\
  17. DROP TABLE IF EXISTS temp_item_lvl_holds;
  18. CREATE TEMP TABLE temp_item_lvl_holds AS
  19. SELECT
  20. h.id,
  21. (
  22. SELECT
  23. string_agg(v.field_content, ', ' order by v.occ_num)
  24. FROM
  25. sierra_view.varfield as v
  26. WHERE
  27. v.record_id = r.id
  28. AND v.varfield_type_code = 'b'
  29. ) as item_barcodes,
  30. (
  31. SELECT
  32. string_agg(v.field_content, ', ' order by v.occ_num)
  33. FROM
  34. sierra_view.varfield as v
  35. WHERE
  36. v.record_id = h.patron_record_id
  37. AND v.varfield_type_code = 'b'
  38. ) as patron_barcodes,
  39. pr.ptype_code::int as ptype,
  40. -- l.*,
  41. -- r.id,
  42. r.record_type_code || r.record_num || 'a' as item_record_num,
  43. i.item_status_code,
  44. c.checkout_gmt,
  45. c.loanrule_code_num,
  46. vr.record_type_code || vr.record_num || 'a' as vol_record_num,
  47. br.record_type_code || br.record_num || 'a' as bib_record_num,
  48. v.field_content as volume_number,
  49. h.placed_gmt,
  50. pn.last_name || ', ' || pn.first_name || coalesce (' ' || NULLIF(pn.middle_name, ''), '') as full_name,
  51. h.pickup_location_code,
  52. -- vr.id,
  53. p.best_title,
  54. p.best_author
  55. -- i.item_status_code,
  56. -- date_part('day', NOW()::timestamp - c.checkout_gmt::timestamp) as days_checked_out,
  57. -- h.*
  58.  
  59. FROM
  60. sierra_view.hold as h
  61.  
  62. -- this join will exclude anything that isn't a item level hold
  63. JOIN
  64. sierra_view.record_metadata as r
  65. ON
  66. (r.id = h.record_id)
  67. AND (r.record_type_code || r.campus_code = 'i')
  68.  
  69. JOIN
  70. sierra_view.item_record as i
  71. ON
  72. i.record_id = r.id
  73.  
  74. LEFT OUTER JOIN
  75. sierra_view.checkout as c
  76. ON
  77. c.item_record_id = r.id
  78.  
  79. LEFT OUTER JOIN
  80. sierra_view.phrase_entry as e
  81. ON
  82. e.record_id = r.id
  83. AND e.index_tag = 'b'
  84.  
  85. LEFT OUTER JOIN
  86. sierra_view.volume_record_item_record_link as l
  87. ON
  88. l.item_record_id = r.id
  89.  
  90. LEFT OUTER JOIN
  91. sierra_view.record_metadata as vr
  92. ON
  93. vr.id = l.volume_record_id
  94.  
  95. LEFT OUTER JOIN
  96. sierra_view.varfield AS v
  97. ON
  98. (v.record_id = vr.id) AND (v.varfield_type_code = 'v')
  99.  
  100. LEFT OUTER JOIN
  101. sierra_view.bib_record_item_record_link as bl
  102. ON
  103. bl.item_record_id = r.id
  104.  
  105. LEFT OUTER JOIN
  106. sierra_view.record_metadata as br
  107. ON
  108. br.id = bl.bib_record_id
  109.  
  110. LEFT OUTER JOIN
  111. sierra_view.bib_record_property as p
  112. ON
  113. p.bib_record_id = br.id
  114.  
  115. LEFT OUTER JOIN
  116. sierra_view.patron_record as pr
  117. ON
  118. pr.record_id = h.patron_record_id
  119.  
  120. LEFT OUTER JOIN
  121. sierra_view.patron_record_fullname as pn
  122. ON
  123. pn.patron_record_id = h.patron_record_id
  124.  
  125.  
  126. WHERE
  127. -- item is not a circulating/active item OR item is checked out
  128. (
  129. i.item_status_code NOT IN ('t', '!', '(') -- might need to include status '-' here as well
  130. OR (
  131. i.item_status_code = '-'
  132. AND c.checkout_gmt IS NOT NULL
  133. )
  134. )
  135. -- item is on shelf not checked out
  136. OR (
  137. i.item_status_code = '-'
  138. AND c.checkout_gmt IS NULL
  139. );
  140. ---
  141.  
  142.  
  143. ---
  144. -- create the table of item level holds on shelf not checked out
  145. DROP TABLE IF EXISTS temp_item_lvl_holds_on_shelf;
  146. CREATE TEMP TABLE temp_item_lvl_holds_on_shelf AS
  147. SELECT
  148. *
  149.  
  150. FROM
  151. temp_item_lvl_holds as i
  152.  
  153. WHERE
  154. -- item is on shelf
  155. (
  156. i.item_status_code = '-'
  157. AND i.checkout_gmt IS NULL
  158. );
  159. ---
  160.  
  161.  
  162. ---
  163. -- remove the on shelf not checked out holds
  164. DELETE FROM
  165. temp_item_lvl_holds as h
  166.  
  167. WHERE h.id IN(
  168. SELECT
  169. t.id
  170.  
  171. FROM
  172. temp_item_lvl_holds_on_shelf as t
  173. )
  174. ;
  175. ---
  176.  
  177.  
  178. ---
  179. -- create the table of item level holds where item is not circulating / item checked out
  180. DROP TABLE IF EXISTS temp_item_lvl_holds_non_or_circ_checked_out;
  181. CREATE TEMP TABLE temp_item_lvl_holds_non_or_circ_checked_out AS
  182. SELECT
  183. *
  184.  
  185. FROM
  186. temp_item_lvl_holds as i
  187.  
  188. WHERE
  189. (
  190. i.item_status_code NOT IN ('t', '!', '(') -- might need to include status '-' here as well
  191. OR (
  192. i.item_status_code = '-'
  193. AND i.checkout_gmt IS NOT NULL
  194. )
  195. );
  196. ---
  197.  
  198.  
  199. ---
  200. -- we shouldn't need to do this, since the temp_item_lvl_holds table should now be empty if the query was correct
  201. DELETE FROM
  202. temp_item_lvl_holds as h
  203.  
  204. WHERE h.id IN(
  205. SELECT
  206. t.id
  207.  
  208. FROM
  209. temp_item_lvl_holds_non_or_circ_checked_out as t
  210. );
  211. ---
  212. """
  213.  
  214. # create a new cursor and execute
  215. try:
  216. cur = conn.cursor()
  217. cur.execute(sql)
  218.  
  219. except:
  220. conn.close()
  221. conn = None
  222. cur = None
  223. print("error connecting or running query")
  224. sys.exit(1)
  225.  
  226.  
  227.  
  228. #~ for record in cur:
  229. #~ print(record)
  230.  
  231. if cur.rowcount != 0:
  232. print("query successful ... producing results")
  233. else:
  234. print("something went horribly wrong!")
  235. cur.close()
  236. conn.close()
  237. conn = None
  238. cur = None
  239. sys.exit(1)
  240.  
  241.  
  242.  
  243. # close our connections
  244. cur.close()
  245. conn.close()
  246. conn = None
  247. cur = None
Add Comment
Please, Sign In to add comment