egdb3_12_9
.action
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
maintain_usr_circ_history()
Parameters
Name
Type
Mode
IN
Definition
DECLARE cur_circ BIGINT; first_circ BIGINT; BEGIN -- Any retention value signifies history is enabled. -- This assumes that clearing these values via external -- process deletes the action.usr_circ_history rows. -- TODO: replace these settings w/ a single bool setting? PERFORM 1 FROM actor.usr_setting WHERE usr = NEW.usr AND value IS NOT NULL AND name IN ( 'history.circ.retention_age', 'history.circ.retention_start' ); IF NOT FOUND THEN RETURN NEW; END IF; IF TG_OP = 'INSERT' AND NEW.parent_circ IS NULL THEN -- Starting a new circulation. Insert the history row. INSERT INTO action.usr_circ_history (usr, xact_start, target_copy, due_date, source_circ) VALUES ( NEW.usr, NEW.xact_start, NEW.target_copy, NEW.due_date, NEW.id ); RETURN NEW; END IF; -- find the first and last circs in the circ chain -- for the currently modified circ. FOR cur_circ IN SELECT id FROM action.circ_chain(NEW.id) LOOP IF first_circ IS NULL THEN first_circ := cur_circ; CONTINUE; END IF; -- Allow the loop to continue so that at as the loop -- completes cur_circ points to the final circulation. END LOOP; IF NEW.id <> cur_circ THEN -- Modifying an intermediate circ. Ignore it. RETURN NEW; END IF; -- Update the due_date/checkin_time on the history row if the current -- circ is the last circ in the chain and an update is warranted. UPDATE action.usr_circ_history SET due_date = NEW.due_date, checkin_time = NEW.checkin_time WHERE source_circ = first_circ AND ( due_date <> NEW.due_date OR ( (checkin_time IS NULL AND NEW.checkin_time IS NOT NULL) OR (checkin_time IS NOT NULL AND NEW.checkin_time IS NULL) OR (checkin_time <> NEW.checkin_time) ) ); RETURN NEW; END;