24.9 Change Triggers For Data Guide-Enabled Search Index
When JSON data changes, some information in a data guide-enabled JSON
    search index is automatically updated. You can specify a procedure whose invocation is triggered
    whenever this happens. You can define your own PL/SQL procedure for this, or you can use the
    predefined change-trigger procedure add_vc.
               
The data-guide information in a data guide-enabled JSON search index records structure, type, and possibly statistical information about a set of JSON documents. Except for the statistical information, which is updated only when you gather statistics, relevant changes in the document set are automatically reflected in the data-guide information stored in the index.
You can define a PL/SQL procedure whose invocation is automatically triggered by such an index update. The invocation occurs when the index is updated. Any errors that occur during the execution of the procedure are ignored.
You can use the predefined change-trigger procedure add_vc to
      automatically add virtual columns that project JSON fields from the document set or to modify
      existing such columns, as needed. The virtual columns added by add_vc follow
      the same naming rules as those you add by invoking procedure
        DBMS_JSON.add_virtual_columns for a JSON column that has a data
      guide-enabled search index.
               
In either case, any error that occurs during the execution of the procedure is ignored.
Unlike DBMS_JSON.add_virtual_columns, add_vc
      does not first drop any existing virtual columns that were projected from fields in the
      same JSON column. To drop virtual columns projected from fields in the same JSON column by
        add_vc or by add_virtual_columns, use procedure
        DBMS_JSON.drop_virtual_columns.
               
You specify the use of a trigger for data-guide changes by using the keywords
          DATAGUIDE ON CHANGE in the PARAMETERS clause when
      you create or alter a JSON search index. Only one change trigger is allowed per index:
      altering an index to specify a trigger automatically replaces any previous trigger for it.
               
Example 24-15 alters existing JSON search index
        po_search_idx to use procedure add_vc.
               
Example 24-15 Adding Virtual Columns Automatically With Change Trigger ADD_VC
This example adds predefined change trigger
          add_vc to JSON search index po_search_idx.
                  
It first drops any existing virtual columns that were projected from fields in JSON
        column data either by procedure
          DBMS_JSON.add_virtual_columns or by a pre-existing
          add_vc change trigger for the same JSON search index. 
                  
Then it alters the search index to add change trigger add_vc (if it was
        already present then this is has no effect). 
                  
Finally, it inserts a new document that provokes a change in the data guide. Two virtual columns are added to the table, for the two scalar fields not under an array.
EXEC DBMS_JSON.drop_virtual_columns('J_PURCHASEORDER', 'DATA');
ALTER INDEX po_search_idx REBUILD
  PARAMETERS ('DATAGUIDE ON CHANGE add_vc');
INSERT INTO j_purchaseorder
  VALUES (
    SYS_GUID(),
    to_date('30-JUN-2015'),
    '{"PO_Number"     : 4230,
      "PO_Reference"  : "JDEER-20140421",
      "PO_LineItems"  : [ {"Part_Number"  : 230912362345,
                           "Quantity"     : 3.0} ]}');
DESCRIBE j_purchaseorder;
 Name                      Null?    Type
 ------------------------- -------- ----------------------------
 ID                        NOT NULL RAW(16)
 DATE_LOADED                        TIMESTAMP(6) WITH TIME ZONE
 DATA                               CLOB
 DATA$PO_Number                    NUMBER
 DATA$PO_Reference                 VARCHAR2(16)
_________________________________________________________
See Also:
- 
                           Oracle AI Database PL/SQL Packages and Types Reference for information about DBMS_JSON.add_virtual_columns
- 
                           Oracle AI Database PL/SQL Packages and Types Reference for information about DBMS_JSON.drop_virtual_columns
- User-Defined Data-Guide Change Triggers
 You can define a procedure whose invocation is triggered automatically whenever a given data guide-enabled JSON search index is updated. Any errors that occur during the execution of the procedure are ignored.
Parent topic: JSON Data Guide
24.9.1 User-Defined Data-Guide Change Triggers
You can define a procedure whose invocation is triggered automatically whenever a given data guide-enabled JSON search index is updated. Any errors that occur during the execution of the procedure are ignored.
Example 24-16 illustrates this.
A user-defined procedure specified with keywords DATAGUIDE ON CHANGE in a JSON search index PARAMETERS clause must accept the parameters specified in Table 24-5.
                  
Table 24-5 Parameters of a User-Defined Data-Guide Change Trigger Procedure
| Name | Type | Description | 
|---|---|---|
| table_name | VARCHAR2 | Name of the table containing column column_name. | 
| column_name | VARCHAR2 | Name of a JSON column that has a data guide-enabled JSON search index. | 
| path | VARCHAR2 | A SQL/JSON path expression that targets a particular field in the data in column column_name. This path is affected by the index change that triggered the procedure invocation. For example, the index change involved adding this path or changing its type value or its type-length value. | 
| new_type | NUMBER | A new type for the given path. | 
| new_type_length | NUMBER | A new type length for the given path. | 
Example 24-16 Tracing Data-Guide Updates With a User-Defined Change Trigger
This example first drops any existing virtual columns projected from fields in JSON
        column data. 
                     
It then defines PL/SQL procedure my_dataguide_trace, which prints the names of the table and JSON column, together with the path, type and length fields of the added virtual column. It then alters JSON search index po_search_idx to specify that this procedure be invoked as a change trigger for updates to the data-guide information in the index.
                     
It then inserts a new document that provokes a change in the data guide, which triggers the output of trace information.
Note that the TYPE argument to the procedure must be a number that is one of the DBMS_JSON constants for a JSON type. The procedure tests the argument and outputs a user-friendly string in place of the number.
                     
EXEC DBMS_JSON.drop_virtual_columns('J_PURCHASEORDER', 'DATA');
CREATE OR REPLACE PROCEDURE my_dataguide_trace(tableName VARCHAR2,
                                               jcolName  VARCHAR2,
                                               path      VARCHAR2,
                                               type      NUMBER,
                                               tlength   NUMBER)
  IS
    typename VARCHAR2(10);
  BEGIN
    IF    (type = DBMS_JSON.TYPE_NULL)    THEN typename := 'null';
    ELSIF (type = DBMS_JSON.TYPE_BOOLEAN) THEN typename := 'boolean';
    ELSIF (type = DBMS_JSON.TYPE_NUMBER)  THEN typename := 'number';
    ELSIF (type = DBMS_JSON.TYPE_STRING)  THEN typename := 'string';
    ELSIF (type = DBMS_JSON.TYPE_OBJECT)  THEN typename := 'object';
    ELSIF (type = DBMS_JSON.TYPE_ARRAY)   THEN typename := 'array';
    ELSE                                       typename := 'unknown';
    END IF;
    DBMS_OUTPUT.put_line('Updating ' || tableName || '(' || jcolName
                         || '): Path = ' || path || ', Type = ' || type
                         || ', Type Name = ' || typename
                         || ', Type Length = ' || tlength);
  END;
/
ALTER INDEX po_search_idx REBUILD
  PARAMETERS ('DATAGUIDE ON CHANGE my_dataguide_trace');
INSERT INTO j_purchaseorder
  VALUES (
    SYS_GUID(),
    to_date('30-MAR-2016'),
    '{"PO_ID"     : 4230,
      "PO_Ref"  : "JDEER-20140421",
      "PO_Items"  : [ {"Part_No"       : 98981327234,
                      "Item_Quantity" : 13} ]}');
COMMIT;
Updating J_PURCHASEORDER(DATA):
  Path = $.PO_ID, Type = 3, Type Name = number, Type Length = 4
Updating J_PURCHASEORDER(DATA):
  Path = $.PO_Ref, Type = 4, Type Name = string, Type Length = 16
Updating J_PURCHASEORDER(DATA):
  Path = $.PO_Items, Type = 6, Type Name = array, Type Length = 64
Updating J_PURCHASEORDER(DATA):
  Path = $.PO_Items.Part_No, Type = 3, Type Name = number, Type Length = 16
Updating J_PURCHASEORDER(DATA):
  Path = $.PO_Items.Item_Quantity, Type = 3, Type Name = number, Type Length = 2
Commit complete.
See Also:
- 
                           Oracle AI Database SQL Language Reference for information about PL/SQL constants TYPE_NULL,TYPE_BOOLEAN,TYPE_NUMBER,TYPE_STRING,TYPE_OBJECT, andTYPE_ARRAY.
- 
                           Oracle AI Database PL/SQL Packages and Types Reference for information about DBMS_JSON.drop_virtual_columns
Parent topic: Change Triggers For Data Guide-Enabled Search Index