my ($ctes, $joins);
if ($ctes_needed >= 1) {
# For our first auxiliary query, the question we seek to answer is, "has
- # our copy been circulating away from home too long?" Two parts to
- # answer this question.
+ # our copy been circulating away from home too long?"
#
- # part 1: Have their been no checkouts at the copy's circ_lib since the
+ # Have there been no checkouts at the copy's circ_lib since the
# beginning of our go-home interval?
- # part 2: Was the last transit to affect our copy before the beginning
- # of our go-home interval an outbound transit? i.e. away from circ-lib
# [We use sprintf because the outer function that's going to send one
# big query through DBI is blind to our process of dynamically building
circ.target_copy = %d AND
circ.circ_lib = %d AND
circ.xact_start >= NOW() - go_home_interval.value
- ) IS NULL AND (
- -- part 2
- SELECT COALESCE(atc.dest <> %d, TRUE)
- FROM go_home_interval
- LEFT JOIN action.transit_copy atc ON (
- atc.id = (
- SELECT MAX(id) FROM action.transit_copy atc_inner
- WHERE
- atc_inner.target_copy = %d AND
- atc_inner.source_send_time < NOW() - go_home_interval.value
- )
- )
- ) AS result
-) !, $cp->id, $cp->circ_lib, $cp->circ_lib, $cp->id);
+ ) IS NULL
+) !, $cp->id, $cp->circ_lib);
$joins .= q!
JOIN copy_has_not_been_home ON (true)
}
if ($ctes_needed == 2) {
- # In this auxiliary query, we ask the question, "has our copy come home
- # by any means that we can determine, even if it didn't circulate once
- # it came home, in the time defined by the go-home-interval?"
- # answer this question. Two parts to this too (besides including the
- # previous auxiliary query).
+ # By this query, we mean to determine that the copy hasn't landed at
+ # home by means of transit during the go-home interval (in addition
+ # to not having circulated from home in the same time frame).
#
- # 1: there have been no homebound transits for this copy since the
- # beginning of the go-home interval.
- # 2: there have been no checkins at home since the beginning of
- # the go-home interval for this copy
+ # There have been no homebound transits that arrived for this copy
+ # since the beginning of the go-home interval.
$ctes .= sprintf(q!
, copy_has_not_been_home_even_to_idle AS (
- SELECT
- copy_has_not_been_home.response AND (
- -- part 1
- SELECT MIN(atc.id) FROM action.transit_copy atc
- JOIN go_home_interval ON (true)
- WHERE
- atc.target_copy = %d AND
- atc.dest = %d AND
- atc.dest_recv_time >= NOW() - go_home_interval.value
- ) IS NULL AND (
- -- part 2
- SELECT MIN(circ.id) FROM action.circulation circ
- JOIN go_home_interval ON (true)
- WHERE
- circ.target_copy = %d AND
- circ.checkin_lib = %d AND
- circ.checkin_time >= NOW() - go_home_interval.value
- ) IS NULL
- AS result
-) !, $cp->id, $cp->circ_lib, $cp->id, $cp->circ_lib);
+ SELECT response AND NOT (
+ SELECT COUNT(*)::INT::BOOL
+ FROM action.transit_copy atc
+ WHERE
+ atc.dest = %d AND
+ atc.target_copy = %d AND
+ atc.dest_recv_time >= NOW() - (SELECT value FROM go_home_interval)
+ ) AS result FROM copy_has_not_been_home
+) !, $cp->circ_lib, $cp->id);
$joins .= " JOIN copy_has_not_been_home_even_to_idle ON (true) ";
}