Meshes
When I came across Meshes first time, it took me some time to understand it completely so I will try to provide examples so that you can understand it easily .
So as mentioned by SAP these are special structures. Their components (referred to as nodes) are either structured internal tables or reference variables that point to structured internal tables.
The addition ASSOCIATION makes it possible to define associations (using ON conditions) between start nodes and target nodes of the mesh.
These semantic relationships between the mesh nodes can be evaluated in mesh paths. The mesh paths can be used in expressions and processing statements for meshes.
In simple words Mesh is to create complex structures which can linked and accessed through association and so that we can efficiently access records from a table which are dependent on other record present in different table.
Mesh is simply a instance of mesh types defined as :
TYPES BEGIN OF MESH
-------nodes --------
END OF MESH.
Lets start with example now to see how its actually done.
We will try to establish a relation between three tables :
lt_employee , lt_company , lt_experience
( Structure will be similar to what we have used till now with few extra columns )
TYPES : BEGIN OF ty_employee,
emp_name TYPE char20,
age TYPE i,
city TYPE char20,
company TYPE char20,
domain TYPE char20,
END OF ty_employee,
tt_employee TYPE STANDARD TABLE OF ty_employee
WITH DEFAULT KEY,
BEGIN OF ty_company,
company TYPE char20,
headquarter TYPE char20,
emp_count TYPE i,
END OF ty_company,
tt_company TYPE STANDARD TABLE OF ty_company
WITH DEFAULT KEY,
BEGIN OF ty_experience,
domain TYPE char20,
lang TYPE char20,
exp TYPE i,
END OF ty_experience,
tt_experience TYPE STANDARD TABLE OF ty_experience
WITH DEFAULT KEY.
DATA(lt_employee) = VALUE tt_employee(
( emp_name = 'RAM' age = '25' city = 'BANGALORE'
company = 'GOOGLE' domain = 'SAP' )
( emp_name = 'RAX' age = '26' city = 'DELHI'
company = 'MICROSOFT' domain = 'AWS' )
( emp_name = 'RAY' age = '27' city = 'MANGALORE'
( emp_name = 'RAX' age = '26' city = 'DELHI'
company = 'MICROSOFT' domain = 'AWS' )
( emp_name = 'RAY' age = '27' city = 'MANGALORE'
company = 'FLIPKART' domain = 'ML' )
).
DATA(lt_company) = VALUE tt_company(
( company = 'GOOGLE' headquarter = 'US'
emp_count = '100' )
( company = 'MICROSOFT' headquarter = 'US'
emp_count = '1000' )
( company = 'FLIPKART' headquarter = 'INDIA'
emp_count = '10000' )
).
DATA(lt_experience) = VALUE tt_experience(
( domain = 'SAP' lang = 'ABAP' exp = '5' )
( domain = 'AWS' lang = 'JAVA' exp = '6' )
( domain = 'ML' lang = 'PYTHON' exp = '7' )
).
).
DATA(lt_company) = VALUE tt_company(
( company = 'GOOGLE' headquarter = 'US'
emp_count = '100' )
( company = 'MICROSOFT' headquarter = 'US'
emp_count = '1000' )
( company = 'FLIPKART' headquarter = 'INDIA'
emp_count = '10000' )
).
DATA(lt_experience) = VALUE tt_experience(
( domain = 'SAP' lang = 'ABAP' exp = '5' )
( domain = 'AWS' lang = 'JAVA' exp = '6' )
( domain = 'ML' lang = 'PYTHON' exp = '7' )
).
Created Tables :
Mesh Creation:
TYPES : BEGIN OF MESH ty_mesh,
employee_d TYPE tt_employee
employee_d TYPE tt_employee
ASSOCIATION _emp_comp TO company_d ON company = company
" Association name can be anything,
I have used underscore as I am use to CDS views
ASSOCIATION _emp_exp TO experience_d ON domain = domain,
company_d TYPE tt_company,
experience_d TYPE tt_experience,
END OF MESH ty_mesh.
* If you understood above created associations
then it will be very easy to understand further
DATA : lt_mesh TYPE ty_mesh.
* Get all the table data in mesh
lt_mesh-employee_d = lt_employee.
lt_mesh-company_d = lt_company.
lt_mesh-experience_d = lt_experience.
After passing data to Mesh :
Now we will be fetching data using two types of association :
1. Forward association : Get data from child table using parent table
1. Forward association : Get data from child table using parent table
2. Inverse association : Get data from parent table using child table
We will access the data using mesh path
Syntax :
Node\association[ condition ]\assocation1[ ]\association2[ ] and so on.
3.8.1. Extract data from MESH inside loop with forward and reverse associations
LOOP AT lt_mesh-employee_d INTO DATA(ls_mesh_emp).
TRY.
* Access the company details
DATA(ls_company_details) =
lt_mesh-employee_d\_emp_comp[ lt_mesh-employee_d[ company =
ls_mesh_emp-company ] ].
WRITE :/ 'Company details with forward association',
/ ls_mesh_emp-emp_name , ls_company_details- company ,
ls_company_details-headquarter, ls_company_details-emp_count.
* Access the experience detail
DATA(ls_exp_details) =
lt_mesh-employee_d\_emp_exp[ lt_mesh-employee_d[ company =
ls_mesh_emp-company ] ].
WRITE :/ 'Experince Details with forward association',
/ ls_mesh_emp-emp_name , ls_exp_details-domain ,
ls_exp_details-lang, ls_exp_details-exp.
* Inverse association , get employee data from company association ,
use '^' to refer parent node of the dependent node
DATA(ls_emp_details1) =
lt_mesh-company_d\^_emp_comp~employee_d[ lt_mesh-company_d[ company =
ls_mesh_emp-company ] ].
WRITE :/ 'Company details with reverse association',
/ ls_mesh_emp-emp_name , ls_company_details-company ,
ls_company_details-headquarter, ls_company_details-emp_count.
CATCH cx_sy_itab_line_not_found. " Catch exception if no entry found
ENDTRY.
ENDLOOP.
Output of the above loop :
3.8.2. Another way to use reverse association
Get parent table data inside loop on child table .
Get parent table data inside loop on child table .
Below example :
*Get employee data from 'domain' inside loop on experience table.
WRITE / 'Exployee data with reverse association using experience table'.
" Taken object reference just to show in a different way
LOOP AT lt_mesh-experience_d REFERENCE INTO DATA(lo_exp).
* Inverse association , get employee data from company association ,
use '^' to refer parent node of the dependent node
DATA(ls_emp) = lt_mesh-experience_d\^_emp_exp~employee_d[
lt_mesh-experience_d[ domain = lo_exp->domain ] ].
WRITE : / ls_emp-emp_name, ls_emp-age ,ls_emp-domain,
lo_exp->lang, lo_exp->exp.
ENDLOOP.
WRITE : / ls_emp-emp_name, ls_emp-age ,ls_emp-domain,
lo_exp->lang, lo_exp->exp.
ENDLOOP.
Output :
3.7.3. Without any loop
Get Company data of employee working in FLIPKART
" forward association
DATA(ls_comp) = lt_mesh-employee_d\_emp_comp[ lt_mesh- employee_d[ company = 'FLIPKART' ] ].
Output :
Get Employee data from company having total 10000 employee
DATA(ls_empl) = lt_mesh-company_d\^_emp_comp~employee_d[
lt_mesh-company_d[ emp_count = '10000' ] ]. " inverse association
Output :
Output :
No comments:
Post a Comment