WSO2Con 2013 CFP Banner

How to use Oracle Ref Cursors with WSO2 Data Services Server?

In this tutorial, Kalpanie Wanigaratne, Software Engineer at WSO2 speaks about what is an Oracle REF cursor and how you can use the REF cursors within WSO2 Data Services Server.

Date: Wed, 6th Jan, 2010
Level: Introductory
Reads: 6469
Discuss this article on Stack Overflow
Kalpanie Wanigaratne

Colombo

Contents

Introduction

General static cursor can only be used to get data which associates with one SELECT statement and it cannot be used dynamically. Therefore if you need to fetch data associated with different SELECT statements at different times you cannot use a general explicit cursor. The answer for such a scenario is REF cursors. In the case of a normal explict cursor, the SQL query has to be defined at the time of declaring the cursor itself. But in REF cursor, the cursor declartion is not associated with any SQL query and it is associated with a query at a later stage. This brings in a lot of flexibility as different SQL queries can be associated with the cursor (one at a time) programatically. REF cursors also provide the feature of passing parameters. The primary advantage of using cursor variables is their capability to pass result sets between sub programs (like stored procedures functions packages etc).

WSO2 Data Services Server caters to the feature of supporting REF cursors. This document will guide you in writing a REF cursor and to use it within WSO2 Data Services Server

Getting Started

The following are the prerequisites to continue with the tutorial.

1) Download WSO2 Data Services Server version 2.2.0 from http://wso2.org/downloads/data-services-server

2) Running Oracle instance

3) Copy Oracle JDBC driver to DS_HOME/components/lib

4) Start WSO2 Data Services Server ( Run DS_HOME/wso2server.bat | wso2server.sh )

5) Open a web browser and navigate to https://localhost:9443/carbon

6) If you have not changed the default Admin user credentials you can use it to login to the system, otherwise you can use a created user name, and a password to login.

Step 1 : Creating and populating the sample database

Before we start on create the data services we will begin with creating the database with a ref cursor.

Let's create a table called 'dept' and insert values to the table. 

/* create and populate dept table */

DROP TABLE dept
/

CREATE TABLE dept (
    dept_id       NUMBER
  , name          VARCHAR2(100)
  , location      VARCHAR2(100)
)
/

INSERT INTO dept VALUES (100 , 'ACCOUNTING'          , 'BUTLER, PA');
INSERT INTO dept VALUES (101 , 'RESEARCH'            , 'DALLAS, TX');
INSERT INTO dept VALUES (102 , 'SALES' , 'CHICAGO, IL');
INSERT INTO dept VALUES (103 , 'OPERATIONS' , 'BOSTON, MA');
INSERT INTO dept VALUES (104 , 'IT' , 'PITTSBURGH, PA');
INSERT INTO dept VALUES (105 , 'ENGINEERING' , 'WEXFORD, PA');
INSERT INTO dept VALUES (106 , 'QA' , 'WEXFORD, PA');
INSERT INTO dept VALUES (107 , 'PROCESSING' , 'NEW YORK, NY');
INSERT INTO dept VALUES (108 , 'CUSTOMER SUPPORT' , 'TRANSFER, PA'); INSERT INTO dept VALUES (109 , 'HQ' , 'WEXFORD, PA');
INSERT INTO dept VALUES (110 , 'PRODUCTION SUPPORT' , 'MONTEREY, CA');
INSERT INTO dept VALUES (111 , 'DOCUMENTATION' , 'WEXFORD, PA');
INSERT INTO dept VALUES (112 , 'HELP DESK' , 'GREENVILLE, PA');
/ commit; / /* CREATE PACKAGE "ref_cursor_package" */ CREATE OR REPLACE PACKAGE ref_cursor_package AS TYPE t_ref_cursor IS REF CURSOR; FUNCTION get_dept_ref_cursor(p_dept_id INTEGER) RETURN t_ref_cursor; END; /

Following package will retrieve the dept_id, name and location from dept table and the reference to the cursor (REF cursor ) will be returned by the function.  

CREATE OR REPLACE PACKAGE BODY ref_cursor_package
AS
    FUNCTION get_dept_ref_cursor (p_dept_id INTEGER)
        RETURN t_ref_cursor IS
        dept_ref_cursor t_ref_cursor;
    BEGIN
        OPEN dept_ref_cursor FOR
            SELECT dept_id, name, location
            FROM   dept
            WHERE  dept_id > p_dept_id
            ORDER BY dept_id;
        RETURN dept_ref_cursor;
    END get_dept_ref_cursor;
END ref_cursor_package;
/

'get_dept_ref_cursor' is a simple function which will get the department id from the user as an input parameter and will return the dept_id, name and the location of the departments where the department id's greater than the input value. The values would return as a ref_cursor. 

We will execute the function in sqlplus console like showing in below figure.

Figure 1 : Executing the Oracle function

Step 2 : Creating the data service 

We can proceed on creating the data service since we are done with creating the database.

Let's start on creating the data service by clicking on 'Create' link under Services/Add/Data Service.  We'll name the service as 'OracleRefCursorSample' as shown below.

Figure 2 : Creating the data service by naming the service. 

Step 3 : Creating the data source

Once you create the data service click on next and then on 'Add New Data Source'. This page is to enter the details of the data source which we are using to create the data service. Let's name the data source as 'oracle_ds'. The following image shows the Add Data Source page with the entered data. 

 

 
Figure 3 : Creating a new Oracle data source.

Click on 'Save' button once you have filled the data source information. You can click on 'Test Connection' to test the database connection.

Step 4: Creating the Query

Once you have saved the data source, click on 'Next' button to create the query. Start creating the query by naming the query. Let's name it as 'get_dept'. Select the created data source from the drop down list. Enter the query to call the function. Lets enter the query as {? = call ref_cursor_package.get_dept_ref_cursor(?)}

Now click on 'Add Input Mapping' button to enter the parameters. Since 'get_dept_ref_cursor ' function accepts dept_id as INPUT and returns the REF cursor we need to specify them as input mappings.

Let's enter the following details.

1) Mapping Name : dept_id

    SQL type         : INTEGER

    IN/OUT type    : IN

    Ordinal            :2

2) Mapping Name : dept_info

    SQL type         : ORACLE_REF_CURSOR

    IN/OUT type    : OUT

    Ordinal            :1

 

Figure 4 : Adding Input Mappings

Once you have entered two input parameters click on 'Main Configuration' to go back to the main page. Then we'll enter the details to create the output result set. Group By Element Name will help you to define a result set name. We will name this as Departments for this scenario and the row we will name as 'department'.

Click on 'Add Output Mapping' to enter the output elements. Let's add name, dept_id and location which comes in the result set as output parameters as shown in the following figure.

Figure 5 : Creating the query

After adding the relevant details click on 'Save' and 'Next' to create the operation.

Step 5: Creating the operation

Let's name the operation as 'GetDeptInfo' and select the defined query id (get_dept) in the previous step. Click on 'Save' and then 'Finish'.

 
Figure 6 : Adding a new operation

Step 6: Deployed Services

Once you click on finish, the data service will deploy and it will list down under List of Deployed services. Following figure shows the deployed 'OracleRefCursorSample' deployed service.

 
Figure 7 : Deployed Services List

Step 7: Invoke the service

Click on 'Try It' link on the deployed service to invoke and test the service. Lets enter 110 as the dept_id, it will list down all the other departments which are greater than 110, which is given by the function.

Figure 8 : Invoked Service

Created data service (OracleRefCursorSample.dbs) will look as below.

 
Figure 9 : OracleRefCursorSample.dbs

Conclusion

This tutorial speaks about what is an Oracle REF cursor and how you can use the REF cursors within WSO2 Data Services Server.

 

Author 

Kalpanie Wanigaratne, WSO2 Inc. kalpanie at wso2 dot com

AttachmentSize
sql_cmd.jpg37.57 KB
step1.jpg24.2 KB
step2.jpg19.72 KB
step3.jpg14.63 KB
step4.jpg30.16 KB
step5.JPG13.26 KB
step6.JPG31.86 KB
step7.JPG29.74 KB
step8.jpg38.06 KB
OracleRefCursorSample.txt1.44 KB
hazelblossom41.gmail.com's picture

I agree with this article

I agree with this article completely, I must thank you for posting such helpful facts.how to write definition essay
latharangith1988.gmail.com's picture

General static cursor can

General static cursor can only be used to get data which associates with one SELECT statement and it cannot be used dynamically. Therefore if you need to fetch data associated with different SELECT statements at different times you cannot use a general explicit cursor. Brain teasers
latharangith1988.gmail.com's picture

Its not the case that reader

Its not the case that reader must be completely agreed with author's views about article. So this is what happened with me, anyways its a good effort, I appreciate it. Thanks reconquerir son ex
latharangith1988.gmail.com's picture

WOA(Web Oriented

WOA(Web Oriented Architecture) is simply a way of implementing SOA, by creating services that are RESTful resources. This allows any service or data to be accessed with a unique URL.This is a much simpler way of accessing your data. LGBT Community
baron_barnaby.yahoo.com's picture

The WSO2 Mashup Server is an

The WSO2 Mashup Server is an open source mashup platform that hosts JavaScript based mashups. It is based on Apache Axis2 and other open source projects, and provides JavaScript authors the ability to consume, compose and emit web services, feeds, scraped web pages, email, and instant messages. brain injury attorney

latharangith1988.gmail.com's picture

The WSO2 Data Services Server

The WSO2 Data Services Server is an extremely simple and elegant mechanism to take data and make it available as a set of WS-* style Web services or as a set of REST style Web resources. It augments SOA development efforts by providing an easy to use platform for creating and hosting data services. stand alone ice maker
latharangith1988.gmail.com's picture

I just stumbled upon your

I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. وظائف
latharangith1988.gmail.com's picture

The applications in an SOA

The applications in an SOA handle different types of data, each with its own access patterns, structure, lifecycle, archival needs, and other attributes and behaviors. As a result, choosing the right storage mechanism can significantly affect the ultimate performance and success of an application. Symptoms of Pneumonia
latharangith1988.gmail.com's picture

Just want to say I definitely

Just want to say I definitely see the content of this I tried to find today. This brief article is directly at the time. Thanks for your time to write and also to see him. This could be one of the best pieces I have had for a long time. afghan lawyers
latharangith1988.gmail.com's picture

WSO2 Governance Registry

WSO2 Governance Registry 4.1.0 Released !!! The WSO2 Governance Registry team is pleased to announce the 4.1.0 release of the WSO2 Governance Registry. WSO2 Governance Registry is a user-friendly, but comprehensive enterprise governance. وظائف
latharangith1988.gmail.com's picture

Thank you for another

Thank you for another fantastic blog. Where else could I get this kind of information written in such an incite full way? I have a project that I am just now working on, and i am sure this will help me a lot..and I have been looking for such information since from few days....Thanks!!!!! dropcards
jenny.jasmeen.gmail.com's picture

Erstwhile you make preserved

Erstwhile you make preserved the data source utter on Incoming switch to create the query. Testking OG0-093 Signal creating the query by naming the query.Let's phratry it as get_dept. Select the created information communicator from the driblet doctor move.Preserve the ask to tendency the office Testking 1z0-052