In this exercise, you push the calculation of free seats and the concatenation of airline ID and airline name down to the database.
Task 1: Copy Template (Optional)
Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_SOLUTION.
Steps
Copy class /LRN/CL_S4D401_DBS_NESTED_JOIN to a class in your own package (suggested name: ZCL_##_SOLUTION, where ## stands for your group number).
In the Project Explorer view, right-click class /LRN/CL_S4D401_DBS_NESTED_JOIN to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_SOLUTION, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
Activate the copy.
Press Ctrl + F3 to activate the class.
Task 2: Push Down Calculation
In local class LCL_PASSENGER_FLIGHT, extend the row type of static attribute flight_buffer with a component seats_free. Adjust the implementation of method GET_FLIGHTS_BY_CARRIER and add the calculation of free seats to the SELECT statement. Then adjust the implementation of the CONSTRUCTOR method. Add the calculation to the SELECT SINGLE statement and use the calculation result to fill instance attribute seats_free.
Steps
In local class LCL_PASSENGER_FLIGHT, navigate to the definition of structure type ST_FLIGHTS_BUFFER.
For example, you can open the Local Types tab in the editor and search for BEGIN OF st_flights_buffer.
Alternatively, you can expand ZCL_##_SOLUTION → LCL_PASSENGER_FLIGHT in the Outline view on the left and choose ST_FLIGHTS_BUFFER.
After component seats_occupied, add a new component seats_free of the same type as type as instance attribute seats_free.
Adjust the code as follows:
12345678910111213
TYPES: BEGIN OF st_flights_buffer,
carrier_id TYPE /lrn/passflight-carrier_id,
connection_id TYPE /lrn/passflight-connection_id,
flight_date TYPE /lrn/passflight-flight_date,
plane_type_id TYPE /lrn/passflight-plane_type_id,
seats_max TYPE /lrn/passflight-seats_max,
seats_occupied TYPE /lrn/passflight-seats_occupied,
seats_free TYPE i,
price TYPE /lrn/passflight-price,
currency_code TYPE /lrn/passflight-currency_code,
END OF st_flights_buffer.
Navigate to the implementation of method GET_FLIGHTS_BY_CARRIER and add the calculation of the free seats to the field list of the SELECT statement.
Caution
Make sure the calculation is at the same position as the new component of structure type ST_FLIGHTS_BUFFER. Alternatively, you can use addition CORRESPONDING FIELDS OF in the INTO clause.Adjust the code as follows:
12345678910
SELECT
FROM /lrn/passflight
FIELDS carrier_id, connection_id, flight_date,
plane_type_id, seats_max, seats_occupied,
seats_max - seats_occupied AS seats_free,
price, currency_code
WHERE carrier_id = @i_carrier_id
INTO TABLE @flights_buffer.
Navigate to the implementation of method CONSTRUCTOR and add the calculation of the free seats to the field list of the SELECT SINGLE statement.
Adjust the code as follows:
123456789101112
SELECT SINGLE
FROM /lrn/passflight
FIELDS plane_type_id,
seats_max, seats_occupied,
seats_max - seats_occupied AS seats_free,
price, currency_code
WHERE carrier_id = @i_carrier_id
AND connection_id = @i_connection_id
AND flight_date = @i_flight_date
INTO CORRESPONDING FIELDS OF @flight_raw .
Scroll down to the value assignment to instance attribute seats_free. Replace the arithmetic expression on the right-hand side of the assignment operator with the content of flight_raw-seats_free.
Adjust the code as follows:
1234567
planetype = flight_raw-plane_type_id.
seats_max = flight_raw-seats_max.
seats_occ = flight_raw-seats_occupied.
* seats_free = flight_raw-seats_max - flight_raw-seats_occupied.
seats_free = flight_raw-seats_free.
Task 3: Push Down String Concatenation
In local class LCL_CARRIER, adjust the implementation of the CONSTRUCTOR method. Move the concatenation of the airline ID and the airline name to the field list of the SELECT SINGLE statement and store the result in instance attribute name.
Steps
In local class LCL_CARRIER, navigate to the implementation of the CONSTRUCTOR method.
Perform this step as before.
Locate the value assignment for the name attribute. Analyze it, then add a comment sign at the beginning of the code row.
Scroll down to statement name = carrier_id && ` ` && name.
Place the cursor anywhere in this code row and press Ctrl + <.
In the field list of the SELECT SINGLE statement, replace database table field NAME with a call of a suitable string processing function to concatenate the ID and the name of the airline separated by a single space.
Note
Alternatively, you can copy the string expression carrier_id && ` ` && carrier_name
to the field list. However, be aware that string literals (with back quotes) are not allowed in ABAP SQL.Adjust the code as follows:
12345678
SELECT SINGLE
FROM /lrn/carrier
* FIELDS name, currency_code
FIELDS concat_with_space( carrier_id, name, 1 ), currency_code
WHERE carrier_id = @i_carrier_id
INTO ( @me->name, @me->currency_code ).
Activate and test your global class as console app.
Press Ctrl + F3.
Press F9.