Scalable Database Server, HiRDB Version 8 UAP Development Guide

[Contents][Index][Back][Next]

4.3.2 Defining a stored function

A stored function is a facility that registers a sequence of SQL-coded database operations to a database as a user-defined function.

Organization of this subsection
(1) Defining and executing an SQL stored function
(2) Example of an SQL stored function
(3) Rules for determining the called function and the result data type

(1) Defining and executing an SQL stored function

CREATE FUNCTION or CREATE TYPE registers a user-defined function in a database as an SQL stored function. DROP FUNCTION deletes an SQL stored function from the database. Once registered in a database, a user-defined function can be executed by calling it in an SQL statement. If a function has an SQL object that has been invalidated, the ALTER ROUTINE statement can be used to re-create that function. Figure 4-5 shows the definition and execution of an SQL stored function.

Figure 4-5 Defining and executing an SQL stored function

[Figure]

(2) Example of an SQL stored function

Figure 4-6 shows an example of how routine control SQL statements are combined and defined as a user-defined function and how the function is called and executed.

Figure 4-6 SQL stored function example

[Figure]

Explanation
  1. Defines the user-defined function name and the SQL parameters.
  2. Specifies the function return value.
  3. Begins the compound statements.
  4. Declares SQL variables.
  5. Specifies value assignments.
  6. Specifies return of the function return value.
  7. Ends the compound statements.
  8. Retrieves the SQL stored function with a function call.

Note
For details about the individual SQL statements, see the HiRDB Version 8 SQL Reference manual.

Defining the following functions is helpful.

(3) Rules for determining the called function and the result data type

A[Figure]B: Indicates that A has a higher priority than B.
  1. Determining the basic function
    The HiRDB system checks the arguments sequentially from the leftmost argument and sets the data types of the individual arguments as references. The system then selects the function whose parameters have data types with priorities that are equal to those of the references and sets that function as the basic function. If it does not find such a function, the system looks at the functions whose parameters have pre-defined data types with priorities that are less than those of the references and selects the function with the highest data type priorities. If a data type is a pre-defined data type, the priority is determined according to Table 4-1. If a data type is an abstract data type, the priority is determined according to Table 4-2 as follows:

    Table 4-2 Priorities of abstract data types

    Argument data type Priority
    Abstract data type Same data type[Figure]super type*

* The super type that is specified directly by the UNDER clause in an abstract type definition has a higher priority than other super types.

A[Figure]B: Indicates that A has a higher priority than B.
  1. Determining other candidate functions
    If an argument has an abstract data type, the data types of values that can actually be used as data for that argument are the same as the abstract data type in the argument definition and the subtypes of that data type. In addition to the basic function, all functions that have parameters corresponding to the same data type as the abstract data type of the argument, or to the abstract data type of a subtype, become candidates for the called function.
    If the basic function is the only candidate function, it becomes the called function. The data type of the function result becomes the RETURNS clause data type of the called function.
  2. Limiting the candidate functions based on the data type in the RETURNS clause
    For each candidate function other than the basic function, the HiRDB system checks whether the RETURNS clause data type is compatible with the RETURNS clause data type for the basic function. If the data type is not compatible, the function is dropped from the candidate functions. After checking this compatibility for all candidate functions, the HiRDB system determines the data type of the function result based on the RETURNS clause data types for the remaining candidate functions. The system performs a set operation (UNION[ALL] or EXCEPT[ALL]) on the remaining candidates. The resulting data type and data length become the data type and data length of the function result. For details, see the HiRDB Version 8 SQL Reference manual.
    However, if the data type of the function result is an abstract data type, the abstract data type of the RETURNS clause for the basic function is used.
  3. Determining the called function when an SQL statement is executed
    If there are two or three functions that cannot be determined uniquely, the HiRDB system determines which one of these candidate functions to call based on the actual data type used for each abstract data type argument when the SQL statement is executed. The system checks the arguments sequentially from the leftmost argument. If the actual value of an argument is a non-null value, the data type of that value is used as a reference. If the actual value is a null value, the data type of that argument is used as reference. From the candidate functions, the HiRDB system selects the function whose parameters have data types with priorities that are equal to those of the references and sets that function as the called function. If it cannot find such a function, the system looks at the functions whose parameters have pre-defined data types with priorities that are less than those of the references and selects the function with the highest data type priorities.

Because HiRDB allows a function to be defined more than once, there may be several candidates for a called function. The called function is determined by how the function call specification and the function definition match. Figure 4-7 shows the correspondences between a table with abstract data types and the called function.

Figure 4-7 Correspondences between a table with abstract data types and the called function

[Figure]

Explanation
Suppose that the following SQL statement uses the abstract data type function REMUNERATION to retrieve data from the staff table:
SELECT STAFF_NUMBER FROM STAFF_TABLE WHERE
  REMUNERATION(EMPLOYEE)>=2000.00
In this case, the function for each data type is determined and called according to whether the data for the parameter value is t_EMPLOYEE or t_SALESPERSON.
For details about the definitions of this staff table, see the HiRDB Version 8 Installation and Design Guide.

1 REMUNERATION = SALARY [Figure] REMUNERATION_RATE()

2 REMUNERATION = TOTAL_NUMBER_OF_CLIENTS [Figure] 1000 + SALARY [Figure] REMUNERATION_RATE()

Examples of determining the called function when abstract data types are included
In the examples below, A, B, and C are abstract data types, C is the super type of B, and B is the super type of A (priority of abstract data type: A[Figure]B[Figure]C).

Example 1

Prerequisite conditions
Table definition
CREATE TABLE T1(C1 C)
Function definitions
f(A), f(B), f(C)
SQL statement
SELECT f(C1) FROM T1

Results
Basic function
f(C)
Candidate functions when function call is f(C1)
f(A), f(B), f(C)
Called function
The following table shows which function is called when the SQL statement is executed.
Actual value of T1.C1 Called function
Type A f(A)
Type B f(B)
Type C f(C)
Null value f(C)

Example 2

Prerequisite conditions
Table definition
CREATE TABLE T1(C1 C,C2 B)
Function definitions
f(A,A), f(A,B), f(A,C), f(B,A), f(B,C), f(C,A),
f(C,B), f(C,C)
SQL statement
SELECT f(C1,C2) from T1

Results
Basic function
f(C,B)
Candidate functions when function call is f(C1,C2)
f(A,A), f(A,B), f(A,C), f(B,A), f(B,C), f(C,A), f(C,B)
Called function
The following table shows which function is called when the SQL statement is executed.
Actual value of T1.C1 Actual value of T1.C2 Called function
Type A Type A f(A,A)
Type B f(A,B)
Null value f(A,B)
Type B Type A f(B,A)
Type B f(B,C)
Null value f(B,C)
Type C Type A f(C,A)
Type B f(C,B)
Null value f(C,B)
Null value Type A f(C,A)
Type B f(C,B)
Null value f(C,B)