Search code examples
regexabapopensql

Match table rows by REST query-like patterns?


I am implementing ReSTful SICF services for a customer. The SICF service has a single handler (implementing IF_HTTP_EXTENSION~HANDLE_REQUEST) and this calls further API handling classes. The API handling class is stored against the incoming path like this

DDIC Table: ZAPI_HNLDRS
    /animals/:id           | ZCL_HANDLE_ANIMALS
    /animals/:id/tiger/:id | ZCL_HANDLE_TIGER

When the API is called /animals/:id/tiger/:id looks like /animals/4545152/tiger/1423331.

How do I make a call to ZAPI_HNDLRS to get ZCL_HANDLE_TIGER?

Of course, this won't help,

select single HANDLING_CLASS from ZAPI_HNDLRS
  into <wa> where uri = '/animals/4545152/tiger/1423331'

I believe I have to use REGEX in some form - I amn't sure how though. How do I match patterns in an ABAP select query?

update

The reason I have a table to store API handlers is because I want the whole process to be dynamic.

We could have newer urls that could be added over a period of time that could look like this.

Thanks for the answers that use SPLIT But that's still not fully dynamic. There could be multiple new urls that could come up in the future. E.g.

/animals/:id/tiger/:id/claws
/animals/:id/tiger/:id/claws/:id
:id - is an unique id

When such cases arise, there will be new handler entries in table ZAPI_HNDLRS.

Hence, I would need a generic logic to convert any incoming resource path. let's say: /animals/1234243242423/tiger/32423443344/claws to it's pattern that's stored /animals/:id/tiger/:id/claws So that I know the right handler to pick up from the table.


Solution

  • I think it is very difficult to do this in a single line of code. Here is a demo for your reference:

    DATA:
      lv_char_org    TYPE char120 VALUE '/animals/4545152/tiger/1423331',
      lv_char_target TYPE char120,
      lt_char_set    TYPE STANDARD TABLE OF char120.
    
    SPLIT lv_char_org AT '/' INTO TABLE lt_char_set.
    DELETE lt_char_set INDEX 1.
    
    LOOP AT lt_char_set INTO DATA(lv_char_set).
      IF sy-tabix MOD 2 EQ 1.
        lv_char_target = lv_char_target && '/' && lv_char_set && '/'.
      ELSE.
        lv_char_target = lv_char_target && 'id:'.
      ENDIF.
    ENDLOOP.
    
    WRITE lv_char_target.