Skip to content

Feature/issue 206: simplify G-1050 #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/4-language-usage/1-general/g-1040.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Any part of your code, which is no longer used or cannot be reached, should be e
declare
co_dept_purchasing constant departments.department_id%type := 30;
begin
if 2 = 3 then -- G-1050 violated, dead code detection works with literals only
if 2 = 3 then -- dead code detection works with literals only
null; -- some dead code here
end if;

Expand All @@ -28,7 +28,7 @@ begin
null; -- some other enabled code here

case
when 1 = 1 and 'x' = 'y' then -- G-1050 violated, dead code detection works with literals only
when 1 = 1 and 'x' = 'y' then -- dead code detection works with literals only
null; -- some dead code here
else
null; -- some further enabled code here
Expand All @@ -40,7 +40,7 @@ begin
from employees
where department_id = co_dept_purchasing
or commission_pct is not null
and 5 = 6 -- G-1050 violated, dead code detection works with literals only
and 5 = 6 -- dead code detection works with literals only
)
-- "or commission_pct is not null" is dead code
loop
Expand Down
6 changes: 1 addition & 5 deletions docs/4-language-usage/1-general/g-1050.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ Literals are often used more than once in your code. Having them defined as a co

All constants should be collated in just one package used as a library. If these constants should be used in SQL too it is good practice to write a deterministic package function for every constant.

In specific situations this rule could lead to an extreme plethora of constants, for example if you use Logger like `logger.append_param(p_params =>l_params, p_name => 'p_param1_todo', p_val => p_param1_todo);`, where the value for `p_name` always should be the name of the variable that is passed to `p_val`. For such cases it would be overkill to add constants for every single variable name you are logging, so if you use Logger or similar, consider making that an exception to the rule, just document exactly which exceptions you will allow and stick to them.

Another exception is literals in views. It is not possible to use constants there, and using functions to wrap constants can have a negative impact on performance when peeking at binding variables is essential for an optimal execution plan.

To reduce the number of false positives, the number of occurrences of a literal should be less than 3.
To avoid an extreme plethora of constants or false positives, a literal should not occur more than once within a file.

## Example (bad)

Expand Down
2 changes: 1 addition & 1 deletion docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ select upper(first_name)
,salary
,hire_date
from employees
order by 4,1,3; -- violates also G-1050
order by 4,1,3;
```

## Example (good)
Expand Down
2 changes: 1 addition & 1 deletion docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Since the meaning of a `literal` depends on the configuration and database versi
select job_id
,sum(salary) as sum_salary
from employees
group by job_id,2 -- violates also G-1050
group by job_id,2
order by job_id;
```

Expand Down
15 changes: 15 additions & 0 deletions docs/4-language-usage/3-dml-and-sql/1-general/g-3185.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ select first_name
order by salary desc
)
where rownum <= 5; -- NOSONAR: G-1050 literal is ok for a standalone query
```

## Example (best)

(Assuming you are using Oracle Database 12c or later.)

``` sql
select first_name
,last_name
,salary
,hire_date
,rank() over (order by salary desc) as salary_rank
from employees
order by salary desc
fetch first 5 rows only; -- NOSONAR: G-1050 literal is ok for a standalone query
```
2 changes: 1 addition & 1 deletion docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Using a wildcard:
select e.employee_id
,e.last_name
from employees e
where e.last_name like 'Smith%'; -- NOSONAR: G-1050 literal is ok for a standalone query
where e.last_name like 'Smith%';
```

Change to equality operator instead:
Expand Down
2 changes: 1 addition & 1 deletion docs/4-language-usage/6-dynamic-sql/g-6010.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Having the executed statement in a variable makes it easier to debug your code (
declare
l_next_val employees.employee_id%type;
begin
execute immediate 'select employees_seq.nextval from dual' -- violates also G-1050
execute immediate 'select employees_seq.nextval from dual'
into l_next_val;
end;
/
Expand Down
2 changes: 1 addition & 1 deletion docs/4-language-usage/9-function-usage/g-9010.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ create or replace package body employee_api is
update employees
set date_of_birth = to_date(
co_dob_str default null on conversion error
,'FXYYYY-MM-DD' -- NOSONAR: G-1050 must be a literal
,'FXYYYY-MM-DD'
)
where employee_id = co_employee_id;
end set_dob;
Expand Down
4 changes: 2 additions & 2 deletions docs/4-language-usage/9-function-usage/g-9020.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ create or replace package body employee_api is
update employees
set salary = to_number(
co_salary default null on conversion error
,'99999999999999999999.99999' -- NOSONAR: G-1050 must be a literal
,q'[nls_numeric_characters='.,']' -- NOSONAR: G-1050 must be a literal
,'99999999999999999999.99999'
,q'[nls_numeric_characters='.,']'
)
where employee_id = co_employee_id;
end set_dob;
Expand Down
4 changes: 2 additions & 2 deletions docs/4-language-usage/9-function-usage/g-9040.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ create or replace package body employee_api is
update employees
set date_of_birth = to_date(
co_dob_str default null on conversion error
,'YYYY-MM-DD' -- violates also G-1050, must be a literal
,'YYYY-MM-DD'
)
where employee_id = co_employee_id;
end set_dob;
Expand All @@ -47,7 +47,7 @@ create or replace package body employee_api is
update employees
set date_of_birth = to_date(
co_dob_str default null on conversion error
,'FXYYYY-MM-DD' -- NOSONAR: G-1050 must be a literal
,'FXYYYY-MM-DD'
)
where employee_id = co_employee_id;
end set_dob;
Expand Down