Skip to main content

REST Data Services in Oracle APEX: A PL/SQL API for Creating Them

    

Create REST Data Services in Oracle APEX Using the Oracle REST Data Services PL/SQL API

The objective of this blog post is to show you how to create REST Data Services in Oracle APEX using a PL/SQL API (Oracle REST Data Services: ORDS).

Oracle APEX provides a sample REST Data Services that can be accessed from Sample APEX Application and it can be installed from Gallery.

RESTful Data Services can be created with Oracle APEX. The same can be done using Oracle REST Data Services PL/SQL API.

To navigate to RESTful Data Services, SQL Workshop --> RESTful Services.

This sample REST Data Service (Screenshot) has module, templates, handlers and parameters.

Module

oracle.example.emp

Templates

1) empinfo/ 

2) employees/:id

Handlers 

1) The empinfo/ template has a GET handler

2) The employeees/:id has GET and PUT handler

Parameters:

The employees/:id template has a PUT handler and it has two parameters

It's already been developed by Oracle APEX and now we will build it using ORDS PL/SQL API.

Steps to follow,

1) Enable Schema (ORDS.ENABLE_SCHEMA)

ENABLE_SCHEMA enables Oracle REST Data Services to access the named schema.

2) Define Module (ORDS.DEFINE_MODULE)

DEFINE_MODULE defines a resource module. If the module already exists, then the module and any existing templates will be replaced by this definition; otherwise, a new module is created.

3) Define Template (ORDS.DEFINE_TEMPLATE)

DEFINE_TEMPLATE defines a resource template. If the template already exists, then the template and any existing handlers will be replaced by this definition; otherwise, a new template is created.

4) Define Handler (ORDS.DEFINE_HANDLER)

DEFINE_HANDLER defines a module handler. If the handler already exists, then the handler and any existing handlers will be replaced by this definition; otherwise, a new handler is created.

5) Define Parameter (ORDS.DEFINE_PARAMETER)

DEFINE_PARAMETER defines a module handler parameter. If the parameter already exists, then the parameter will be replaced by this definition; otherwise, a new parameter is created.

Query

In Script Editor (Navigation: SQL Worksop --> SQL Scripts --> Create), you can copy and paste the following script for compilation. Click "Run" to compile the script. 


It will create a RESTful Data Service for you.

BEGIN -- Enable Schema (Schema: FAMPROP) ORDS.ENABLE_SCHEMA( p_enabled => TRUE, p_schema => 'FAMPROP', p_url_mapping_type => 'BASE_PATH', p_url_mapping_pattern => 'famprop', p_auto_rest_auth => FALSE); -- Define Module (Module: oracle.example.emp) ORDS.DEFINE_MODULE( p_module_name => 'oracle.example.emp', p_base_path => '/emp/', p_items_per_page => 25, p_status => 'PUBLISHED', p_comments => NULL); -- Define Template (Template: empinfo/) ORDS.DEFINE_TEMPLATE( p_module_name => 'oracle.example.emp', p_pattern => 'empinfo/', p_priority => 0, p_etag_type => 'HASH', p_etag_query => NULL, p_comments => NULL); -- Define Handler (Method: GET) ORDS.DEFINE_HANDLER( p_module_name => 'oracle.example.emp', p_pattern => 'empinfo/', p_method => 'GET', p_source_type => 'json/query', p_items_per_page => 25, p_mimes_allowed => '', p_comments => NULL, p_source => 'select * from emp'); -- Define Template (Template: employees/:id) ORDS.DEFINE_TEMPLATE( p_module_name => 'oracle.example.emp', p_pattern => 'employees/:id', p_priority => 0, p_etag_type => 'HASH', p_etag_query => NULL, p_comments => NULL); -- Define Handler (Method: GET) ORDS.DEFINE_HANDLER( p_module_name => 'oracle.example.emp', p_pattern => 'employees/:id', p_method => 'GET', p_source_type => 'json/query;type=single', p_items_per_page => 25, p_mimes_allowed => '', p_comments => NULL, p_source => 'select * from emp where empno = :id'); -- Define Handler (Method: PUT) ORDS.DEFINE_HANDLER( p_module_name => 'oracle.example.emp', p_pattern => 'employees/:id', p_method => 'PUT', p_source_type => 'plsql/block', p_items_per_page => 25, p_mimes_allowed => '', p_comments => NULL, p_source => 'begin update emp set ename = :ename, job = :job,
hiredate = :hiredate,
mgr = :mgr,
sal = :sal,
comm = :comm,
deptno = :deptno where empno = :id; :status := 200; :location := :id; exception when others then :status := 400; sys.htp.p( sys.htf.escape_sc(sqlerrm) ); end;'); -- Define Paramter (Parameter: ID) ORDS.DEFINE_PARAMETER( p_module_name => 'oracle.example.emp', p_pattern => 'employees/:id', p_method => 'PUT', p_name => 'ID', p_bind_variable_name => 'id', p_source_type => 'HEADER', p_param_type => 'STRING', p_access_method => 'IN', p_comments => NULL); -- Define Paramter (Parameter: X-APEX-FORWARD) ORDS.DEFINE_PARAMETER( p_module_name => 'oracle.example.emp', p_pattern => 'employees/:id', p_method => 'PUT', p_name => 'X-APEX-FORWARD', p_bind_variable_name => 'location', p_source_type => 'HEADER', p_param_type => 'STRING', p_access_method => 'OUT', p_comments => NULL); -- Define Paramter (Parameter: X-APEX-STATUS-CODE) ORDS.DEFINE_PARAMETER( p_module_name => 'oracle.example.emp', p_pattern => 'employees/:id', p_method => 'PUT', p_name => 'X-APEX-STATUS-CODE', p_bind_variable_name => 'status', p_source_type => 'HEADER', p_param_type => 'INT', p_access_method => 'OUT', p_comments => NULL); COMMIT; END; /

Output

https://apex.oracle.com/pls/apex/famprop/emp/empinfo/ (GET)

https://apex.oracle.com/pls/apex/famprop/emp/employees/:id (GET)

https://apex.oracle.com/pls/apex/famprop/emp/employees/:id (PUT)

Generate Swagger Doc

You can generate Swagger Document for the REST Data Service that you build.


Generated Swagger Doc: https://editor.swagger.io/?url=https://apex.oracle.com/pls/apex/famprop/open-api-catalog/emp/

That's it. Happy APEXing!

References/Credits:

Comments

Post a Comment

Popular posts from this blog

Printing Page Numbers in RTF Template [Oracle BI Publisher]

Here I am giving an example to print the page numbers dynamically in the RTF (Rich Text Format) template. Step 1:  Go to page footer and copy and paste the below script. Page |  <?fo:page-number?>  of  <?fo:page-number-citation:xdofo:lastpage-joinseq?> <fo:page-number> :   This is the object, which is used to represent the current page-number. <?fo:page-number-citation:xdofo:lastpage-joinseq?> :  This is the syntax, which is used to represent the total number of pages. Step 2:  Load the XML and preview the result. Output: That's it. References: fo:page-number Printing Page Number Code in Oracle XMLP RTF Template

Oracle Application Express Views (APEX)

Application Express Views Search SELECT * FROM apex_dictionary WHERE column_id = 0; View Comment Parent View APEX_APPLICATIONS Applications defined in the current workspace or database user. APEX_WORKSPACES APEX_APPLICATION_ALL_AUTH All authorization schemes for all components by Application APEX_APPLICATIONS

Save Selected Interactive Grid Records into a Collection - Oracle APEX

Here I am giving an example to save selected interactive grid records into a oracle apex collection. Step 1: Create a new blank page. Note: Mine was page 20. You will need to update reference to " P20 " with your page number if it's different. Step 2: Create a new interactive grid report region to the page using below query. Set Static Id "EmpDetails" to the region. SELECT  *     FROM   ( SELECT  emp . empno ,                emp . ename ,                emp . JOB ,                dept . dname department ,                dept . loc  LOCATION ,                mgr . ename  manager ,                emp . hiredate ,                 nvl ( emp . sal , 0 )  salary ,                 nvl ( emp . comm , 0 )  commission            FROM  eba_demo_chart_emp emp ,                eba_demo_chart_dept dept ,                eba_demo_chart_emp mgr           WHERE  emp . deptno = dept . deptno             AND  emp . mgr      = mgr . empno  ( + )           ORDER   BY  emp . ename