ABAP CDS Releases CDS-Native Data Types: CDS Simple Types
source link: https://blogs.sap.com/2023/06/30/abap-cds-releases-cds-native-data-types-cds-simple-types/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ABAP CDS Releases CDS-Native Data Types: CDS Simple Types
CDS simple types allow developers to define elementary data types directly in ABAP CDS. CDS simple types provide all the functionality of DDIC data elements, plus they can be enriched with domain-specific metadata using CDS annotations. When a simple type is used in ABAP or ABAP CDS for typing and casting, it comes with its complete metadata. Therefore, CDS simple types provide a powerful means for code reuse on the smallest possible level, namely the elementary data type. Read this blog post for more details.
Release info: CDS simple types have been first published with ABAP release 7.91, which corresponds to 7.58 on-prem and 2302 SAP BTP ABAP Environment.
Comparison of DDIC data elements and CDS simple types
DDIC data elements | CDS simple types | |
Reuse capabilities | Reusable data types for typing in ABAP and in ABAP CDS. | Reusable data types for typing in ABAP and CDS that can be nested with each other. |
Metadata | Technical settings in SE11. | CDS annotations for domain-specific metadata |
Definition | Form-based editor and DDIC DDL | Syntax-based definition in ADT |
Framework support | Supported by technologies such as WebDynpro and SAP GUI. | Supported by frameworks such as RAP and the ABAP Analytical Engine. |
Defining a CDS simple type
Defining a CDS simple type is really easy. You can specify some annotations and then all you have to do is give it a name and a type after the statement DEFINE TYPE.
[@type_annot1]
[@type_annot2]
...
DEFINE TYPE simple_type : typing
Here’s an example:
@EndUserText.label: ‘This is a CDS simple type’
DEFINE TYPE myType : abap.int4;
Typing of a CDS simple type
A CDS simple type can be typed using a DDIC data element, a built-in data type, or using another simple type.
-
Typing with a DDIC data element
When a CDS simple type is typed with a DDIC data element, the technical and semantic properties of the data element are inherited by the CDS simple type and translated into annotation values. The following table shows the mapping of DDIC properties to simple types:
DDIC property | CDS simple type annotation |
Conversion routing | @AbapCatalog.typeSpec.conversionExit |
Lowercase letters | @ObjectModel.upperCase |
Flag for change document | @AbapCatalog.typeSpec.changeDocumentRelevant |
Field label, Heading | @EndUserText.heading |
Field label, Medium | @EndUserText.label |
Short Text | @EndUserText.quickInfo |
Here’s an example of inheritance from a data element to a CDS simple type. The simple type DEMO_SIMPLE_TYPE_INHERITANCE is typed using a DDIC data element.
define type demo_simple_type_inheritance: demo_bt_inheritance;
The Active Annotations View in ADT shows that all annotations derived from the underlying data element and from the DDIC domain are translated into annotation values:
Active Annotations View
2. Typing using a built-in data type
A CDS simple type can be typed using a built-in data type. Almost all built-in data types from ABAP Dictionary are possible. Here’s an example:
@EndUserText.heading: 'abc'
@EndUserText.label: 'myLabel'
@EndUserText.quickInfo: 'lala'
define type demo_simple_type : abap.int4;
3. Typing with another simple type
A CDS simple type can be typed with another simple type. Annotations are inherited, but it is also possible to overwrite the inherited annotation values.
With simple type hierarchies, reuse takes place on the level of elementary data types. Therefore, simple types provide a multi-level reuse concept.
Here’s an example:
First layer simple type:
@EndUserText.heading: 'abc'
@EndUserText.label: 'myLabel'
@EndUserText.quickInfo: 'lala'
define type demo_simple_type : abap.int4;
Second simple type that is based on the first simple type:
define type DEMO_SIMPLE_TYPE_2 : DEMO_SIMPLE_TYPE;
Using a CDS simple type
A CDS simple type can be used in ABAP CDS for typing of elements and parameters and for casting. In ABAP, CDS simple types can be used for type declarations after the TYPE statement. Here are some examples:
-
Using a CDS simple type in a CDS view entity
The following CDS view entity uses a CDS simple type in a cast expression and for typing the input parameter p1.
@EndUserText.label: 'CDS view entity, simple types'
define view entity DEMO_CDS_SIMPLE_TYPE_USAGE
with parameters
p1 : demo_simple_type
as select from demo_expressions
{
key id as Id,
num1 as Num1,
char1,
$parameters.p1 as Parameter1,
cast( char2 as demo_simple_type_de ) as cast_bt
}
The view entity can be evaluated using methods of the CDS read API (IF_DD_CDS_READ_API_ENTITY) and using the annotation API (CL_DD_DDL_ANNOTATION_SERVICE) as follows:
DATA:
lo_api_param TYPE REF TO if_dd_cds_read_api_parameter.
TRY.
DATA(result) = cl_dd_cds_read_api_factory=>create(
iv_stobname = 'DEMO_CDS_SIMPLE_TYPE_USAGE' ).
DATA(result_header) = result->get_header( ).
lo_api_param = CAST #( result ).
DATA(lt_param) = lo_api_param->get_all( ).
CATCH cx_dd_cds_read_api.
ENDTRY.
cl_dd_ddl_annotation_service=>get_annos_4_element(
EXPORTING
entityname = 'DEMO_CDS_SIMPLE_TYPE_USAGE'
elementname = 'Parameter1'
IMPORTING
annos = FINAL(annos) ).
The type properties of the input parameter P1 are evaluated. The data type and length are derived from the simple type and there’s a dedicated field that points to the simple type. The console output might looks as follows:
The annotations of the field Parameter1 are derived from the simple type that was used for typing. The console output might look as follows:
2. Using a CDS simple type in an ABAP program
Simple types can be used for typing in ABAP programs after the TYPES statement in the same way as DDIC data elements. Here’s an example:
DATA MyType TYPE demo_simple_type.
MyType = 1.
cl_demo_output=>display( MyType ).
Outlook
- ABAP Cloud is a new development model that aims at modernizing and simplifying the ABAP programming language. It takes place entirely in ADT and relies on ABAP CDS for the data modeling layer. Therefore, there is a lot of investment in ABAP CDS features to provide maximum ease of use.
- CDS simple types are another step in the modernization of the ABAP language. ABAP Dictionary is receding into the background, while users are encouraged to use ABAP CDS.
- Another CDS-native data type will be made available soon: The CDS enumerated type to define globally available enumerated types. Be prepared!
Further information
- Simple Types | SAP Help Portal, ABAP Data Models guide
- ABAP CDS – Simple Types, ABAP Keyword Documentation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK