Skip to content

Commit a3fa5fc

Browse files
authored
add new check for 'Prefer INSERT INTO TABLE to APPEND TO' (#623)
* added check for Prefer INSERT INTO TABLE to APPEND #415 * change documentation path * added docs * Delete prefer-insert-into-to-append.md * add docs * Update check_documentation.md * not emit findings for append ... to ... sorted by * Update src/y_check_prefer_insert_into.clas.abap
1 parent 1cd7cb0 commit a3fa5fc

5 files changed

+290
-0
lines changed

docs/check_documentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [Prefer CASE to ELSEIF](checks/prefer-case-to-elseif.md)
4646
- [Prefer RETURNING to EXPORTING](checks/prefer-returning-to-exporting.md)
4747
- [Prefer IS NOT to NOT IS](checks/prefer-is-not-to-not-is.md)
48+
- [Prefer INSERT INTO TABLE to APPEND TO](checks/prefer-insert-into-to-append.md)
4849
- [Prefer LINE_EXISTS or LINE_INDEX to READ TABLE or LOOP AT](checks/prefer-line-exists.md)
4950
- [Prefer NEW to CREATE OBJECT](checks/prefer-new-to-create-object.md)
5051
- [Prefer Pragmas to Pseudo Comments](checks/prefer-pragmas-to-pseudo-comments.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[code pal for ABAP](../../README.md) > [Documentation](../check_documentation.md) > [Prefer INSERT INTO TABLE to APPEND TO](prefer-insert-into-to-append.md)
2+
3+
## Prefer INSERT INTO TABLE to APPEND TO
4+
5+
### What is the intent of the check?
6+
7+
This check searches for `APPEND` statements and reports a finding. `INSERT INTO TABLE` works with all table and key types, thus making it easier for you to refactor the table's type and key definitions if your performance requirements change.
8+
9+
### How to solve the issue?
10+
11+
Use `INSERT INTO` instead of `APPEND TO`.
12+
13+
### What to do in case of exception?
14+
15+
In exceptional cases, you can suppress this finding by using the pseudo comment `"#EC PREF_INSERT_INT`:
16+
17+
```abap
18+
DATA prefer_insert_into_table TYPE TABLE OF string.
19+
APPEND `example` TO prefer_insert_into_table. "#EC PREF_INSERT_INT
20+
```
21+
22+
### Example
23+
24+
Before the check:
25+
26+
```abap
27+
DATA prefer_insert_into_table TYPE TABLE OF string.
28+
APPEND `example` TO prefer_insert_into_table.
29+
```
30+
31+
After the check:
32+
33+
```abap
34+
DATA prefer_insert_into_table TYPE TABLE OF string.
35+
INSERT `example` INTO TABLE prefer_insert_into_table.
36+
```
37+
38+
### Further Readings & Knowledge
39+
40+
* [Clean ABAP - Prefer INSERT INTO TABLE to APPEND TO](https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-insert-into-table-to-append-to)
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CLASS y_check_prefer_insert_into DEFINITION
2+
PUBLIC
3+
INHERITING FROM y_check_base
4+
FINAL
5+
CREATE PUBLIC .
6+
7+
PUBLIC SECTION.
8+
METHODS constructor.
9+
PROTECTED SECTION.
10+
METHODS: inspect_tokens REDEFINITION.
11+
PRIVATE SECTION.
12+
ENDCLASS.
13+
14+
15+
16+
CLASS y_check_prefer_insert_into IMPLEMENTATION.
17+
18+
METHOD constructor.
19+
super->constructor( ).
20+
21+
settings-pseudo_comment = '"#EC PREF_INSERT_INT' ##NO_TEXT.
22+
settings-disable_threshold_selection = abap_true.
23+
settings-threshold = 0.
24+
settings-documentation = |{ c_docs_path-checks }prefer-insert-into-to-append.md|.
25+
26+
set_check_message( 'Prefer INSERT INTO TABLE to APPEND TO.' ).
27+
28+
ENDMETHOD.
29+
30+
METHOD inspect_tokens.
31+
32+
IF get_token_abs( statement-from ) <> 'APPEND'.
33+
RETURN.
34+
ENDIF.
35+
36+
IF next2( p_word1 = 'SORTED'
37+
p_word2 = 'BY' ) IS NOT INITIAL.
38+
RETURN.
39+
ENDIF.
40+
41+
DATA(check_configuration) = detect_check_configuration( statement ).
42+
43+
raise_error( statement_level = statement-level
44+
statement_index = index
45+
statement_from = statement-from
46+
check_configuration = check_configuration ).
47+
48+
ENDMETHOD.
49+
50+
ENDCLASS.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
CLASS ltc_append_to DEFINITION INHERITING FROM y_unit_test_base FOR TESTING
2+
DURATION SHORT
3+
RISK LEVEL HARMLESS.
4+
PROTECTED SECTION.
5+
METHODS: get_cut REDEFINITION,
6+
get_code_with_issue REDEFINITION,
7+
get_code_without_issue REDEFINITION,
8+
get_code_with_exemption REDEFINITION.
9+
10+
ENDCLASS.
11+
12+
13+
CLASS ltc_append_to IMPLEMENTATION.
14+
15+
METHOD get_code_without_issue.
16+
17+
result = VALUE #(
18+
( ' REPORT y_example.' )
19+
20+
( ' CLASS y_example DEFINITION.' )
21+
( ' PUBLIC SECTION.' )
22+
( ' METHODS example.' )
23+
( ' ENDCLASS.' )
24+
25+
( ' CLASS y_example IMPLEMENTATION.' )
26+
( ' METHOD example.' )
27+
( ' DATA prefer_insert_into_table TYPE TABLE OF string.' )
28+
( ' INSERT `example` INTO TABLE prefer_insert_into_table.' )
29+
( ' ENDMETHOD.' )
30+
( ' ENDCLASS.' )
31+
).
32+
33+
ENDMETHOD.
34+
35+
METHOD get_code_with_exemption.
36+
37+
result = VALUE #(
38+
( ' REPORT y_example.' )
39+
40+
( ' CLASS y_example DEFINITION.' )
41+
( ' PUBLIC SECTION.' )
42+
( ' METHODS example.' )
43+
( ' ENDCLASS.' )
44+
45+
( ' CLASS y_example IMPLEMENTATION.' )
46+
( ' METHOD example.' )
47+
( ' DATA prefer_insert_into_table TYPE TABLE OF string.' )
48+
( ' APPEND `example` TO prefer_insert_into_table. "#EC PREF_INSERT_INT' )
49+
( ' ENDMETHOD.' )
50+
( ' ENDCLASS.' )
51+
).
52+
53+
ENDMETHOD.
54+
55+
METHOD get_code_with_issue.
56+
57+
result = VALUE #(
58+
( ' REPORT y_example.' )
59+
60+
( ' CLASS y_example DEFINITION.' )
61+
( ' PUBLIC SECTION.' )
62+
( ' METHODS example.' )
63+
( ' ENDCLASS.' )
64+
65+
( ' CLASS y_example IMPLEMENTATION.' )
66+
( ' METHOD example.' )
67+
( ' DATA prefer_insert_into_table TYPE TABLE OF string.' )
68+
( ' APPEND `example` TO prefer_insert_into_table.' )
69+
( ' ENDMETHOD.' )
70+
( ' ENDCLASS.' )
71+
).
72+
73+
ENDMETHOD.
74+
75+
METHOD get_cut.
76+
result ?= NEW y_check_prefer_insert_into( ).
77+
ENDMETHOD.
78+
79+
ENDCLASS.
80+
81+
CLASS ltc_append_initial_line DEFINITION INHERITING FROM ltc_append_to FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
82+
PROTECTED SECTION.
83+
METHODS get_code_with_issue REDEFINITION.
84+
ENDCLASS.
85+
86+
CLASS ltc_append_initial_line IMPLEMENTATION.
87+
88+
METHOD get_code_with_issue.
89+
result = VALUE #(
90+
( ' REPORT y_example.' )
91+
92+
( ' CLASS y_example DEFINITION.' )
93+
( ' PUBLIC SECTION.' )
94+
( ' METHODS example.' )
95+
( ' ENDCLASS.' )
96+
97+
( ' CLASS y_example IMPLEMENTATION.' )
98+
( ' METHOD example.' )
99+
( ' DATA prefer_insert_into_table TYPE TABLE OF string.' )
100+
( ' FIELD-SYMBOLS <example> TYPE string.' )
101+
( ' APPEND INITIAL LINE TO prefer_insert_into_table ASSIGNING <example>.' )
102+
( ' ENDMETHOD.' )
103+
( ' ENDCLASS.' )
104+
).
105+
ENDMETHOD.
106+
107+
ENDCLASS.
108+
109+
CLASS ltc_append_lines_of DEFINITION INHERITING FROM ltc_append_to FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
110+
PROTECTED SECTION.
111+
METHODS get_code_with_issue REDEFINITION.
112+
ENDCLASS.
113+
114+
CLASS ltc_append_lines_of IMPLEMENTATION.
115+
116+
METHOD get_code_with_issue.
117+
result = VALUE #(
118+
( ' REPORT y_example.' )
119+
120+
( ' CLASS y_example DEFINITION.' )
121+
( ' PUBLIC SECTION.' )
122+
( ' METHODS example.' )
123+
( ' ENDCLASS.' )
124+
125+
( ' CLASS y_example IMPLEMENTATION.' )
126+
( ' METHOD example.' )
127+
( ' DATA prefer_insert_into_table TYPE TABLE OF string.' )
128+
( ' DATA example_table TYPE TABLE OF string.' )
129+
( ' APPEND LINES OF example_table TO prefer_insert_into_table.' )
130+
( ' ENDMETHOD.' )
131+
( ' ENDCLASS.' )
132+
).
133+
ENDMETHOD.
134+
135+
ENDCLASS.
136+
137+
CLASS ltc_append_sorted_by DEFINITION INHERITING FROM ltc_append_to FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
138+
PROTECTED SECTION.
139+
METHODS get_code_without_issue REDEFINITION.
140+
ENDCLASS.
141+
142+
CLASS ltc_append_sorted_by IMPLEMENTATION.
143+
144+
METHOD get_code_without_issue.
145+
result = VALUE #(
146+
( ' REPORT y_example.' )
147+
148+
( ' CLASS y_example DEFINITION.' )
149+
( ' PUBLIC SECTION.' )
150+
( ' METHODS example.' )
151+
( ' ENDCLASS.' )
152+
153+
( ' CLASS y_example IMPLEMENTATION.' )
154+
( ' METHOD example.' )
155+
( ' DATA prefer_insert_into_table TYPE TABLE OF sflight.' )
156+
( ' DATA example_line TYPE sflight.' )
157+
( ' APPEND example_line TO prefer_insert_into_table SORTED BY fldate.' )
158+
( ' ENDMETHOD.' )
159+
( ' ENDCLASS.' )
160+
).
161+
ENDMETHOD.
162+
163+
ENDCLASS.
164+
165+
166+
CLASS ltc_select DEFINITION INHERITING FROM ltc_append_to FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
167+
PROTECTED SECTION.
168+
METHODS get_code_without_issue REDEFINITION.
169+
ENDCLASS.
170+
171+
CLASS ltc_select IMPLEMENTATION.
172+
173+
METHOD get_code_without_issue.
174+
result = VALUE #(
175+
( ' REPORT ut_test.' )
176+
( ' START-OF-SELECTION.' )
177+
( ' DATA example_table TYPE TABLE OF sflight.' )
178+
( ' SELECT * FROM sflight APPENDING TABLE example_table.' )
179+
).
180+
ENDMETHOD.
181+
182+
ENDCLASS.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>Y_CHECK_PREFER_INSERT_INTO</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Prefer INSERT INTO TABLE to APPEND TO</DESCRIPT>
9+
<STATE>1</STATE>
10+
<CLSCCINCL>X</CLSCCINCL>
11+
<FIXPT>X</FIXPT>
12+
<UNICODE>X</UNICODE>
13+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
14+
</VSEOCLASS>
15+
</asx:values>
16+
</asx:abap>
17+
</abapGit>

0 commit comments

Comments
 (0)