[Download] | [Documentation Home] | [Release Note]

Nested Queries

Introduction

When using a data service query, we may need to use the result of one query to be used as an input parameter of another query. This can be achieved using WSO2 Data Services Server which is explained here.

Step 1

First add the query with an input parameter which is evaluated as the result of another query. So here the 'customerNumber' input maping will be result of another query.


Figure 1: Define Query 1

Step 2

Now add the query which will result 'customerNumber'. Below shows the query page with the query 'customerOrderSQL'.


Figure 2: Define Query 2

Step 3

Then go to Add New Output Mapping option of the above query page and define the values as below. Here 'query' should be selected as the mapping type which will allow to select the query name and define query parameter mapping. Now as shown in below the query 'customerNameSQL' will be invoked with 'customerNumber' parameter which is a result of 'customerOrderSQL'.


Figure 3: Define Nested Query Mapping

Invoke Nested Query Operation

Add an operation with customerOrderSQL as follows.


Figure 4: Define Nested Query Operation


So when we invoke the operation result will contain the nested result element 'Customer' which is returned from 'customerNameSQL' query.


Figure 5: Nested Query Result

Nested Query Configuration Syntax with call-query

As described above, nested queries can be configured and invoked in data services. In the configuration syntax this is achieved by defining call-query element as a result element of a query.


Figure 6: Call-query syntax for nested queries

Here is the description of the child elements and attributes of the call-query.

  • call-query/@href : Name of the nested query
  • call-query/with-param : Define the input parameter to be passed to nested query
  • call-query/with-param/@name : name of the parameter as defined in the nested query
  • call-query/with-param/@query-param : name of the parameter as returned from this query
  • call-query/with-param/@column : name of the parameter as returned from the immediate parent result element

From call-query/with-param/@query-param and call-query/with-param/@column attributes only one is used in a single call-query/with-param element.