Skip to content

Commit 1dc334b

Browse files
Merge pull request #199 from Trivadis/bugfix/violations-in-code-blocks
Fix/explain violations of unrelated guidelines
2 parents 8804b58 + 8877f3f commit 1dc334b

File tree

47 files changed

+658
-399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+658
-399
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ jobs:
1818
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
1919
- name: db* CODECOP Issues
2020
run: |
21-
export COP=4.3.0
22-
export VALIDATOR=4.3.0
21+
export COP=4.3.1
22+
export VALIDATOR=4.3.1
2323
wget https://github.com/Trivadis/plsql-cop-cli/releases/download/v$COP/tvdcc-$COP.zip
2424
unzip tvdcc-$COP.zip -d .
2525
wget -P tvdcc-$COP/plugin/ https://github.com/Trivadis/plsql-cop-validators/releases/download/v$VALIDATOR/sonar-plsql-cop-custom-validators-plugin-$VALIDATOR.jar
2626
echo $TVDCC_LIC | base64 -d > tvdcc-$COP/tvdcc.lic
27-
tvdcc-$COP/tvdcc.sh path=docs skip=none html=false excel=false validator=com.trivadis.tvdcc.validators.TrivadisGuidelines3Plus
27+
tvdcc-$COP/tvdcc.sh path=docs skip=0 html=false excel=false validator=com.trivadis.tvdcc.validators.TrivadisGuidelines3Plus
2828
env:
2929
TVDCC_LIC: ${{ secrets.TVDCC_LIC }}
3030
- name: SonarCloud Scan

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
/pdf
1414
/docs/9-appendix/PLSQL-and-SQL-Coding-Guidelines.pdf
1515

16+
# Files generated by tvdcc
17+
/tvdcc_report.json
18+
1619
# Distribution files
1720
/dist
1821
/mkdocs_material.egg-info

docs/3-coding-style/coding-style.md

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,37 @@ Rule | Description
1919
### Example
2020

2121
``` sql
22-
create or replace procedure set_salary(in_employee_id in employees.employee_id%type) is
23-
cursor c_employees(p_employee_id in employees.employee_id%type) is
24-
select last_name
25-
,first_name
26-
,salary
27-
from employees
28-
where employee_id = p_employee_id
29-
order by last_name
30-
,first_name;
31-
32-
r_employee c_employees%rowtype;
33-
l_new_salary employees.salary%type;
34-
begin
35-
open c_employees(p_employee_id => in_employee_id);
36-
fetch c_employees into r_employee;
37-
close c_employees;
38-
39-
new_salary(in_employee_id => in_employee_id
40-
,out_salary => l_new_salary);
41-
42-
-- Check whether salary has changed
43-
if r_employee.salary <> l_new_salary then
44-
update employees
45-
set salary = l_new_salary
46-
where employee_id = in_employee_id;
47-
end if;
48-
end set_salary;
22+
create or replace package body employee_api is
23+
procedure set_salary(in_employee_id in integer) is
24+
co_employee_id constant employees.employee_id%type := in_employee_id;
25+
26+
cursor c_employees(p_employee_id in employees.employee_id%type) is
27+
select last_name
28+
,first_name
29+
,salary
30+
from employees
31+
where employee_id = p_employee_id
32+
order by last_name
33+
,first_name;
34+
35+
r_employee c_employees%rowtype;
36+
l_new_salary employees.salary%type;
37+
begin
38+
open c_employees(p_employee_id => co_employee_id);
39+
fetch c_employees into r_employee;
40+
close c_employees;
41+
42+
new_salary(in_employee_id => in_employee_id
43+
,out_salary => l_new_salary);
44+
45+
-- Check whether salary has changed
46+
if r_employee.salary <> l_new_salary then
47+
update employees
48+
set salary = l_new_salary
49+
where employee_id = in_employee_id;
50+
end if;
51+
end set_salary;
52+
end employee_api;
4953
```
5054

5155
## Code Commenting

docs/4-language-usage/1-general/g-1020.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ begin
3838

3939
<<basic_loop>>
4040
loop
41-
exit basic_loop;
41+
exit basic_loop when true;
4242
end loop;
4343

4444
<<for_loop>>
@@ -78,7 +78,7 @@ begin
7878

7979
<<basic_loop>>
8080
loop
81-
exit basic_loop;
81+
exit basic_loop when true;
8282
end loop basic_loop;
8383

8484
<<for_loop>>

docs/4-language-usage/1-general/g-1040.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ Any part of your code, which is no longer used or cannot be reached, should be e
1313
declare
1414
co_dept_purchasing constant departments.department_id%type := 30;
1515
begin
16-
if 2 = 3 then
16+
if 2 = 3 then -- G-1050 violated, dead code detection works with literals only
1717
null; -- some dead code here
1818
end if;
1919

2020
null; -- some enabled code here
2121

2222
<<my_loop>>
2323
loop
24-
exit my_loop;
24+
exit my_loop when true;
2525
null; -- some dead code here
2626
end loop my_loop;
2727

2828
null; -- some other enabled code here
2929

3030
case
31-
when 1 = 1 and 'x' = 'y' then
31+
when 1 = 1 and 'x' = 'y' then -- G-1050 violated, dead code detection works with literals only
3232
null; -- some dead code here
3333
else
3434
null; -- some further enabled code here
@@ -40,7 +40,7 @@ begin
4040
from employees
4141
where department_id = co_dept_purchasing
4242
or commission_pct is not null
43-
and 5 = 6
43+
and 5 = 6 -- G-1050 violated, dead code detection works with literals only
4444
)
4545
-- "or commission_pct is not null" is dead code
4646
loop

docs/4-language-usage/1-general/g-1080.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,34 @@ This rule ignores operators `+`, `*` and `||`, and expressions: `1=1`, `1<>1`, `
1212
## Example (bad)
1313

1414
``` sql
15-
select emp.first_name
16-
,emp.last_name
17-
,emp.salary
18-
,emp.hire_date
19-
from employees emp
20-
where emp.salary > 3000
21-
or emp.salary > 3000
22-
order by emp.last_name,emp.first_name;
15+
declare
16+
co_max_salary constant emp.salary%type := 3000;
17+
begin
18+
select emp.first_name
19+
,emp.last_name
20+
,emp.salary
21+
,emp.hire_date
22+
from employees emp
23+
where emp.salary > co_max_salary
24+
or emp.salary > co_max_salary
25+
order by emp.last_name,emp.first_name;
26+
end;
27+
/
2328
```
2429

2530
## Example (good)
2631

2732
``` sql
28-
select emp.first_name
29-
,emp.last_name
30-
,emp.salary
31-
,emp.hire_date
32-
from employees emp
33-
where emp.salary > 3000
34-
order by emp.last_name,emp.first_name;
33+
declare
34+
co_max_salary constant emp.salary%type := 3000;
35+
begin
36+
select emp.first_name
37+
,emp.last_name
38+
,emp.salary
39+
,emp.hire_date
40+
from employees emp
41+
where emp.salary > co_max_salary
42+
order by emp.last_name,emp.first_name;
43+
end;
44+
/
3545
```

docs/4-language-usage/2-variables-and-types/1-general/g-2135.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ Expending resources calculating and assigning values to a local variable and nev
1313
create or replace package body my_package is
1414
procedure my_proc is
1515
co_employee_id constant employees.employee_id%type := 1042;
16+
co_hello constant type_up.text := 'Hello, ';
1617
l_last_name employees.last_name%type;
17-
l_message varchar2(100 char);
18+
l_message types_up.text;
1819
begin
1920
select emp.last_name
2021
into l_last_name
2122
from employees emp
2223
where emp.employee_id = co_employee_id;
2324

24-
l_message := 'Hello, ' || l_last_name;
25+
l_message := co_hello || l_last_name;
2526
exception
2627
when no_data_found then
2728
null; -- handle_no_data_found;
@@ -38,15 +39,16 @@ end my_package;
3839
create or replace package body my_package is
3940
procedure my_proc is
4041
co_employee_id constant employees.employee_id%type := 1042;
42+
co_hello constant type_up.text := 'Hello, ';
4143
l_last_name employees.last_name%type;
42-
l_message varchar2(100 char);
44+
l_message types_up.text;
4345
begin
4446
select emp.last_name
4547
into l_last_name
4648
from employees emp
4749
where emp.employee_id = co_employee_id;
4850

49-
l_message := 'Hello, ' || l_last_name;
51+
l_message := co_hello || l_last_name;
5052

5153
message_api.send_message(l_message);
5254
exception

docs/4-language-usage/2-variables-and-types/1-general/g-2145.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ There is no reason to assign a variable to itself. It is either a redundant stat
1111

1212
``` sql
1313
declare
14-
l_function_result pls_integer;
15-
l_parallel_degree pls_integer;
14+
co_parallel_degree constant types_up.name%type := 'parallel_degree';
15+
l_function_result pls_integer;
16+
l_parallel_degree pls_integer;
1617
begin
17-
l_function_result := maintenance.get_config('parallel_degree');
18-
l_parallel_degree := l_parallel_degree;
19-
do_something(l_parallel_degree);
18+
l_function_result := maintenance.get_config(co_parallel_degree);
19+
if l_function_result is not null then
20+
l_parallel_degree := l_parallel_degree;
21+
do_something(l_parallel_degree);
22+
end if;
2023
end;
2124
/
2225
```
@@ -25,12 +28,15 @@ end;
2528

2629
``` sql
2730
declare
28-
l_function_result pls_integer;
29-
l_parallel_degree pls_integer;
31+
co_parallel_degree constant types_up.name%type := 'parallel_degree';
32+
l_function_result pls_integer;
33+
l_parallel_degree pls_integer;
3034
begin
31-
l_function_result := maintenance.get_config('parallel_degree');
32-
l_parallel_degree := l_function_result;
33-
do_something(l_parallel_degree);
35+
l_function_result := maintenance.get_config(co_parallel_degree);
36+
if l_function_result is not null then
37+
l_parallel_degree := l_function_result;
38+
do_something(l_parallel_degree);
39+
end if;
3440
end;
3541
/
3642
```

docs/4-language-usage/2-variables-and-types/1-general/g-2180.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ Quoted identifiers make your code hard to read and maintain.
1111

1212
``` sql
1313
declare
14-
"sal+comm" integer;
15-
"my constant" constant integer := 1;
16-
"my exception" exception;
14+
"sal+comm" integer; -- violates also naming conventions (G-9102)
15+
"my constant" constant integer := 1; -- violates also naming conventions (G-9114)
16+
"my exception" exception; -- violates also naming conventsion (G-9113)
1717
begin
1818
"sal+comm" := "my constant";
19+
do_something("sal+comm");
1920
exception
2021
when "my exception" then
2122
null;
@@ -32,6 +33,7 @@ declare
3233
e_my_exception exception;
3334
begin
3435
l_sal_comm := co_my_constant;
36+
do_something(l_sal_comm);
3537
exception
3638
when e_my_exception then
3739
null;

docs/4-language-usage/2-variables-and-types/1-general/g-2185.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ You should ensure that the name you have chosen well defines its purpose and usa
1212
``` sql
1313
declare
1414
i integer;
15-
c constant integer := 1;
16-
e exception;
15+
c constant integer := 1; -- violates also naming conventions (G-9114)
16+
e exception; -- violates also naming conventions (G-9113)
1717
begin
1818
i := c;
19+
do_something(i);
1920
exception
2021
when e then
2122
null;
@@ -32,6 +33,7 @@ declare
3233
e_my_exception exception;
3334
begin
3435
l_sal_comm := co_my_constant;
36+
do_something(l_sal_comm);
3537
exception
3638
when e_my_exception then
3739
null;

0 commit comments

Comments
 (0)