Created
November 26, 2010 13:46
-
-
Save jonte/716727 to your computer and use it in GitHub Desktop.
Trigger of 'effin doom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TRIGGER CourseOverFlowTrigger; | |
CREATE TRIGGER CourseOverflowTrigger | |
INSTEAD OF INSERT ON WaitingListView | |
REFERENCING NEW AS NewRow | |
OLD AS OldRow | |
FOR EACH ROW | |
DECLARE maxStudents NUMBER; | |
regedStudents NUMBER; | |
maxPlacement NUMBER; | |
numMissingCourses NUMBER; | |
listPlacements NUMBER; | |
BEGIN | |
SELECT nrStudents INTO maxStudents FROM LimitedCourses WHERE LimitedCourses.code = :NewRow.course; | |
SELECT COUNT(status) INTO regedStudents FROM WaitingListView WHERE status = 'REGISTERED' AND Course = :NewRow.course; | |
SELECT nvl(MAX(placement),0) INTO maxPlacement FROM QueueingFor WHERE course = :NewRow.course; | |
-- Have we missed some pre-req? | |
SELECT COUNT(*) INTO numMissingCourses FROM | |
((SELECT RequiredCourse as c FROM Requires WHERE Requires.course = :NewRow.course) | |
MINUS | |
(SELECT COURSE as c FROM Completed WHERE Completed.Student = :NewRow.student)); | |
-- Are we on either the waiting list or registeredFor list already? | |
SELECT COUNT(*) INTO listPlacements FROM | |
((SELECT Student FROM RegisteredFor WHERE RegisteredFor.Student = :NewRow.student) | |
UNION | |
(SELECT Student FROM QueueingFor WHERE QueueingFor.Student = :NewRow.student)); | |
IF (regedStudents > maxStudents) THEN -- There are too many students | |
IF (listPlacements = 0) THEN -- Have we previously enqueued ourselves? | |
INSERT INTO test VALUES ('Student has no prior list placement (ALL OK!)'); | |
INSERT INTO test VALUES ('RegedStudents > maxStudents, Enqueueing student.'); | |
INSERT INTO QueueingFor VALUES (:NewRow.course, :NewRow.student, maxPlacement+1); | |
END IF; | |
ELSE -- There is room for us! | |
INSERT INTO test VALUES ('RegedStudents < maxStudents, Checking validity...'); | |
IF (numMissingCourses = 0) THEN -- Have we taken all the pre-reqs? | |
INSERT INTO test VALUES ('Student has taken pre-reqs'); | |
IF (listPlacements = 0) THEN -- Are we already enrolled, or enqueued? | |
INSERT INTO test VALUES ('Student has no prior list placement (ALL OK!)'); | |
INSERT INTO RegisteredFor VALUES (:NewRow.course, :NewRow.student); | |
END IF; | |
ELSE | |
INSERT INTO test VALUES ('Student has NOT taken pre-reqs'); | |
END IF; | |
END IF; | |
END; | |
. | |
run; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment