3.2. Adding Value
Sometimes it is worth stepping back and searching for the bigger picture before embarking on one's enhancements. In this case, I find myself wondering what other twists and turns I might encounter in my application development. Since I need UPPER-lower string duplication, I might also run into a requirement to perform lower-UPPER string duplication. As long as I am changing the twice function for one of these variations, I should try to stay ahead of the game and handle both variations.
So I will restate the new requirements of twice: double the specified string. Return the new string with the same case as the original, and return it in UPPER-lower or return it in lower-UPPER, depending on the user request.
When stated in this way, an obvious question pops up: how is the user going to specify the case handling in the call to twice? For a standalone function, this means adding a parameter. Instead of just accepting the string value for doubling, twice must also receive the type of action to perform. The new header for twice, therefore, must be:
FUNCTION twice (string_in IN VARCHAR2, action_in IN VARCHAR2)
where the action can be one of these values:
N
No change to case
UL
UPPER-lower case conversion
LU
lower-UPPER case conversion
Once the parameter and valid options are in place, the implementation is straightforward (and is shown in Example 3.2). I simply use an IF statement to direct the runtime engine to the right RETURN statement.
Example 3.2. The twice ...
Get Advanced Oracle PL/SQL Programming with Packages now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.