[ABAP] MESH – Beziehungen zw. internen Tabellen abbilden (JOIN)

* https://answers.sap.com/questions/64735/what-are-meshes-really-good-for.html
* https://l3consulting.de/meshes-in-abap/
* http://zevolving.com/2015/05/abap-740-meshes-a-new-complex-type-of-structures/
* http://zevolving.com/2015/05/abap-740-mesh-path-forward-and-inverse-association/

* Demoprogramme:
*   DEMO_MESH_DELETE_NUMBERS
*   DEMO_MESH_DELETE_TABLE_NUMBERS
*   DEMO_MESH_EXPRESSIONS_FLIGHTS
*   DEMO_MESH_EXPRESSIONS_NUMBERS
*   DEMO_MESH_FORWARD_ASSOC
*   DEMO_MESH_FOR_FLIGHTS
*   DEMO_MESH_INSERT_BUILD_TABLE
*   DEMO_MESH_INSERT_NUMBERS
*   DEMO_MESH_INVERSE_ASSOC
*   DEMO_MESH_LOOP_AT_FLIGHTS
*   DEMO_MESH_LOOP_AT_NUMBERS
*   DEMO_MESH_MODIFY_NUMBERS
*   DEMO_MESH_MODIFY_TABLE_NUMBERS
*   DEMO_MESH_PACK
*   DEMO_MESH_REFLEXIVE_ASSOC_LOOP
*   DEMO_MESH_REFLEXIVE_ASSOC_SNGL
*   DEMO_MESH_REFLEXIVE_ASSOC_TREE
*   DEMO_MESH_SET_ASSOC_NUMBERS

* Orte
TYPES: BEGIN OF ty_orte,
         key_ort TYPE i,
         ort     TYPE string,
       END OF ty_orte.

* Personen
TYPES: BEGIN OF ty_personen,
         key_ort TYPE i,
         name    TYPE string,
         alter   TYPE i,
       END OF ty_personen.

TYPES: ty_it_orte TYPE SORTED TABLE OF ty_orte WITH UNIQUE KEY key_ort.
TYPES: ty_it_personen TYPE SORTED TABLE OF ty_personen WITH NON-UNIQUE KEY key_ort.

* MESH mit Assoziation
TYPES: BEGIN OF MESH ty_mesh,
         orte     TYPE ty_it_orte ASSOCIATION orte_to_personen TO personen ON key_ort = key_ort,
         personen TYPE ty_it_personen,
       END OF MESH ty_mesh.

DATA: it_mesh TYPE ty_mesh.

INITIALIZATION.
* Daten einpflegen
  it_mesh-orte = VALUE #( ( key_ort = 1 ort = 'FRA' )
                          ( key_ort = 2 ort = 'BER' )
                          ( key_ort = 3 ort = 'DRS' )
                          ( key_ort = 4 ort = 'MUN' )
                          ( key_ort = 5 ort = 'DUS' ) ).

  it_mesh-personen = VALUE #( ( key_ort = 1 name = 'Udo'      alter = 35 )
                              ( key_ort = 4 name = 'Horst'    alter = 60 )
                              ( key_ort = 3 name = 'Inge'     alter = 70 )
                              ( key_ort = 1 name = 'Elfriede' alter = 85 )
                              ( key_ort = 5 name = 'Florian'  alter = 16 ) ).

START-OF-SELECTION.

  WRITE: / 'Orte:'.
  LOOP AT it_mesh-orte ASSIGNING FIELD-SYMBOL(<o>).
    WRITE: / <o>-key_ort, <o>-ort.
  ENDLOOP.

  ULINE.

  WRITE: / 'Personen:'.
  LOOP AT it_mesh-personen ASSIGNING FIELD-SYMBOL(<p>).
    WRITE: / <p>-key_ort, <p>-name, <p>-alter.
  ENDLOOP.

  ULINE.

  TRY.
* Forward Association
      WRITE: / 'Forward Association Ort->Person ( key = 1 )'.
      DATA(lv_p) = it_mesh-orte\orte_to_personen[ it_mesh-orte[ key_ort = 1 ] ].
      WRITE: / lv_p-key_ort, lv_p-name, lv_p-alter.
    CATCH cx_root.
  ENDTRY.

  TRY.
* Inverse Assocition
      WRITE: / 'Inverse Assocition Ort->Personen->Ort( key_ort = 3 )'.
      DATA(lv_o) = it_mesh-personen\^orte_to_personen~orte[ it_mesh-personen[ key_ort = 3 ] ] .
      WRITE: / lv_o-key_ort, lv_o-ort.
    CATCH cx_root.
  ENDTRY.