Skip to content

Commit f115966

Browse files
Merge pull request #207 from Trivadis/feature/issue-206-simplify-g-1050
Feature/issue 206: simplify G-1050
2 parents c2e946f + 5fbff9e commit f115966

File tree

10 files changed

+28
-17
lines changed

10 files changed

+28
-17
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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 -- G-1050 violated, dead code detection works with literals only
16+
if 2 = 3 then -- dead code detection works with literals only
1717
null; -- some dead code here
1818
end if;
1919

@@ -28,7 +28,7 @@ begin
2828
null; -- some other enabled code here
2929

3030
case
31-
when 1 = 1 and 'x' = 'y' then -- G-1050 violated, dead code detection works with literals only
31+
when 1 = 1 and 'x' = 'y' then -- 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 -- G-1050 violated, dead code detection works with literals only
43+
and 5 = 6 -- 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-1050.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ Literals are often used more than once in your code. Having them defined as a co
99

1010
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.
1111

12-
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.
13-
14-
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.
15-
16-
To reduce the number of false positives, the number of occurrences of a literal should be less than 3.
12+
To avoid an extreme plethora of constants or false positives, a literal should not occur more than once within a file.
1713

1814
## Example (bad)
1915

docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ select upper(first_name)
1515
,salary
1616
,hire_date
1717
from employees
18-
order by 4,1,3; -- violates also G-1050
18+
order by 4,1,3;
1919
```
2020

2121
## Example (good)

docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Since the meaning of a `literal` depends on the configuration and database versi
2121
select job_id
2222
,sum(salary) as sum_salary
2323
from employees
24-
group by job_id,2 -- violates also G-1050
24+
group by job_id,2
2525
order by job_id;
2626
```
2727

docs/4-language-usage/3-dml-and-sql/1-general/g-3185.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ select first_name
3737
order by salary desc
3838
)
3939
where rownum <= 5; -- NOSONAR: G-1050 literal is ok for a standalone query
40+
```
41+
42+
## Example (best)
43+
44+
(Assuming you are using Oracle Database 12c or later.)
45+
46+
``` sql
47+
select first_name
48+
,last_name
49+
,salary
50+
,hire_date
51+
,rank() over (order by salary desc) as salary_rank
52+
from employees
53+
order by salary desc
54+
fetch first 5 rows only; -- NOSONAR: G-1050 literal is ok for a standalone query
4055
```

docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Using a wildcard:
2424
select e.employee_id
2525
,e.last_name
2626
from employees e
27-
where e.last_name like 'Smith%'; -- NOSONAR: G-1050 literal is ok for a standalone query
27+
where e.last_name like 'Smith%';
2828
```
2929

3030
Change to equality operator instead:

docs/4-language-usage/6-dynamic-sql/g-6010.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Having the executed statement in a variable makes it easier to debug your code (
1313
declare
1414
l_next_val employees.employee_id%type;
1515
begin
16-
execute immediate 'select employees_seq.nextval from dual' -- violates also G-1050
16+
execute immediate 'select employees_seq.nextval from dual'
1717
into l_next_val;
1818
end;
1919
/

docs/4-language-usage/9-function-usage/g-9010.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ create or replace package body employee_api is
4242
update employees
4343
set date_of_birth = to_date(
4444
co_dob_str default null on conversion error
45-
,'FXYYYY-MM-DD' -- NOSONAR: G-1050 must be a literal
45+
,'FXYYYY-MM-DD'
4646
)
4747
where employee_id = co_employee_id;
4848
end set_dob;

docs/4-language-usage/9-function-usage/g-9020.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ create or replace package body employee_api is
4242
update employees
4343
set salary = to_number(
4444
co_salary default null on conversion error
45-
,'99999999999999999999.99999' -- NOSONAR: G-1050 must be a literal
46-
,q'[nls_numeric_characters='.,']' -- NOSONAR: G-1050 must be a literal
45+
,'99999999999999999999.99999'
46+
,q'[nls_numeric_characters='.,']'
4747
)
4848
where employee_id = co_employee_id;
4949
end set_dob;

docs/4-language-usage/9-function-usage/g-9040.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ create or replace package body employee_api is
2525
update employees
2626
set date_of_birth = to_date(
2727
co_dob_str default null on conversion error
28-
,'YYYY-MM-DD' -- violates also G-1050, must be a literal
28+
,'YYYY-MM-DD'
2929
)
3030
where employee_id = co_employee_id;
3131
end set_dob;
@@ -47,7 +47,7 @@ create or replace package body employee_api is
4747
update employees
4848
set date_of_birth = to_date(
4949
co_dob_str default null on conversion error
50-
,'FXYYYY-MM-DD' -- NOSONAR: G-1050 must be a literal
50+
,'FXYYYY-MM-DD'
5151
)
5252
where employee_id = co_employee_id;
5353
end set_dob;

0 commit comments

Comments
 (0)