diff --git a/04_ABAP_Object_Orientation.md b/04_ABAP_Object_Orientation.md index 930b7eb..e88b7b7 100644 --- a/04_ABAP_Object_Orientation.md +++ b/04_ABAP_Object_Orientation.md @@ -46,6 +46,7 @@ - [Events](#events) - [Examples for Design Patterns: Factory Methods and Singletons](#examples-for-design-patterns-factory-methods-and-singletons) - [Class-Based Exceptions](#class-based-exceptions) + - [ABAP Unit Tests](#abap-unit-tests) - [ABAP Doc Comments](#abap-doc-comments) - [More Information](#more-information) - [Executable Examples](#executable-examples) @@ -4423,6 +4424,16 @@ obj_factory = class=>factory_method( par = ... ).
+### ABAP Unit Tests + +- [ABAP Unit](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_unit_glosry.htm) is a test tool integrated into the ABAP runtime framework. +- It can be used to run individual or mass tests, and to evaluate test results. +- In ABAP programs, individual unit tests are implemented as [test methods](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentest_method_glosry.htm) of local [test classes](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentest_class_glosry.htm). +- Find information on ABAP Unit tests in the [ABAP Unit Tests](14_ABAP_Unit_Tests.md) cheat sheet. + + + + ### ABAP Doc Comments - The ABAP Doc documentation tool allows you to add special ABAP Doc comments to ABAP source code for documentation. diff --git a/15_CDS_View_Entities.md b/15_CDS_View_Entities.md index c38031a..9b2de7c 100644 --- a/15_CDS_View_Entities.md +++ b/15_CDS_View_Entities.md @@ -8,9 +8,8 @@ Core data services (CDS) are an infrastructure for defining and consuming semantically rich data models on the [standard database](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstandard_db_glosry.htm) of an [AS ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenas_abap_glosry.htm). > **💡 Note**| Terms | Code Snippet | +
| + +Standalone and bound data types + + | + ++ +``` abap "----------------- Standalone and bound data types ----------------- "Standalone: Data type that is defined using the statement TYPES in an " ABAP program, as a data type of the ABAP Dictionary or as @@ -1550,7 +1645,21 @@ TYPES te_a_c10 TYPE c LENGTH 10. DATA do_a_c20 TYPE c LENGTH 20. DATA do_b_like LIKE do_a_c20. TYPES te_b_like LIKE do_a_c20. +``` + | +
| + +Complex and elementary data type/object + + | + ++ +``` abap "-------------- Complex and elementary data type/object -------------- "Elementary: Data type of fixed or variable length that is neither " structured, nor a table type or a reference type. @@ -1568,8 +1677,22 @@ DATA: BEGIN OF struc_a, comp4 TYPE string_table, "internal table comp5 TYPE REF TO i, "reference type END OF struc_a. +``` -"------------------ Complete and generic data types ------------------ + | +
| + +Complete and generic data type + + | + +
+
+``` abap
+"------------------ Complete and generic data type ------------------
"Complete: Non-generic data type
"Generic:
"- Data type that does not set all properties of a data object.
@@ -1598,8 +1721,22 @@ DATA(itab_a) = VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ).
ASSIGN do_e_c5 TO |
+
| + +Variable and constant data object + + | + ++ +``` abap +"------------------ Variable and constant data object ------------------ "Variable: Named data object whose value can be changed during the runtime " of an ABAP program. "Constant: Named data object whose value cannot be changed at runtime. @@ -1611,8 +1748,22 @@ do_g_i = 456. CONSTANTS con_a_i TYPE i VALUE 789. "Not changeable "con_a_i = 321. +``` -"--------------------- Static and dynamic data objects -------------------- + | +
| + +Static and dynamic data object + + | + ++ +```abap +"--------------------- Static and dynamic data object -------------------- "Static: "- Data object for which all attributes, including memory use, are specified " statically by the data type. @@ -1642,7 +1793,21 @@ do_h_c3 = 'defghi'. do_i_str = `abc`. do_i_str = `d`. do_i_str = `efghijklmnopqrstuvwxyz`. +``` + | +
| + +Static type and dynamic type + + | + ++ +``` abap "------------------------ Static type and dynamic type ----------------------- "Both are data types of a reference variable (reference type) that determine "the objects a reference variable can point to. @@ -1667,8 +1832,22 @@ dref_a_i = REF #( do_j_i ). "Only type i possible; the dynamic type is the same dref_b_data = REF #( do_j_i ). dref_b_data = REF #( do_k_str ). dref_b_data = REF #( dref_a_i ). +``` -"------------------------ Flat and deep data objects ----------------------- + | +
| + +Flat and deep data object + + | + ++ +``` abap +"------------------------ Flat and deep data object ----------------------- "Flat: "- Property of a data type, where the content of its data objects represents " the actual work data. @@ -1700,8 +1879,21 @@ DATA: BEGIN OF struc_c_deep, comp3 TYPE string, "string as deep data object comp4 TYPE string_table, "internal table as deep data object END OF struc_c_deep. +``` -"---------------------- Named and unnamed data object --------------------- + | +
| + +Named and unnamed data object + + | + ++ +``` abap "Named: Data object that can be identified via a name. "Unnamed: Data object that cannot be addressed by a name. Unnamed data " objects are literals and anonymous data objects. @@ -1725,7 +1917,21 @@ SELECT * FROM zdemo_abap_carr INTO TABLE NEW @DATA(dref_d_tab) UP TO 3 ROWS. +``` + | +
| + +Compatibility, convertibility and conversion regarding source and target data objects + + | + ++ +``` abap "--- Compatibility, convertibility and conversion regarding source and --- "--- target data objects ------------------------------------------------- "1. Source and target are compatible, all technical type properties @@ -1756,11 +1962,432 @@ DATA itab_str TYPE string_table. "Type conversion not possible "itab_str = some_number. "some_number = itab_str. -``` +``` + + | +
| Superclass of Exception Classes | Notes | +
| + +`TYPE` + + | + ++ + +``` abap +"------------ TYPE ---------------- + +"--- Elementary types/data objects --- + +"Built-in type +DATA elem1 TYPE string. +DATA elem2 TYPE i. +"Local elementary type +TYPES c3 TYPE c LENGTH 3. +DATA elem3 TYPE c3. +"Global elementary types (e.g. DDIC data elements, CDS simple types, +"elementary types declared in global classes/interfaces, components of +"local and global structures) +DATA elem4 TYPE abap_boolean. +DATA elem5 TYPE timestampl. +DATA elem6 TYPE zcl_demo_abap_dtype_dobj=>t_pub_text_c30. +DATA elem7 TYPE zdemo_abap_get_data_itf=>occ_rate. +DATA elem8 TYPE zdemo_abap_carr-carrid. +DATA elem9 TYPE zdemo_abap_carr_ve-carrname. + +"--- Structured types/data objects --- + +"Local structured type +TYPES: BEGIN OF struct_ty1, + comp1 TYPE i, + comp2 TYPE string, + comp3 TYPE c LENGTH 3, + END OF struct_ty1. + +DATA struct1 TYPE struct_ty1. + +DATA: BEGIN OF struct2, + comp1 TYPE xstring, + comp2 TYPE timestampl, + comp3 TYPE abap_boolean, + END OF struct2. + +"Global structured types (e.g. DDIC database tables, DDIC structures, +"CDS entities, structured types declared in global classes/interfaces) + +"Database table +DATA struct3 TYPE zdemo_abap_carr. +"View entity +DATA struct4 TYPE zdemo_abap_carr_ve. +TYPES struct_ty3 TYPE zdemo_abap_fli_ve. +DATA struct5 TYPE zdemo_abap_fli_ve. +"CDS abstract entity +TYPES struct_ty4 TYPE zdemo_abap_abstract_ent. +DATA struct6 TYPE zdemo_abap_abstract_ent. +"CDS table function +TYPES struct_ty5 TYPE zdemo_abap_table_function. +DATA struct7 TYPE zdemo_abap_table_function. +"Structured types declared in global classes/interfaces +DATA struct8 TYPE zcl_demo_abap_amdp=>carr_fli_struc. + +"--- Table types --- + +"Referring to global table types with TYPE +DATA itab1 TYPE string_table. +DATA itab2 TYPE xstring_table. +DATA itab3 TYPE string_hashed_table. + +"Table types declared in global classes/interfaces +DATA itab5 TYPE zcl_demo_abap_amdp=>carr_tab. +``` + + | +
| + +`TYPE TABLE OF` + + | + ++ + +``` abap +"------------ TYPE TABLE OF ---------------- + +"Table types and internal tables created with TYPE TABLE OF +"The examples only include standard tables. + +"Structured line types +"Local line/table type +TYPES: BEGIN OF struct_ty6, + comp1 TYPE i, + comp2 TYPE string, + comp3 TYPE c LENGTH 3, + END OF struct_ty6. +TYPES tab_ty1 TYPE TABLE OF struct_ty6 WITH EMPTY KEY. +DATA itab6 TYPE TABLE OF struct_ty6 WITH EMPTY KEY. + +"Globally available line types (e.g. DDIC database tables, CDS entites, etc.) +DATA itab7 TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY. +DATA itab8 TYPE TABLE OF zdemo_abap_fli_ve WITH EMPTY KEY. +DATA itab9 TYPE TABLE OF zdemo_abap_table_function WITH EMPTY KEY. +DATA itab10 TYPE TABLE OF zcl_demo_abap_amdp=>carr_fli_struc WITH EMPTY KEY. + +"Local and global elementary types +TYPES c20 TYPE c LENGTH 20. +DATA itab11 TYPE TABLE OF c20 WITH EMPTY KEY. +DATA itab12 TYPE TABLE OF string WITH EMPTY KEY. +DATA itab13 TYPE TABLE OF utclong WITH EMPTY KEY. +DATA itab14 TYPE TABLE OF zdemo_abap_carr-carrname WITH EMPTY KEY. +DATA itab15 TYPE TABLE OF zdemo_abap_get_data_itf=>occ_rate WITH EMPTY KEY. +``` + + | +
| + +`TYPE TABLE FOR/TYPE STRUCTURE FOR` + + | + +
+
+Find more information in the [ABAP for RAP: Entity Manipulation Language (ABAP EML)](08_EML_ABAP_for_RAP.md) cheat sheet.
+
+ + + +``` abap +"------------ TYPE TABLE FOR/TYPE STRUCTURE FOR ---------------- +"BDEF derived types in the context of RAP +DATA der_type1 TYPE TABLE FOR CREATE zdemo_abap_rap_ro_m. +TYPES der_type2 TYPE TABLE FOR UPDATE zdemo_abap_rap_ro_m. +DATA der_type3 TYPE TABLE FOR ACTION IMPORT zdemo_abap_rap_ro_m~multiply_by_2. +DATA der_type4 TYPE TABLE FOR READ IMPORT zdemo_abap_rap_ro_m. + +TYPES der_type5 TYPE STRUCTURE FOR DELETE zdemo_abap_rap_ro_m. +DATA der_type6 TYPE STRUCTURE FOR PERMISSIONS REQUEST zdemo_abap_rap_ro_m. +``` + + |
+
| + +`TYPE ... REF TO` + + | + ++ + +``` abap +"------------ TYPE ... REF TO ---------------- +"Reference types + +"Reference to elemtary/structured types, including the +"generic type data +DATA ref1 TYPE REF TO i. +DATA ref2 TYPE REF TO string. +DATA ref3 TYPE REF TO zdemo_abap_carr. +DATA ref4 TYPE REF TO data. + +"Classes and interfaces +DATA oref TYPE REF TO zcl_demo_abap_objects. +DATA iref TYPE REF TO zdemo_abap_get_data_itf. + +"Table types +DATA ref5 TYPE TABLE OF REF TO data. +DATA ref6 TYPE TABLE OF REF TO i. +DATA ref7 TYPE TABLE OF REF TO zdemo_abap_carr. +DATA ref8 TYPE TABLE OF REF TO zcl_demo_abap_objects. +``` + + | +
| + +`TYPE LINE OF` + + | + ++ + +``` abap +"------------ TYPE LINE OF ---------------- +"Structured types based on the line type of internal tables + +"Local table type +TYPES: BEGIN OF struct_ty7, + comp1 TYPE i, + comp2 TYPE string, + comp3 TYPE c LENGTH 3, + END OF struct_ty7. +TYPES tab_ty2 TYPE TABLE OF struct_ty7 WITH EMPTY KEY. + +DATA struct9 TYPE LINE OF tab_ty2. +DATA struct10 TYPE LINE OF zcl_demo_abap_oo_inheritance_1=>t_log. +``` + + | +
| + +`LIKE` + + | + ++ + +``` abap +"------------ LIKE ---------------- +"Types/data objects resusing types of other data objects + +"Elementary types/data objects +DATA copy_elem TYPE c LENGTH 3. + +DATA like_elem1 LIKE copy_elem. +TYPES like_elem2 LIKE copy_elem. +DATA like_elem3 LIKE space. + +DATA copy_struc1 TYPE zdemo_abap_carr. +DATA copy_struc2 TYPE zdemo_abap_fli_ve. + +DATA like_elem4 LIKE copy_struc1-carrid. +DATA like_elem5 LIKE copy_struc2-fldate. +DATA like_elem6 LIKE sy-index. + +"Structured types/data objects +DATA like_struc1 LIKE copy_struc1. +TYPES like_struc2 LIKE copy_struc2. + +"Table types/internal tables +DATA copy_tab1 TYPE TABLE OF string. +DATA copy_tab2 TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY. + +DATA like_tab1 LIKE copy_tab1. +TYPES like_tab2 LIKE copy_tab2. +``` + + | +
| + +`LIKE ...` + + | + ++ + +``` abap +"------------ LIKE ... ---------------- + +"--- Structured type/data object based on internal table using LIKE LINE OF --- +DATA like_line_tab1 TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY. +DATA like_line_tab2 TYPE TABLE OF string. + +DATA struc_like_line LIKE LINE OF like_line_tab1. +DATA elem_like_line LIKE LINE OF like_line_tab2. + +"--- Reference based on existing data object using LIKE REF TO --- +DATA str TYPE string. + +DATA ref9 LIKE REF TO str. +DATA ref10 LIKE REF TO struc_like_line. + +"--- Reference table based on existing data object using LIKE TABLE REF TO --- +DATA ref11 LIKE TABLE OF REF TO str. +DATA ref12 LIKE TABLE OF REF TO struc_like_line. +``` + + | +
| + +`BEGIN OF ENUM` + + | + ++ + +``` abap +"------------ BEGIN OF ENUM ---------------- +"Creating ABAP enumerated types and objects + +"a) No explicit base type is specified, base type of the constants is i by default, +" values are counted up (a = 0, b = 1, ...) +TYPES: BEGIN OF ENUM t_enum, + a, + b, + c, + END OF ENUM t_enum. + +"b) Explicit base type is specified, start values are provided (one of them must be initial) +TYPES: basetype TYPE c LENGTH 2, + BEGIN OF ENUM t_enum_base BASE TYPE basetype, + d VALUE IS INITIAL, + e VALUE 'ab', + f VALUE 'cd', + g VALUE 'ef', + END OF ENUM t_enum_base. + +"c) Optionally declaring an enumerated structure +TYPES: BEGIN OF ENUM t_enum_struc STRUCTURE en_struc BASE TYPE basetype, + h VALUE IS INITIAL, + i VALUE 'hi', + j VALUE 'ab', + k VALUE 'ap', + END OF ENUM t_enum_struc STRUCTURE en_struc. + + +DATA enum1 TYPE t_enum. +DATA enum2 TYPE t_enum_base. +DATA enum3 TYPE t_enum_struc. +DATA enum LIKE en_struc. +``` + + | +
| + +Excursion: Dynamically creating data objects using `CREATE DATA` + + | + +
+
+Find more information in the [Dynamic Programming](06_Dynamic_Programming.md) cheat sheet.
+
+ + +``` abap +"The examples show various statement patterns +DATA dref TYPE REF TO data. + + +"CREATE DATA dref TYPE (typename) ... +CREATE DATA dref TYPE ('STRING'). "Elementary data object +CREATE DATA dref TYPE ('ZDEMO_ABAP_CARR'). "Structure +CREATE DATA dref TYPE ('STRING_TABLE'). "Internal table + +"CREATE DATA dref TYPE ... TABLE OF (typename) ... +CREATE DATA dref TYPE TABLE OF ('STRING'). "Internal table +CREATE DATA dref TYPE SORTED TABLE OF ('ZDEMO_ABAP_FLI') WITH UNIQUE KEY carrid. + + +"CREATE DATA dref TYPE REF TO (typename). +CREATE DATA dref TYPE REF TO ('STRING'). +CREATE DATA dref TYPE REF TO ('ZDEMO_ABAP_FLI'). + +"CREATE DATA dref TYPE LINE OF (typename). +TYPES tab_ty3 TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY. +CREATE DATA dref TYPE LINE OF ('TAB_TY3'). + +"CREATE DATA dref LIKE struc-(compname). +DATA struct11 TYPE zdemo_abap_fli. +CREATE DATA dref LIKE struct11-('CARRID'). + +"CREATE DATA dref TYPE (absolute_name). +CREATE DATA dref TYPE ('\TYPE=STRING'). +"Getting an absolute type name; see more information further down +DATA(absolute_name) = cl_abap_typedescr=>describe_by_name( 'ZDEMO_ABAP_CARR' )->absolute_name. +CREATE DATA dref TYPE (absolute_name). + +"CREATE DATA dref TYPE HANDLE type_description_object. +"Getting a type description object. Find more information about RTTI below. +DATA(tdo_elem) = cl_abap_elemdescr=>get_c( 4 ). "type c length 4 +CREATE DATA dref TYPE HANDLE tdo_elem. +``` + + |
+
CL_BGMC_PROCESS_FACTORY IF_BGMC_OP_SINGLE_TX_UNCONTR interface.IF_BGMC_OP_SINGLE interface. Note: If you are in a RAP context, you do not need to implement COMMIT/ROLLBACK WORK because the RAP framework takes care of it.WAIT statements are included to have a self-contained example, and that all created database entries can be shown in the output. In the example, the background processing may be visualized, for example, by the MODIFY statement that is followed by a WAIT statement in the loop. The output can show that the entry for the first asynchronously created entry was added before a synchronously created entry. For more visualization options regarding the execution in the background, you can, for example, check the ABAP Cross Trace. For more information, refer to the documentation.IF_BGMC_OP_SINGLE_TX_UNCONTR interface.
+ - Using bgPF with transactional control, for example, if you work with a RAP application. In that case, you can implement the IF_BGMC_OP_SINGLE interface. Note: If you are in a RAP context, you do not need to implement COMMIT/ROLLBACK WORK because the RAP framework takes care of it.
+- More information:
+ - Background Processing Framework
+ - Transactional control with the controlled SAP LUW
+
+
+**Example 1: Using bgPF without transactional control**
+
+WAIT statements are included to have a self-contained example, and that all created database entries can be shown in the output. In the example, the background processing may be visualized, for example, by the MODIFY statement that is followed by a WAIT statement in the loop. The output can show that the entry for the first asynchronously created entry was added before a synchronously created entry. For more visualization options regarding the execution in the background, you can, for example, check the ABAP Cross Trace. For more information, refer to the documentation.
+