From 2e8d269bf36e225f407858116c63b6cff801e499 Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 09:17:08 +0200 Subject: [PATCH 01/11] simplify G-1050 based on #206 --- docs/4-language-usage/1-general/g-1050.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/4-language-usage/1-general/g-1050.md b/docs/4-language-usage/1-general/g-1050.md index 49cd6ee6..2fd41611 100644 --- a/docs/4-language-usage/1-general/g-1050.md +++ b/docs/4-language-usage/1-general/g-1050.md @@ -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) From 17d6c746aa3eb233ee18686a952f728997078bb5 Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 15:20:48 +0200 Subject: [PATCH 02/11] remove comment regarding G-1050 in G-3180 --- docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md index 4e1927fe..c7159086 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3180.md @@ -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) From eb1bb341c4e4f2e84bbd016a103918cdd0b94ffb Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 15:21:19 +0200 Subject: [PATCH 03/11] remove unnecssary NOSONAR marker for G-1050 in G-3195 --- docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md index f2d6b47e..1654945e 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md @@ -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: From 483f9d72fbedac18c776c2925a5a0674e429c78c Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 15:21:58 +0200 Subject: [PATCH 04/11] remove unnecessary NOSONAR marker for G-1050 in G-9010 --- docs/4-language-usage/9-function-usage/g-9010.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/9-function-usage/g-9010.md b/docs/4-language-usage/9-function-usage/g-9010.md index f4b82e56..c0c0c038 100644 --- a/docs/4-language-usage/9-function-usage/g-9010.md +++ b/docs/4-language-usage/9-function-usage/g-9010.md @@ -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; From 77f9d21510476ed2a971830458739fa492c17696 Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 15:22:26 +0200 Subject: [PATCH 05/11] remove unnecessary NOSONAR marker for G-1050 in G-9020 --- docs/4-language-usage/9-function-usage/g-9020.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/4-language-usage/9-function-usage/g-9020.md b/docs/4-language-usage/9-function-usage/g-9020.md index 29645077..6ed12b05 100644 --- a/docs/4-language-usage/9-function-usage/g-9020.md +++ b/docs/4-language-usage/9-function-usage/g-9020.md @@ -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; From ab8932bca664d2e9527d2b3ebdda1ca9d584d545 Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 15:23:22 +0200 Subject: [PATCH 06/11] remove unnecessary NOSONAR marker for G-1050 in G-9040 --- docs/4-language-usage/9-function-usage/g-9040.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/9-function-usage/g-9040.md b/docs/4-language-usage/9-function-usage/g-9040.md index 27b46525..0beede0d 100644 --- a/docs/4-language-usage/9-function-usage/g-9040.md +++ b/docs/4-language-usage/9-function-usage/g-9040.md @@ -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; From 0d3a3ca27d08cc4cd9a2b66b4e6f798ffcb9aa0b Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 15:23:54 +0200 Subject: [PATCH 07/11] remove comment regarding G-1050 in G-9040 --- docs/4-language-usage/9-function-usage/g-9040.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/9-function-usage/g-9040.md b/docs/4-language-usage/9-function-usage/g-9040.md index 0beede0d..745b679b 100644 --- a/docs/4-language-usage/9-function-usage/g-9040.md +++ b/docs/4-language-usage/9-function-usage/g-9040.md @@ -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; From 9559e976fbf9f4a8c692cba5bf526d39f12ced4a Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 16:08:11 +0200 Subject: [PATCH 08/11] remove comments regarding G-1050 in G-1040 --- docs/4-language-usage/1-general/g-1040.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/4-language-usage/1-general/g-1040.md b/docs/4-language-usage/1-general/g-1040.md index 1843a868..440ccad0 100644 --- a/docs/4-language-usage/1-general/g-1040.md +++ b/docs/4-language-usage/1-general/g-1040.md @@ -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; @@ -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 @@ -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 From a4e584e61ddd02d75216fde17504b1430a4e784a Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 16:08:37 +0200 Subject: [PATCH 09/11] remove comment regarding G-1050 in G-3182 --- docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md index e9432dc8..9ba52b38 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3182.md @@ -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; ``` From 6c8974968e9684a8d51f6ff8accb354155118393 Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 16:09:28 +0200 Subject: [PATCH 10/11] add best example using row limiting clause in G-3185 --- .../3-dml-and-sql/1-general/g-3185.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3185.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3185.md index ed9e731e..6d5c60a0 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3185.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3185.md @@ -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 ``` \ No newline at end of file From 5fbff9ed9f7fdeb0dd96f1027f71e7bfcdd6dd61 Mon Sep 17 00:00:00 2001 From: Philipp Salvisberg Date: Tue, 26 Sep 2023 16:10:13 +0200 Subject: [PATCH 11/11] remove comment regarding G-1050 in G-6010 --- docs/4-language-usage/6-dynamic-sql/g-6010.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/4-language-usage/6-dynamic-sql/g-6010.md b/docs/4-language-usage/6-dynamic-sql/g-6010.md index bfe57ffb..b2ee5faa 100644 --- a/docs/4-language-usage/6-dynamic-sql/g-6010.md +++ b/docs/4-language-usage/6-dynamic-sql/g-6010.md @@ -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; /