Oracle12c enhances a series of methods for defining and executing PLSQL program units. This article covers several new features of Oracle12c: Oracle11g introduces PLSQL function result Cache
Oracle 12c enhances a series of methods for defining and executing PL/SQL program units. This article covers several new features of Oracle 12c: Oracle 11g introduces PL/SQL function result caching
Oracle 12c enhances a series of methods for defining and executing PL/SQL program units. This article covers several new features of Oracle 12c:
1. optimized the caller permission function for the result cache condition.
2. PL/SQL functions can be defined and executed in SQL statements.
3. Use the accessible by condition to specify a whitelist to restrict access to program units.
4. You can directly authorize a role to a specified program unit.
Caller permission and PL/SQL function result Cache
Oracle 11g introduces PL/SQL function result caching, providing a very powerful, effective, and easy-to-use caching mechanism. The main goal is to ensure that if the last retrieved record does not change, the same results are returned from the cache without executing any SQL statements.
This feature is available to the entire database instance. That is to say, if a user USER_ONE executes a result cache function to obtain the row ID = 100 from the employees table. When another user USER_TWO executes the row with the same function request ID = 100, the result will be returned directly from the cache without executing a SELECT statement.
If you have never used this feature, I strongly recommend that you study it and use it-be sure to coordinate with DBA to set a reasonable result cache.
Even in Oracle 11.2, you still cannot use it with the caller permission (AUTHID CURRENT_USER) and function result cache (RESULT_CACHE. This attempt will only result in compilation failure:
Create or replace function last_name (
Employee_id_in
IN employees. employee_id % TYPE)
RETURN employees. last_name % TYPE
AUTHID CURRENT_USER
RESULT_CACHE
IS
Rochelle return employees. last_name % TYPE;
BEGIN
SELECT last_name
INTO l_return
FROM employees
WHERE employee_id = employee_id_in;
RETURN l_return;
END;
/
This compilation error occurs:
PLS-00999: implementation restriction (may be temporary) RESULT_CACHE is disallowed on subprograms in Invoker-Rights modules1
The cause of the failure is the caller permission. During running, the PL/SQL engine uses the current user permission to process related database objects, such as tables and views. However, if the function has the RESULT_CACHE condition, the user USER_ONE executes the function. After the 100 parameter is input, the user USER_TWO calls the same function, the function body will not be executed at all and the related table EMPLOYEES will not be checked based on the USER_TWO permission. This will cause serious security problems!
The good news is that this restriction is temporary. In 12c, We can compile the above last_name function without reporting an error!
Let's take a look at the background. Oracle 12c passes the current user as an implicit parameter. This parameter will be cached together with other input parameters!
This means that the result cache of the caller permission function is based on the current user partition. Therefore, the result cache of the caller permission function improves performance only for repeated calls with the same user parameters. In Oracle 11g, we can use another method to achieve the same effect. Just change the implementation of the last_name function:
Code Listing 1: "Partitioned" Oracle Database 11g Release 2 invoker's rights function
Create or replace package employee_api
AUTHID CURRENT_USER
IS
FUNCTION last_name (
Employee_id_in IN employees. employee_id % TYPE)
RETURN employees. last_name % TYPE;
END;
/
Create or replace package body employee_api
IS
FUNCTION I _last_name (
Employee_id_in IN employees. employee_id % TYPE,
User_in IN VARCHAR2 default user)
RETURN employees. last_name % TYPE
RESULT_CACHE
IS
Rochelle return employees. last_name % TYPE;
BEGIN
SELECT last_name
INTO l_return
FROM employees
WHERE employee_id = employee_id_in;
RETURN l_return;
END;
FUNCTION last_name (
Employee_id_in IN employees. employee_id % TYPE)
RETURN employees. last_name % TYPE
IS
Rochelle return employees. last_name % TYPE;
BEGIN
RETURN I _last_name (employee_id_in,
USER );
END;
END;
/
Note that the last_name function is defined in the package description and the result is not cached. Instead, a public function only calls a private function (defined only in the function body). We can see that we have added 2nd more parameters: USER!
So every time I call employee_api.last_name, Oracle will determine whether the user has been cached. Obviously, this method is a bit redundant! In 12c, we only need to consider whether it is worthwhile to add the result cache to the caller permission program!
Define PL/SQL subroutines in SQL statements
Developers have long been able to call their own PL/SQL functions in SQL statements. In this case, I have created a function BETWNSTR which features similar to substr user-defined functions:
FUNCTION betwnstr (
String_in IN VARCHAR2
, Start_in IN PLS_INTEGER
, End_in IN PLS_INTEGER
)
RETURN VARCHAR2
IS
BEGIN
RETURN (SUBSTR (
String_in, start_in,
End_in-start_in + 1 ));
END;
I can call it like this:
SELECT betwnstr (last_name, 3, 5)
FROM employees
This method extends the use of the SQL language. The disadvantage is that you need to switch between the SQL and PL/SQL Execution engines!
To Oracle 12c, you can use the WITH clause to define PL/SQL functions and procedures and then call the subquery to return results. This feature enables us to upgrade the BETWNSTR function and query into a statement !!!
WITH
FUNCTION betwnstr (
String_in IN VARCHAR2,
Start_in IN PLS_INTEGER,
End_in IN PLS_INTEGER)
RETURN VARCHAR2
IS
BEGIN
RETURN (SUBSTR (
String_in,
Start_in,
End_in-start_in + 1 ));
END;
SELECT betwnstr (last_name, 3, 5)
FROM employees