-
Notifications
You must be signed in to change notification settings - Fork 286
/
zcl_demo_abap_cds_ve.clas.abap
451 lines (358 loc) · 16.5 KB
/
zcl_demo_abap_cds_ve.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"! <p class="shorttext"><strong>CDS view entities</strong><br/>ABAP cheat sheet example class</p>
"!
"! <p>The example class demonstrates CDS view entities.<br/>
"! Choose F9 in ADT to run the class.</p>
"!
"! <h2>Note</h2>
"! <ul>
"! <li>Example demonstrating CDS view entities. Refer to the CDS view entities used
"! here for more details.</li>
"! <li>Topics covered: Operands, expressions, built-in functions in the element list
"! of CDS view entities, input parameters, joins, associations.</li>
"! <li>Note: In ADT, hold CTRL and click on the CDS view entity in this example to
"! access the artifact. You can choose F8 there to open the data preview.</li>
"! <li>Find information on <strong>getting started with the example class</strong> and the
"! <strong>disclaimer</strong> in the ABAP Doc comment of class {@link zcl_demo_abap_aux}.</li></ul>
CLASS zcl_demo_abap_cds_ve DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES:
if_oo_adt_classrun.
CLASS-METHODS class_constructor.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_demo_abap_cds_ve IMPLEMENTATION.
METHOD class_constructor.
"Filling demo database tables.
zcl_demo_abap_aux=>fill_dbtabs( ).
"Some more database table insertions for this particular example
MODIFY zdemo_abap_carr FROM TABLE @( VALUE #(
( carrid = 'SQ'
carrname = 'Singapore Airlines'
currcode = 'SGD'
url = 'http://www.singaporeair.com ' )
( carrid = 'QF'
carrname = 'Qantas Airways'
currcode = 'AUD'
url = 'http://www.qantas.com.au' ) ) ).
MODIFY zdemo_abap_flsch FROM TABLE @( VALUE #(
( carrid = 'UA'
connid = 3517
countryfr = 'DE'
cityfrom = 'FRANKFURT'
airpfrom = 'FRA'
countryto = 'US'
cityto = 'NEW YORK'
airpto = 'JFK'
fltime = 495
deptime = '104000'
arrtime = '125500'
distance = 6162
distid = 'KM'
fltype = ''
period = 0 ) ) ).
MODIFY zdemo_abap_fli FROM TABLE @( VALUE #( ( carrid = 'UA' ) ) ).
ENDMETHOD.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP Cheat Sheet Example: CDS view entities\n\n| ).
out->write( `1) Operands, expressions and built-in functions ` &&
|in a CDS view entity\n\n| ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. All data is retrieved. The sample CDS view entity
"uses many operands, expressions and built-in functions to demonstrate
"various syntax options. In addition, the view contains an input
"parameter that must be provided with an actual parameter and
"specified in the ABAP SQL SELECT statement.
"In ADT, check out the CDS view entity by holding down CTRL and clicking
"on the CDS view entity. This will take you to the artifact. There you
"can choose F8 to open the data preview.
SELECT *
FROM zdemo_abap_cds_ve_sel( p_smax = 20 )
ORDER BY CarrierId
INTO TABLE @DATA(select_from_cds).
out->write( data = select_from_cds name = `select_from_cds` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) Aggregate Expressions` ) ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. All data is retrieved. The sample CDS view entity
"uses aggregate expressions.
SELECT *
FROM zdemo_abap_cds_ve_agg_exp
ORDER BY carrid
INTO TABLE @DATA(agg_expr).
out->write( data = agg_expr name = `agg_expr` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3) Joins` ) ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. All data is retrieved. The sample CDS view entity
"contains multiple joins.
"The CDS view entity is designed to contain different join variants
"in one artifact. There, you can comment in/out code sections to
"check out the individual join variants. Therefore, the result of
"the following SELECT statement depends on which section you have
"commented in in the CDS view entity.
SELECT *
FROM zdemo_abap_cds_ve_joins
ORDER BY carrid
INTO TABLE @DATA(cds_joins).
out->write( data = cds_joins name = `cds_joins` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `4) Excursion: ABAP SQL and joins` ) ).
"The following ABAP SQL SELECT statements are intended to reproduce
"the different joins that are performed by the CDS view entity.
"Inner, left and right outer, and cross joins are covered. The data
"sources for the SELECT statements are the database tables.
"Note:
"- The prefix ~ is used in the ABAP SQL statements instead of . in
" the CDS view entity.
"- To demonstrate the handling of null values, some SELECT statements
" contain the coalesce function and CASE expressions similar to the
" CDS view entity.
out->write( `---------- Inner join ----------` ).
out->write( |\n| ).
out->write( |\n| ).
SELECT _carr~carrid,
_carr~carrname,
_flsch_in~cityfrom AS cityfr_in,
_flsch_in~cityto AS cityto_in
FROM zdemo_abap_carr AS _carr
INNER JOIN zdemo_abap_flsch AS _flsch_in
ON _carr~carrid = _flsch_in~carrid
ORDER BY _carr~carrid
INTO TABLE @DATA(sql_inner_join).
out->write( data = sql_inner_join name = `sql_inner_join` ).
out->write( |\n| ).
out->write( `---------- Left outer join ----------` ).
out->write( |\n| ).
out->write( |\n| ).
SELECT _carr~carrid,
_carr~carrname,
_flsch_lo~cityfrom AS cityfr_lo,
coalesce( _flsch_lo~cityto, '???' ) AS cityto_lo
FROM zdemo_abap_carr AS _carr
LEFT OUTER JOIN zdemo_abap_flsch AS _flsch_lo
ON _carr~carrid = _flsch_lo~carrid
ORDER BY _carr~carrid
INTO TABLE @DATA(sql_left_outer_join).
out->write( data = sql_left_outer_join name = `sql_left_outer_join` ).
out->write( |\n| ).
out->write( `---------- Right outer join ----------` ).
out->write( |\n| ).
out->write( |\n| ).
SELECT _carr~carrid,
_carr~carrname,
CASE WHEN _carr~url IS NOT NULL THEN _carr~url
ELSE '!!!'
END AS url_ro,
_flsch_ro~cityfrom AS cityfr_ro,
_flsch_ro~cityto AS cityto_ro
FROM zdemo_abap_carr AS _carr
RIGHT OUTER JOIN zdemo_abap_flsch AS _flsch_ro
ON _carr~carrid = _flsch_ro~carrid
ORDER BY _carr~carrid
INTO TABLE @DATA(sql_right_outer_join).
out->write( data = sql_right_outer_join name = `sql_right_outer_join` ).
out->write( |\n| ).
out->write( `---------- Cross join ----------` ).
out->write( |\n| ).
out->write( |\n| ).
SELECT _carr~carrid,
_carr~carrname,
_flsch_cr~cityfrom AS cityfr_cr,
_flsch_cr~cityto AS cityto_cr
FROM zdemo_abap_carr AS _carr
CROSS JOIN zdemo_abap_flsch AS _flsch_cr
ORDER BY _carr~carrid
INTO TABLE @DATA(sql_cross_join).
out->write( data = sql_cross_join name = `sql_cross_join` ).
out->write( |\n| ).
"Just a check what join example is currently commented in
IF cds_joins = sql_inner_join.
out->write( `In the example CDS view entity, the inner join example is commented in.` ).
ELSEIF cds_joins = sql_left_outer_join.
out->write( `In the example CDS view entity, the left outer join example is commented in.` ).
ELSEIF cds_joins = sql_right_outer_join.
out->write( `In the example CDS view entity, the right outer join example is commented in.` ).
ELSEIF cds_joins = sql_cross_join.
out->write( `In the example CDS view entity, the cross join example is commented in.` ).
ELSE.
out->write( `In the example CDS view entity, there is some other code present.` ).
ENDIF.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Associations` ) ).
out->write( |5) Selecting data from a CDS view that contains associations\n\n| ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. All data is retrieved. The sample CDS view entity
"contains multiple associations.
"Some fields of some associations are used in the element list of the
"CDS view entity. This data is included in the result set. Some
"associations are exposed but no fields of those associations are
"included in the element list. Therefore, no join is instantiated on
"the database and no data from these exposed assocations is included
"in the result set.
SELECT *
FROM zdemo_abap_cds_ve_assoc
ORDER BY carrier
INTO TABLE @DATA(assoc).
out->write( data = assoc name = `assoc` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Using exposed associations in ABAP SQL statements: ...` ) ).
"The following examples use path expressions to access the association
"targets of exposed associations.
out->write( |6) ... SELECT clause\n\n| ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. The statement uses an exposed association.
"The SELECT list contains fields from the exposed association. Only in
"this case (when a consumer, such as an ABAP SQL statement, requests
"data) is the join instantiated on the database.
"Note:
"- No attributes are specified for the path expression (attributes
" are covered in examples further down). In particular, a join type is
" not explicitly specified. In such a case, the join type depends on
" the place where the path expression is used. Since the path expression
" is used in the SELECT list of an ABAP SQL SELECT statement (where
" fields are specified), a LEFT OUTER JOIN is used by default.
"- The coalesce function is included for a field to handle null values.
SELECT carrier,
\_carr3-carrname,
coalesce( \_carr3-url, '###' ) AS cityto_lo
FROM zdemo_abap_cds_ve_assoc
ORDER BY carrier
INTO TABLE @DATA(assoc_exp_select).
out->write( data = assoc_exp_select name = `assoc_exp_select` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `7) ... FROM clause` ) ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. All data is retrieved.
"In this case, an exposed association is used in the FROM clause.
"Note:
"- No join type is explicitly specified. As mentioned above, the
" position of the path expression is important. In this case (when
" used in the FROM clause), an INNER JOIN is used by default.
"- You can open the data preview for the CDS view entity used and
" compare the result set with the output here. Due to the inner
" join, non-existent 'carrid' values in the association target are
" not contained in the result set.
SELECT *
FROM zdemo_abap_cds_ve_assoc\_carr3 AS _exp
ORDER BY carrid
INTO TABLE @DATA(assoc_exp_from).
out->write( data = assoc_exp_from name = `assoc_exp_from` ).
out->write( |\n| ).
"The following ABAP SQL SELECT statement is intended to reproduce
"the data retrieval as above.
"The statement uses the same CDS view entity as data source that
"is used by the CDS view entity above as data source.
"An inner join is performed on a database table (the _carr3
"association from above has this table defined as the association
"target in the CDS view entity) since the inner join is used by
"default above so as to reproduce the effect. The result set
"should be the same as above.
SELECT _carr~mandt,
_carr~carrid,
_carr~carrname,
_carr~currcode,
_carr~url
FROM zdemo_abap_cds_ve_assoc_e AS _cds
JOIN zdemo_abap_carr AS _carr ON _cds~carrid = _carr~carrid
ORDER BY _carr~carrid
INTO TABLE @DATA(sql_repr).
out->write( data = sql_repr name = `sql_repr` ).
out->write( |\n| ).
IF sql_repr = assoc_exp_from.
out->write( `The result sets are the same.` ).
ELSE.
out->write( `The result sets are differrent.` ).
ENDIF.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `8) ... Specifying attributes` ) ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. The statement uses an exposed association.
"The SELECT list contains a path expression that is specified with
"an attribute.
"Note:
"- Cardinality, join types, and filter conditions can be used as
" attributes.
"- In the example, only the cardinality is specified.
"- The cardinality can be specified to prevent syntax warnings/errors
" in cases where the cardinality of the association does not match
" the way it is used in a path expression.
"- The example does not explicitly specify a join type. As mentioned
" above, if not explicitly specified, the join type depends on where
" the path expression is used. Here, a column is specified in the
" SELECT list. This means that a LEFT OUTER JOIN is used by default.
"- The result set should contain an entry for 'UA' having an initial
" value for 'fldate'.
SELECT carrid,
connid,
cityfrom,
cityto,
"Without specifying the cardinality, the following warning
"occurs: Using association "_FLI" can increase the cardinality
"of the results set
"\_fli-fldate AS flightdate
\_fli[ (*) ]-fldate AS flightdate
"The specification above corresponds to the following specification
"that includes an explicit specification of LEFT OUTER
"\_fli[ (*) LEFT OUTER ]-fldate AS flightdate
FROM zdemo_abap_cds_ve_assoc_e
ORDER BY carrid, connid, flightdate
INTO TABLE @DATA(assoc_attr_card).
out->write( data = assoc_attr_card name = `assoc_attr_card` ).
out->write( |\n| ).
"Specifying the join type explicitly
"- INNER, LEFT/RIGHT OUTER are possible
"- The join type can only be specified together with the cardinality.
"- In the result set of the example, the 'UA' entry should not be
" contained.
SELECT carrid,
connid,
cityfrom,
cityto,
\_fli[ (*) INNER ]-fldate AS flightdate
FROM zdemo_abap_cds_ve_assoc_e
ORDER BY carrid, connid, flightdate
INTO TABLE @DATA(assoc_attr_joty).
out->write( data = assoc_attr_joty name = `assoc_attr_joty` ).
out->write( |\n| ).
"Specifying conditions
"- Filter conditions can be specified for the current association
"- The addition WHERE is optional in cases where the filter condition
" is the only attribute specified in the square brackets.
"- When the association is instantiated as a join, the filter condition
" is transformed into an extended condition for the join.
"- In the example, a specific 'carrid' value is filtered for. LEFT OUTER
" is specified as join type explicitly. Not specifying the join type here
" has the same effect. The 'fldate' value is only retrieved for 'DL'
" entries. The other ones have initial values.
SELECT carrid,
connid,
cityfrom,
cityto,
\_fli[ (*) LEFT OUTER WHERE carrid = 'DL' ]-fldate AS flightdate
"The following has the same effect in this example
"\_fli[ (*) WHERE carrid = 'DL' ]-fldate as flightdate
FROM zdemo_abap_cds_ve_assoc_e
ORDER BY carrid, connid, flightdate
INTO TABLE @DATA(assoc_attr_where).
out->write( data = assoc_attr_where name = `assoc_attr_where` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `9) ... WHERE clause` ) ).
"The following ABAP SQL SELECT statement uses a CDS view entity as
"the data source. The statement uses an exposed association.
"The SELECT list and the WHERE clause contain a path expression.
SELECT carrid,
connid,
countryfr,
countryto,
\_carr_exp-carrname
FROM zdemo_abap_cds_ve_assoc_e
WHERE \_carr_exp-carrid LIKE 'A_'
ORDER BY carrid, connid
INTO TABLE @DATA(assoc_exp_where).
out->write( data = assoc_exp_where name = `assoc_exp_where` ).
ENDMETHOD.
ENDCLASS.