Previous | Table of Contents | Next |
To design the function, we first need to examine how each business rule can be satisfied by the function.
SELECT count (*) FROM SCHEDULED_CLASSES WHERE instructor_number = <the professor's ID number>;
After determining how we can meet each individual requirement, we can write the pseudocode in Listing 5.22 for the function.
Listing 5.22 Pseudocode for the Assign_Instructor() function.
get the information about the specified course; open a cursor of all instructors still able to teach courses; for each instructor in the list loop determine how many classes the instructor is teaching; determine how many classes the instructor can teach; if the instructor can't teach any more classes then update the maximum_classes field in the INSTRUCTORS table; goto the next professor; end if; if the instructor's approved level is too low for the course then goto the next professor; end if; if the instructor's min level > the course level then goto the next professor; end if; open a cursor of other classes taught by the instructor; for each course taught by the instructor loop compare the course time and day against the specified course; if there is a conflict then goto the next professor; end if; return the professor's ID to the calling procedure; end loop; end loop; if the function has come this far then raise exception NO_INSTRUCTORS_AVAILABLE; end if;
Once pseudocode is written it s very easy to write the code for the function, because the essential part of the work, deciding the flow of the function s logic, has already been accomplished. Figure 5.1 illustrates the logic for the Assign_Instructor() function.
Figure 5.1 The logic flow of the Assign_Instructor() function.
Now that we have the logic of the function outlined, we re ready to write the code. Our largest obstacle is the lack of a continue statement in PL/SQL. While our pseudocode can say skip to the next instructor or goto the next instructor, PL/SQL doesn t provide us with an easy way to do this. We could use the GOTO statement to handle this situation, but most people consider that to be bad coding style.
To resolve this dilemma, we ll create a boolean variable called bInstructorValid that we ll use to keep track of the status. If a condition occurs that forces us to discard an instructor, all subsequent tests inside the loop will be skipped , because the first condition of each test will be that bInstructorValid returns TRUE . Listing 5.23 is the code for our new function.
Previous | Table of Contents | Next |