Show TOC

$filter System Query Option APIsLocate this document in the navigation structure

The OData $filter system query option filters a subset of the entries from the collection that are addressed in the request URL. SAP Gateway provides filter APIs which can be used to query in the SAP backend.

Filter expression tree is a tree representation of OData filter system query option provided in the request URI.

A visitor interface /IWBEP/IF_MGW_EXPR_VISITOR has been provided to process the filter expression tree. You have the option to:

  • Implement the visitor interface /IWBEP/IF_MGW_EXPR_VISITOR in your own way to process the filter expression tree.

  • Use the /IWBEP/IF_MGW_REQ_ENTITYSET API which is an implementation of the visitor interface /IWBEP/IF_MGW_EXPR_VISITOR to get the ABAP Open SQL WHERE clause.

    Note

    While implementing a query for an entity, you can do a query on the database using Open SQL SELECT syntax if you have direct access to persistency.

Implementing the Visitor Interface /IWBEP/IF_MGW_EXPR_VISITOR

Use the method GET_FILTER_EXPRESSION_TREE of the interface /IWBEP/IF_MGW_REQ_ENTITYSET to get the filter expression tree and then implement the visitor interface /IWBEP/IF_MGW_EXPR_VISITOR to process the filter expression tree as per your requirement. You should implement the visitor interface using Visitor design pattern.

To get the filter expression tree, invoke the /IWBEP/IF_MGW_EXPR_NODE API in the GET_ENTITYSET method of the Data Provider class (DPC).

Sample Code

Data: lo_filter_tree TYPE REF TO /iwbep/if_mgw_expr_node.
  lo_filter_tree = io_tech_request_context->get_filter_expression_tree( ). 
Using the Interface /IWBEP/IF_MGW_REQ_ENTITYSET

The OData $filter system query option allows clients to filter the set of resources that are addressed by a request URL. Application developers who need to implement a query for an entity in a service and have direct access to persistency can run a direct query on the database using Open SQL SELECT syntax. SAP Gateway provides a feature to get an Open SQL WHERE clause from the OData request that can easily be used in the SELECT query directly. Use the method GET_OSQL_WHERE_CLAUSE of the interface /IWBEP/IF_MGW_REQ_ENTITYSET to get an Open SQL WHERE clause. The Open SQL WHERE clause can be used do a query in the backend directly using ABAP SELECT syntax.

To get the Open SQL WHERE clause, use the code below in the GET_ENTITYSET method of the DPC of an OData service:

Sample Code

DATA: lv_osql_where_clause TYPE string. 
 *----get Open SQL WHERE Clause from $filter-----*
lv_osql_where_clause = io_tech_request_context->get_osql_where_clause( ). 

Only a subset of the possible filter expressions can be converted into a WHERE clause.

$filter on Navigation Property

The system query option $filter is supported on navigation properties.

Information about $filter on navigation is provided via the filter expression tree in the backend system for the affected data provider.

Examples of supported URLs containing navigation steps in the $filter query option:

  • SalesOrder?$filter=TwinEntity eq 'null'

  • SalesOrder?$filter=TwinEntity/property eq 'xyz'

  • SalesOrders?$filter=EditState ne 2 or (EditState eq 2 and TwinEntity eq null)

  • SalesOrder?$filter=TwinEntity/<next_navigation>/...<last_navigation> eq 'null'

The navigation or complex properties appearing in the $filter query option will be provided as follows in the filter expression tree:

  • <property name without '-'>:

    Name of a simple property or a navigation. In case of navigation the property value can only be 'null' and the operator is '=' or '<>'.

  • <property name containing one or more '-'>:

    Name before '-': This name can be the name of a complex property or a navigation.

    Name after '-': This name can be the name of a simple property or a complex property or a navigation.