Sub topic 2
Public versus private: what the specification hides
This chapter assumes you know basic SQL, standalone stored procedures and functions, and PL/SQL exception handling. A package has two parts. The specification lists what callers may use. The body holds the code. Anything declared only in the body is private, so code outside the package cannot reference it.
| Where declared | Callable from outside the package? |
|---|---|
| Package specification | Yes |
| Package body only | No |
Hiding helpers in the body
Any item you do not declare in the specification stays inside the body. The following package exposes two public subprograms and keeps one helper private.
CREATE OR REPLACE PACKAGE raise_api AS
FUNCTION get_salary(p_employee_id IN employees.employee_id%TYPE)
RETURN employees.salary%TYPE;
PROCEDURE raise_salary(p_employee_id IN employees.employee_id%TYPE,
p_pct IN NUMBER);
END raise_api;
/
CREATE OR REPLACE PACKAGE BODY raise_api AS
-- Private: not in the specification, so callers cannot call it.
FUNCTION is_valid_pct(p_pct IN NUMBER) RETURN BOOLEAN IS
BEGIN
RETURN p_pct > 0 AND p_pct <= 20;
END is_valid_pct;
FUNCTION get_salary(p_employee_id IN employees.employee_id%TYPE)
RETURN employees.salary%TYPE IS
l_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO l_salary
FROM employees
WHERE employee_id = p_employee_id;
RETURN l_salary;
END get_salary;
PROCEDURE raise_salary(p_employee_id IN employees.employee_id%TYPE,
p_pct IN NUMBER) IS
BEGIN
IF NOT is_valid_pct(p_pct) THEN
RAISE_APPLICATION_ERROR(-20001, 'Raise must be between 0 and 20 percent');
END IF;
UPDATE employees
SET salary = salary * (1 + p_pct / 100)
WHERE employee_id = p_employee_id;
END raise_salary;
END raise_api;
/Forward declarations for private subprograms
PL/SQL requires a subprogram to be declared before it is referenced. When two private subprograms call each other, you declare one of them first in the body. This declaration is called a forward declaration.
CREATE OR REPLACE PACKAGE parity AS
FUNCTION is_even(p_n IN PLS_INTEGER) RETURN BOOLEAN;
END parity;
/
CREATE OR REPLACE PACKAGE BODY parity AS
-- Forward declaration of the private function.
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN;
FUNCTION is_even(p_n IN PLS_INTEGER) RETURN BOOLEAN IS
BEGIN
IF p_n = 0 THEN
RETURN TRUE;
END IF;
RETURN is_odd(p_n - 1);
END is_even;
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN IS
BEGIN
IF p_n = 0 THEN
RETURN FALSE;
END IF;
RETURN is_even(p_n - 1);
END is_odd;
END parity;
/Overloading subprograms in a package
Two subprograms in one package can share a name if their parameter lists differ in number or datatype. The compiler picks the version that matches the arguments. A difference in return type alone is not enough.
CREATE OR REPLACE PACKAGE fmt AS
FUNCTION to_text(p_value IN NUMBER) RETURN VARCHAR2;
FUNCTION to_text(p_value IN DATE) RETURN VARCHAR2;
END fmt;
/
CREATE OR REPLACE PACKAGE BODY fmt AS
FUNCTION to_text(p_value IN NUMBER) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'FM999G999G990D00');
END to_text;
FUNCTION to_text(p_value IN DATE) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'YYYY-MM-DD');
END to_text;
END fmt;
/Package state and how long it lives
A variable declared in a package body keeps its value for the whole database session. Each call to the package sees the value left by the previous call. A new session starts with fresh variables.
CREATE OR REPLACE PACKAGE call_counter AS
PROCEDURE hit;
FUNCTION hits RETURN PLS_INTEGER;
END call_counter;
/
CREATE OR REPLACE PACKAGE BODY call_counter AS
g_hits PLS_INTEGER;
PROCEDURE hit IS
BEGIN
g_hits := g_hits + 1;
END hit;
FUNCTION hits RETURN PLS_INTEGER IS
BEGIN
RETURN g_hits;
END hits;
BEGIN
-- Initialization section: runs once per session, on first use of the package.
g_hits := 0;
END call_counter;
/Exercise
In the raiseapi package above, why does isvalid_pct stay out of the specification?
Show answer
It is an implementation detail. Keeping it private lets the body change or remove it without affecting callers.