LIKE filtering of LCHR field in OData service

后端 未结 1 432
暖寄归人
暖寄归人 2021-01-07 07:15

I am developing a UI5 application consuming OData services with SAP Gateway, where I have implemented a search functionality which is producing an SQL where conditi

相关标签:
1条回答
  • 2021-01-07 07:22

    The answer is CDS Table Function. The basic idea is to push the search down to HANA which is implemented by AMDP. I did some local testing to answer this by concrete code.

    I created a table zza_test

    @EndUserText.label : 'TEST'
    @AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
    @AbapCatalog.tableCategory : #TRANSPARENT
    @AbapCatalog.deliveryClass : #A
    @AbapCatalog.dataMaintenance : #LIMITED
    define table zza_test {
        key mandt : mandt not null;
        key bukrs : bukrs not null;
        cnt       : abap.int4;
        des       : abap.lchr(2000);
    }
    

    Defined a CDS table function zza_test_tf with parameter search_str.

    @EndUserText.label: 'TEST TF'
    define table function zza_test_tf
    with parameters
        @Environment.systemField: #CLIENT
           clnt   :abap.clnt,
           search_str : char255
    returns
    {
        mandt : mandt;
        bukrs : bukrs;
        cnt   : abap.int4;
        des   : abap.lchr(2000);
    
    }
    implemented by method zcl_zza_test_tf=>search_des;
    

    Created a Class zcl_zza_test_tf with interface if_amdp_marker_hdb and implement the method search_des

    class zcl_zza_test_tf definition
    public
    final
    create public .
    
    public section.
      interfaces if_amdp_marker_hdb.
      class-methods search_des
    
          for table function zza_test_tf.
     endclass.
    
    class zcl_zza_test_tf implementation.
       
       method search_des by database function for hdb
          language sqlscript
          options read-only
          using zza_test.
       
          return select mandt,bukrs, cnt, des
                    from zza_test where des like concat( concat( '%', 
                       :search_str), '%');
    
       endmethod.
    
    endclass.
    

    Now we have a CDS table function with parameters. Run the CDS view and input the search parameter to get the result. You can even define your HANA SQL in your AMDP implementation with fuzzy search.

    To fulfill the requirement, you implement the Open SQL select on this CDS view at your GateWay layer.

    data:
        lt_test type standard table of ZZA_TEST_TF.
    
    select * from ZZA_TEST_TF( search_str = 'DUM' ) into table @lt_test.
    

    Everything should work. Hope it helps. Thank you!

    0 讨论(0)
提交回复
热议问题