CDS view是SAP目前推出的下一代database views,SAP认为传统的database view只是将多张表进行关联,CDS View是一个成熟的Data Model,它有很多SE11中所没有的新特性,并且可以被SAP产品外的Application使用。
1 在Eclipse中创建CDS View
右击新建ABAP Object,选择CSD->Data Definition
点击下一步往后,在此页面可以选择模板,其中
${sql_view_name}
是将来在SE11中生成的view的名称2 CDS View代码
2.1 Define View / Define View with Join
使用模板生成的代码如下
@AbapCatalog.sqlViewName: 'ZV_MONSTERS'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Monster CDS views'
define view zcds_monsters as select from ztmonster_header as
monster_header
inner join ztmonster_items as monster_items
on monster_header.db_key = monster_items.db_key
left outer join ztmonster_pets as monster_pets
on monster_header.monster_number = monster_pets.owner
{
key monster_header.monster_number as monster_number ,
monster_header.name as monster_name ,
LPAD ( monster_header.tax_number, 10 , '0' ) as hat_size ,
SUBSTRING ( monster_header.name, 1 , 1 ) as first_initial ,
case monster_header.evilness
when 'BANKER_STYLE_EVIL' then 'REALLY SCARY'
when 'EVIL' then
case monster_header.strength
when 'REALLY STRONG ' then 'SCARY'
else ' NOT SO SCARY REALLY '
end
when 'SLIGHTLY_EVIL' then 'SLIGHTLY SCARY '
else 'NOT REALLY SCARY AT ALL '
end as scariness
}
where monster_header.sanity < 10
and monster_header.color = 'GREEN'
以@开头的为注解,除了默认的注解,可以自己添加比如buffer的注解,按F1可以获得注解的介绍
@AbapCatalog.buffering.status : #ACTIVE
@AbapCatalog.buffering.type : #SINGLE
CSD veiw中,可以使用 inner jion / left jion等join命令,相比ABAP定义更广泛了,通过as添加别名,使得看起来更具有实际意义。CDS中访问表的字段使用点号.
而不是传统ABAP中的波浪号~
。在SE11中我们定义字段时,左边是human-friendly names, 比如CUSTOMER / CUSTOMER_NAME,而右边是 data element ,比如 KUNNR and NAME1,CDS 中只定义human-friendly names,其类型有所取出的数据库表字段类型自动定义。这有点类似于ABAP中通过DATA( )
来定义数据的结构。所有的字段都放在{}
中,在select jion关键词后,在where关键词前。上面例子中,通过LPAD可以将tax_number的值比如3
补全成0000000003
,通过SUBSTRING来取首字母。字段定义还可以用case语句。最后跟where条件语句,cds中可以用IS NULL / IS NOT NULL
来判断是否为空。
激活后,在se11中可以查看view
2.2 Define View with Association
SAP希望大家更多的是用association而不是jion,这更体现了概念思维,assocation不仅仅是jion,它是数据字典存储的一种可能的jion连接的metadata,实际的jion在association被使用时才创建。
两张表通过外键链接对我们来说很容易理解,但这其实与实际不匹配,比如我们下3个不同类型订单时,我们对header数据和item数据并没有有意思地去区别,因为通常概念中,很明显一个订单要有item数据,否则是没有意义的。再比如通过表LFA1/LFM1寻找supplier的purchasing related data,通过association,我们可以用Supplier.PurchasingData.BlockingFlag
而不是LFM1 ~ SPERM
。当你创建了association view连接起address和customer,就可以在order item的customer中重用这个association view。
Association View定义如下,monster可能没有pets,也可能有多个,因此设为[0..*]
,header和item的关系可以设成[1..*]
,如果jion两个主键字段,可以用[1..1]
。另外,SAP推荐assocation的名字前面加下划线来区分CDS view中标准字段。
@AbapCatalog.sqlViewName: 'zv_monsters_ass'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Monsters CDS View with Association'
define view Zcds_Monsters_Association as select from ztmonster_header as monster
association [0..*] to ztmonster_pets as _Pet
on monster.monster_number = _Pet.owner {
key monster.monster_number as MonsterNumber ,
key _Pet.pet_number as PetNumber ,
_Pet.owner as Owner ,
monster.name as OwnerName ,
_Pet.pet_name as Name ,
_Pet.pet_type as Type,
_Pet.pet_species as Species,
_Pet // Make association public
}
2.3 Define View with Parameters
前面建立的CDS view,其中where条件是hard-code的,我们希望能通过参数来指定where条件,比如传统ABAP中
SELECT * FROM zv_monster_pars
WHERE color EQ p_color
AND sanity IN s_sanity .
可以通过如下代码定义参数,参数后面是其data element。
@AbapCatalog.sqlViewName: 'zv_monsters_pars'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Monster CDS View with Parameters'
define view Zcds_Monsters_Parameters
with parameters p_sanity_low : ZDE_MONSTER_SANITY ,
p_sanity_high: ZDE_MONSTER_SANITY ,
p_color : ZDE_MONSTER_COLOR
as select from ztmonster_header as monster {
key monster.monster_number as MonsterNumber,
monster.name as name ,
monster.color as color ,
monster.sanity as sanity ,
monster.strength as strength
}
where monster.color = :p_color and
sanity between :p_sanity_low and :p_sanity_high
在程序中可以通过如下代码来调用
SELECT * FROM zcds_monsters_parameters(
p_color = @p_color ,
p_sanity_low = @s_sanity- l ow,
p_sanity_high = @s_sanity-high )
INTO TABLE @DATA(colorful _mad_monster_tablel)
WHERE strength~ 'REALLY_STRONG '.
2.4 Extend View
在SE11中,我们通过append对table或view进行增强,在CDSview中,对原有的CDS View进行增强可以如下
@AbapCatalog.sqlViewAppendName: 'ZV_MONSTERS_EXT'
@EndUserText.label: 'Monster CDS view extension'
extend view ZCDS_MONSTERS with ZCDS_MONSTERS_EXTENSION {
monster_header.age,
monster_header.monster_count as counts
}
增强后,原CDS View将有增强标记
在原始view中,可以看到append的字段
2.5 Table Function
定义table function如下,table function必须将clinet作为参数传入,returns中的字段即为返回的字段,取数逻辑放在AMDP Class的方法中。
@EndUserText.label: 'Table Function'
define table function ZCDS_tablefun
with parameters @Environment.systemField: #CLIENT
clnt : abap.clnt
returns {
client : s_mandt;
carrname : s_carrname;
connid : s_conn_id;
city_from : s_from_cit;
city_to : s_to_city;
}
implemented by method ZLL_DEMO_AMDP_FUNCTIONS=>GET_flights;
定义AMDP class,继承接口if_amdp_marker_hdb
,
CLASS zll_demo_amdp_functions DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb .
CLASS-METHODS get_flights FOR TABLE FUNCTION zcds_tablefun .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zll_demo_amdp_functions IMPLEMENTATION.
METHOD get_flights
BY DATABASE FUNCTION FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING scarr spfli.
RETURN SELECT sc.mandt as client, sc.carrname, sp.connid, sp.cityfrom, sp.cityto
from scarr as sc
inner join spfli as sp on sc.mandt = sp.mandt and
sc.carrid = sp.carrid
ORDER BY sc.carrname, sp.connid;
endmethod.
ENDCLASS.
2.6 Authority Checks
在DDL中,我们配置@AccessControl.authorizationCheck: #CHECK
,一个完整的CDS View需要 DDL + DCL
选择模板,代码如下
@EndUserText.label: 'Authority Check for CDS view'
@MappingRole: true
define role Zdcl_Monsters_Parameters {
grant
select
on
Zcds_Monsters_Parameters
where
// condition;
// -- Example WHERE condition
// -- Two-field mapping to PFCG authorization with filter on read authorization
( color ) = aspect pfcg_auth( z_mon_col, color, ACTVT = '03' ) ;
// and
// -- Equals-or-initial operator
// ( CustomerCountry ) ?= aspect pfcg_auth( S_ACM_DEMO, SACMCNTRY )
// or
// -- Reference to the logged on users name
// CreatedBy = aspect User
// or
// -- Literal condition
// isPublic = 'X';
}