15.2 Create a Custom End-User Context
Use the CREATE END USER CONTEXT command to define a new,
custom end-user context object in the database.
Required privilege
- The
CREATE END USER CONTEXTsystem privilege to create an end-user context in the executing user's own schema. - The
CREATE ANY END USER CONTEXTsystem privilege to create an end-user context in any schema.
Syntax
CREATE [OR REPLACE] END USER CONTEXT [IF NOT EXISTS] [schema.]name
USING JSON SCHEMA '{JSON_Schema}';
JSON_Schema ::= "type": "object", properties
properties ::= "properties": { attribute [, attribute]... }
attribute ::= attr_name : {
"type": attr_type,
property_name_value
}
attr_type ::= {"integer" | "string" | "object" | "null"}
property_name_value ::=
{"o:onFirstRead" : "hdlowner.package.function" |
"default" : value}
Parameters
| Parameter | Description |
|---|---|
|
|
The schema (owner) name of the end-user context definition. If omitted, the current schema is used. |
|
|
The name of the end-user context definition. |
|
|
The name of the attribute within the context. |
|
|
The data type of the attribute. Supported types
are |
|
|
The default value of the attribute. A JSON number for integer attributes; a double-quoted JSON string for string attributes. |
|
|
The schema that owns the PL/SQL handler function for the attribute. |
|
|
The PL/SQL package that contains the handler function. |
|
|
The PL/SQL function name that is executed when
the specified event ( |
Usage notes and restrictions
- When
IF NOT EXISTSis specified:- If the context exists, the statement is a no-op. No error is raised.
- If the context does not exist, it is created.
- When
IF NOT EXISTSis omitted:- If the context already exists, an error is raised.
- If the context does not exist, it is created.
- When
OR REPLACEis specified:- If the context already exists, its attribute-value settings are replaced.
- If the context does not exist, it is created.
- When
OR REPLACEis omitted:- If the context already exists, an error is raised.
- If the context does not exist, it is created.
OR REPLACEandIF NOT EXISTScannot be combined in the same statement. If combined, an error is raised.- Event handlers:
- Each attribute can specify either a default value or an event handler, not both.
- The
o:onFirstReadevent causes the handler function to be called the first time the attribute is read during a session. The handler function must then set the attribute value. - A context can have at most one unique PL/SQL handler
function. If multiple attributes reference event handlers, they must
all reference the same
schema.package.function; otherwise, an error is raised.
Note:
- Context instantiation is transparent. When an end user or application references a custom context for the first time, the database automatically instantiates it from the definition. No explicit instantiation command is required.
- Context instances are session-specific. They are only visible and modifiable within the current session.
For syntax diagrams and additional details, see CREATE END USER CONTEXT in Oracle AI Database SQL Language Reference.
Example 15-1 Create a custom context with lazy-loaded and static attributes
The following example creates an end-user context
hr.hcm_context with two attributes. The emp_id
attribute is loaded lazily through a PL/SQL callback. The org_id
attribute has a static default value of 1.
CREATE END USER CONTEXT hr.hcm_context USING JSON SCHEMA '{
"type": "object",
"properties": {
"emp_id": {
"type": "integer",
"o:onFirstRead": "hr.hcm_core.init_user_context"
},
"org_id": {
"type": "integer",
"default": 1
}
}
}';
When emp_id is first accessed, the database invokes
hr.hcm_core.init_user_context to set the value. The
org_id attribute is always initialized with the value
1.
Example 15-2 Replace an existing context definition
To replace the definition of an existing end-user context, use OR
REPLACE.
CREATE OR REPLACE END USER CONTEXT hr.hcm_context USING JSON SCHEMA '{
"type": "object",
"properties": {
"emp_id": {
"type": "integer",
"o:onFirstRead": "hr.hcm_core.init_user_context"
},
"org_id": {
"type": "integer",
"default": 1
},
"region_id": {
"type": "string",
"default": "EMEA"
}
}
}';
Example 15-3 Create a context only if it does not already exist
CREATE END USER CONTEXT IF NOT EXISTS hr.hcm_context USING JSON SCHEMA '{
"type": "object",
"properties": {
"org_id": {
"type": "integer",
"default": 1
}
}
}';