I am developing a SAPUI5 application consuming oData services with SAP Gateway. I have implemented a search functionality which is producing a SQL where condition. One part of the condition looks like follows: ... OR DESCRIPTION LIKE '%searchString%'...
. In my database table I have a field DESCRIPTION
which is of type LCHR
length 32000
. The only problem is that the field DESCRIPTION
cannot be in WHERE
clause.
What would be a correct approach of searching long strings in database table via oData services? Do I need to hardcode the search functionality or is there some cleaner way?
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 funtion 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!