Sie sind auf Seite 1von 39

Week 1: Database Development

Unit 2: Core Data Services


Core Data Services
Development artifact

 New development artifact extension in


SAP HANA SPS 06 – hdbdd (data dictionary)
– changed to hdbcds in HDI with SPS 11
 Defines reusable types, multiple entities
(tables), and views all within one source file
 Multiple catalog objects are generated upon
activation

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Core Data Services
User-defined types

 Individual types as well as structured types


 A structured type is a data type comprising
a list of attributes, each of which has its
own data type
 The attributes of the structured type can be
defined manually in the structured type
itself, and reused either by another
structured type or an entity

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Core Data Services
Entities and associations

 An entity is the core artifact for persistence-


model definition using the CDS syntax
 A CDS entity is a table with a set of data
elements that are organized using columns
and rows
 Associations define relationships between
entities (tables)
 Associations are specified by adding an
element to a source entity with an association
type that points to a target entity,
complemented by optional information
defining cardinality and which keys to use

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 4


Core Data Services
Views

 CDS syntax expands to include the definition


of views
 A view is a virtual table, not persisted, based
on the dynamic results returned in response
to a SQL statement
 Views can utilize the defined associations
between entities
 Supports the use of functions and
expressions as well as aggregation and
filtering

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 5


Core Data Services
HDI-incompatible changes

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 6


Core Data Services
Incompatible changes

 In general, CDS works in HDI like it does in the SAP HANA repository. There are some incompatible
changes, however.

The following annotations are not supported in HDI:


@Schema
@Catalog (index and tableType)
@SearchIndex
@SearchIndexes
@nokey
@GenerateTableType
@WithStructuredPrivilegeCheck

 For all annotations that have been replaced by regular syntax in HDI, the syntax variant is also available and
recommended in the repository

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 7


Core Data Services
Schema handling

 The annotation "Schema" is not supported in HDI


 Schema is handled automatically via the HDI
container

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 8


Core Data Services
Index, tableType, and @Catalog

 The "Catalog" annotation is not supported in HDI


 In order to specify indexes, use the corresponding
syntax in the technical configuration (see Index)
 The specification of tableType is split: the storage
type (row/column) is specified in the technical
configuration section (see Row/Column); in order to
get a temporary table, use the keyword "temporary
entity"

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 9


Core Data Services
Fulltext index, @SearchIndex and @SearchIndexes

 The annotations "SearchIndex" and "SearchIndexes"


are not supported in HDI
 In order to define full-text indexes, use the
corresponding syntax in the technical configuration
(see Full-text index)

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 10


Core Data Services
Keyless entities

 In order to define an entity without key elements, in


the repository the annotation "nokey" had to be used
 In HDI, it is possible to define an entity without key
elements without using an annotation
 The annotation "nokey" is not supported in HDI

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 11


Core Data Services
@GenerateTableType

 In the repository, for each structured CDS type, an SAP HANA table type was generated by default. The
generation of table types could be controlled explicitly via the "GenerateTableType" annotation.
 In HDI, no SAP HANA table type is generated for a structured type by default. To enforce generation of an
SAP HANA table type, use the keyword "table type" instead of "type":

type Struc1 { ... }; // no HANA table type is generated

table type Struc2 { ... }; // a corresponding HANA table type is created

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 12


Core Data Services
Access control via DCL

 The annotation view MyView as select from Foo {


"WithStructuredPrivilegeCheck" is not <select_list>
supported in HDI. Use the syntax variant } <where_groupBy_Having_OrderBy> with
instead structured privilege check;

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 13


Core Data Services
New features

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 14


Core Data Services
Technical configuration

 The definition of an entity can contain a section entity MyEntity {


called "technical configuration" <element_definitions>
 In this section, you can specify } technical configuration {
<technical_configurations>
– storage type (row/column)
};
– indexes
– full-text indexes
– partitioning (HDI only)
– grouping (HDI only)
– unload priority (HDI only)
 The syntax in the technical configuration section
is as close as possible to the corresponding
clauses in the HANA SQL Create Table statement
 Each clause in the technical configuration must
end with a semicolon

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 15


Core Data Services
Storage type

 In the technical configuration for an entity, the entity MyEntity {


storage type (row or column) for the generated key id : Integer;
table can be specified a : Integer;
 Default: if no storage type is specified, a column } technical configuration {
store table is generated row store;
};
 In repo, this is an alternative way to specifying
the storage type via the annotation
Catalog.tableType. It is not possible to use both
the annotation and the technical configuration at
the same time to define the storage type for an
entity
 In HDI, the storage type can only be specified
via the technical configuration. The respective
annotation is not supported

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 16


Core Data Services
Index

 In repo, this is an alternative way to entity MyEntity {


specifying the index via the index annotation. key id : Integer;
It is not possible to use both the index a : Integer;
annotation and the technical configuration at b : Integer;
the same time to define indexes for an entity c : Integer;
 In HDI, indexes can only be specified via the s {
technical configuration. The index annotation m : Integer;
is not supported n : Integer;
};
} technical configuration {
index MyIndex1 on (a, b) asc;
unique index MyIndex2 on (c, s) desc;
};

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 17


Core Data Services
Full-text index

 The <fulltext_parameter_list> is basically identical to the entity MyEntity {


standard HANA SQL syntax for CREATE FULLTEXT key id : Integer;
INDEX t : String(100);
 A fuzzy search index corresponds to specifying "FUZZY s {
SEARCH INDEX ON" for a table column u : String(100);
};
 It is not possible to specify both a full-text index and a } technical configuration {
fuzzy search index for the same element fulltext index MyFTI1 on (t)
 In repo, this is an alternative way to specifying full-text <fulltext_parameter_list>;
indexes via the annotations SearchIndex and fuzzy search index on (s.u);
SearchIndexes. It is not possible to use both the };
annotations and the technical configuration at the same
time to define full-text indexes for an entity
 In HDI, full-text indexes can only be specified via the
technical configuration. The respective annotations are not
supported. The full-text parameters CONFIGURATION
and TEXT MINING CONFIGURATION are not supported
© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 18
Core Data Services
Partitioning

 In the technical configuration for an entity, the entity MyEntity {


partitioning information for the generated table key id : Integer;
can be specified a : Integer;
 The <partition_clause> is identical to the } technical configuration {
respective clause in the standard HANA SQL partition by hash (id) partitions 2,
CREATE TABLE statement range (a) (partition 1 <=
values < 10, partition values = 10,
 The partition clause is only supported in HDI partition others);
};

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 19


Core Data Services
Grouping

 In the technical configuration for an entity, the entity MyEntity {


grouping information for the generated table can key id : Integer;
be specified a : Integer;
 The <group_option> is identical to the respective } technical configuration {
clause in the standard HANA SQL CREATE group type Foo group subtype Bar
TABLE statement group name myGroup;
};
 The group option clause is only supported in HDI

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 20


Core Data Services
Unload priority

 In the technical configuration for an entity, entity MyEntity {


the unload priority for the generated table <element_list>
can be specified } technical configuration {
 The unload priority clause is only supported unload priority <integer_literal>;
in HDI };

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 21


Core Data Services
Using constants in queries and associations

 Constants could already be defined before SPS11,


but their usage was restricted to annotation
assignments and default values for entity elements
 With SPS11, constants can also be used in views
and in the ON-condition of unmanaged associations
 When constants are used in a view definition, their
name must be prefixed with the scope operator ":".
Usually, names that appear in a query are resolved
as alias or element names. The scope operator
makes the compiler resolve the name outside of the
query

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 22


Core Data Services
Cardinality in filters

 When an infix filter effectively reduces the entity Person {


cardinality of a to-N association to to-1, this can key id : Integer;
name : String(100);
be made explicit in the filter address : Association[*] to Address on
 This allows to use the association also in address.personId = id;
the WHERE clause, where to-N associations };
are not allowed
entity Address {
key id : Integer;
personId : Integer;
type : String(20); // home, business,
vacation, ...
street : String(100);
city : String(100);
};

view V as select from Person {


name
} where address[1: type='home' ].city
= 'Hamburg';

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 23


Core Data Services
Calculated fields

 The definition of an entity can contain calculated fields


 The values for calculated fields are not persisted in the database, but only computed on access, i.e.
when the element is selected. Basically, when the field is used in a select statement, it is replaced by
the expression
 The calculation expression can contain arbitrary expressions and SQL functions
 Note: in SAP HANA tables, you can define columns with the addition "GENERATED ALWAYS AS".
These columns are physically present in the table and all the values are actually stored. They behave
like ordinary columns, with the difference that their value is computed upon insertion rather than
specified in the INSERT statement. This is in contrast to calculated fields: For them, no values are
actually stored. The values are computed upon SELECT
 Restrictions
– must not contain other calculated fields, associations, aggregations, or subqueries
– cannot be a key
– no index can be defined
– cannot be used as foreign key for a managed association

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 24


Core Data Services
JOIN

CDS now supports the JOIN clause in entity E {


key id : Integer;
view definitions. The following JOIN a : Integer;
types are supported: };
 [ INNER ] JOIN entity F {
 LEFT [ OUTER ] JOIN key id : Integer;
b : Integer;
 RIGHT [ OUTER ] JOIN };
 FULL [ OUTER ] JOIN
entity G {
 CROSS JOIN key id : Integer;
c : Integer;
};
view V_join as select from E join
(F as X full outer join G on X.id = G.id)
on E.id = c {
a, b, c
};

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 25


Core Data Services
LIMIT & OFFSET

LIMIT & OFFSET is used to return a


subset of the selected rows
 LIMIT value defines the number of rows
returned after OFFSET has been applied
 OFFSET will skip the first n rows of the
selected dataset

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 26


Core Data Services
TOP

Support for the SQL clause TOP in


query of view definition
 Limits result set to TOP n rows

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 27


Core Data Services
DISTINCT

CDS now supports SELECT DISTINCT view V_dist as


semantic. Note the position of the select from E distinct { a };
DISTINCT keyword directly in front of
the curly brace

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 28


Core Data Services
GENERATED {ALWAYS | BY DEFAULT} AS IDENTITY

GENERATED {ALWAYS | BY DEFAULT }


AS IDENTITY is used to create identity
columns with auto-increment functionality
 Creates a sequence automatically
 If ALWAYS is specified, values are always
generated; if BY DEFAULT is specified, the
values are generated by default, but can be
overridden

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 29


Core Data Services
GENERATED ALWAYS AS <Expression>

GENERATED ALWAYS AS <Expression> is


used to create calculated columns
 An element which is defined corresponds to a
field in the database table that is present in the
persistence and has a value
 Since the value is computed on INSERT, the
expression cannot contain non-deterministic
functions

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 30


Core Data Services
Subquery support

Same subquery functionality provided by


SAP HANA core SQL brought to the CDS
layer

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 31


Core Data Services
@Odata.publish

@Odata.publish allows the generation of


generic OData services
 No need for separate xsodata file
 Consumable via Java module
 Consumable via JavaScript module in a future
support package

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 32


Core Data Services
Multiple top-level artifacts

Up to now, a CDS file could contain only a single


top-level artifact. As a consequence, in order
to define several artifacts in a single source file,
you had to use a context as top-level artifact and
put all other artifacts inside that context. Since
multiple top-level artifacts are now allowed, there
is no longer any requirement that the name of the
CDS source file must be the same as the name
of a top-level artifact. You can choose any name
for the CDS source file

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 33


Core Data Services
View with parameters

CDS views can now have parameters


 Must be defined in the view definition before the
query block
 Parameters must be typed with built-in primitive,
user-defined scalar types, or use “Type of”
operator
 Parameters can be used at any position in a
query where an expression is allowed. A
parameter is referred to inside a query by
prefixing the parameter name either with the
scope operator ':' or "$parameters“
 Runtime object is a table function

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 34


Core Data Services
New view – SYS.CDS_ANNOTATION_ASSIGNMENT

The new view


SYS.CDS_ANNOTATION_ASSIGNMENTS
in the CDS catalog API returns annotation
values in a flat manner. This is in contrast to
the view CDS_ANNOTATION_VALUES,
where values of structured annotations are
returned in structured form as a JSON value

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 35


Core Data Services
Comments

Allows developer to insert comments into the source code of the CDS artifact via annotations.
Column-level comment is transferred to the runtime object in the catalog.

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 36


Core Data Services
Selecting from table functions

Developer can now define a CDS


view which selects from a native
table function which was defined
using .hdbfunction artifact

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 37


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.

Das könnte Ihnen auch gefallen