diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6512debf..d4c25580 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,8 +18,8 @@ jobs: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - name: db* CODECOP Issues run: | - export COP=4.3.1 - export VALIDATOR=4.3.1 + export COP=4.4.0 + export VALIDATOR=4.4.0 wget https://github.com/Trivadis/plsql-cop-cli/releases/download/v$COP/tvdcc-$COP.zip unzip tvdcc-$COP.zip -d . wget -P tvdcc-$COP/plugin/ https://github.com/Trivadis/plsql-cop-validators/releases/download/v$VALIDATOR/sonar-plsql-cop-custom-validators-plugin-$VALIDATOR.jar diff --git a/README.md b/README.md index a67e95a9..0f0c3897 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ HTML is the primary output format. [Material for MkDocs](https://squidfunk.githu * [Current version based on the main branch, snapshot version of the coming release](https://trivadis.github.io/plsql-and-sql-coding-guidelines/main/) * [Latest Release](https://trivadis.github.io/plsql-and-sql-coding-guidelines/) * Releases + * [Version 4.4](https://trivadis.github.io/plsql-and-sql-coding-guidelines/v4.4/) * [Version 4.3](https://trivadis.github.io/plsql-and-sql-coding-guidelines/v4.3/) * [Version 4.2](https://trivadis.github.io/plsql-and-sql-coding-guidelines/v4.2/) * [Version 4.1](https://trivadis.github.io/plsql-and-sql-coding-guidelines/v4.1/) diff --git a/docs/4-language-usage/1-general/g-1010.md b/docs/4-language-usage/1-general/g-1010.md index 72f5f175..8a5b65b8 100644 --- a/docs/4-language-usage/1-general/g-1010.md +++ b/docs/4-language-usage/1-general/g-1010.md @@ -9,7 +9,7 @@ It's a good alternative for comments to indicate the start and end of a named pr ## Example (bad) -``` sql +``` sql hl_lines="2 4 6 8" begin begin null; @@ -24,7 +24,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2 5 7 10" begin <> begin diff --git a/docs/4-language-usage/1-general/g-1020.md b/docs/4-language-usage/1-general/g-1020.md index 9304972a..95892c12 100644 --- a/docs/4-language-usage/1-general/g-1020.md +++ b/docs/4-language-usage/1-general/g-1020.md @@ -12,7 +12,7 @@ Use a label directly in front of loops and nested anonymous blocks: ## Example (bad) -``` sql +``` sql hl_lines="10 15 22 27 33" declare i integer; co_min_value constant integer := 1; @@ -52,7 +52,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="10 15 22 27 33" declare i integer; co_min_value constant integer := 1; diff --git a/docs/4-language-usage/1-general/g-1030.md b/docs/4-language-usage/1-general/g-1030.md index 8bba6a17..e4c563e3 100644 --- a/docs/4-language-usage/1-general/g-1030.md +++ b/docs/4-language-usage/1-general/g-1030.md @@ -9,7 +9,7 @@ Unused variables decrease the maintainability and readability of your code. ## Example (bad) -``` sql +``` sql hl_lines="4 6" create or replace package body my_package is procedure my_proc is l_last_name employees.last_name%type; @@ -33,7 +33,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="5 12" create or replace package body my_package is procedure my_proc is l_last_name employees.last_name%type; diff --git a/docs/4-language-usage/1-general/g-1040.md b/docs/4-language-usage/1-general/g-1040.md index 440ccad0..0193d2b9 100644 --- a/docs/4-language-usage/1-general/g-1040.md +++ b/docs/4-language-usage/1-general/g-1040.md @@ -9,7 +9,7 @@ Any part of your code, which is no longer used or cannot be reached, should be e ## Example (bad) -``` sql +``` sql hl_lines="4 12 19 31 38" declare co_dept_purchasing constant departments.department_id%type := 30; begin diff --git a/docs/4-language-usage/1-general/g-1050.md b/docs/4-language-usage/1-general/g-1050.md index 2fd41611..26e0c3f8 100644 --- a/docs/4-language-usage/1-general/g-1050.md +++ b/docs/4-language-usage/1-general/g-1050.md @@ -13,7 +13,7 @@ To avoid an extreme plethora of constants or false positives, a literal should n ## Example (bad) -``` sql +``` sql hl_lines="2-4" begin some_api.setup(in_department_id => 10); some_api.process(in_department_id => 10); @@ -24,7 +24,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="7-9" create or replace package constants_up is co_dept_admin constant departments.department_id%type := 10; end constants_up; diff --git a/docs/4-language-usage/1-general/g-1060.md b/docs/4-language-usage/1-general/g-1060.md index 388f8d8a..3cf82d1d 100644 --- a/docs/4-language-usage/1-general/g-1060.md +++ b/docs/4-language-usage/1-general/g-1060.md @@ -11,7 +11,7 @@ Instead of using `rowid` for later reference to the original row one should use ## Example (bad) -``` sql +``` sql hl_lines="6 11" begin insert into employees_log ( employee_id diff --git a/docs/4-language-usage/1-general/g-1070.md b/docs/4-language-usage/1-general/g-1070.md index 871b075e..0b3bc5b9 100644 --- a/docs/4-language-usage/1-general/g-1070.md +++ b/docs/4-language-usage/1-general/g-1070.md @@ -9,7 +9,7 @@ Having an end-of-comment within a block comment will end that block-comment. Thi ## Example (bad) -``` sql +``` sql hl_lines="2 4" begin /* comment one -- nested comment two */ null; @@ -21,7 +21,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2 4" begin /* comment one, comment two */ null; diff --git a/docs/4-language-usage/1-general/g-1080.md b/docs/4-language-usage/1-general/g-1080.md index ed223b32..97ed2c30 100644 --- a/docs/4-language-usage/1-general/g-1080.md +++ b/docs/4-language-usage/1-general/g-1080.md @@ -11,7 +11,7 @@ This rule ignores operators `+`, `*` and `||`, and expressions: `1=1`, `1<>1`, ` ## Example (bad) -``` sql +``` sql hl_lines="9 10" declare co_max_salary constant emp.salary%type := 3000; begin @@ -29,7 +29,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="9" declare co_max_salary constant emp.salary%type := 3000; begin diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2110.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2110.md index 3eba8581..467c73ad 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2110.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2110.md @@ -9,7 +9,7 @@ Changing the size of the database column last_name in the employees table from ` ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package body my_package is procedure my_proc is l_last_name varchar2(20 char); @@ -31,7 +31,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="3" create or replace package body my_package is procedure my_proc is l_last_name employees.last_name%type; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2120.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2120.md index 14e09b0e..219b756a 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2120.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2120.md @@ -11,7 +11,7 @@ A single location could be either a type specification package or the database ( ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package body my_package is procedure my_proc is subtype big_string_type is varchar2(1000 char); @@ -26,7 +26,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="8" create or replace package types_up is subtype big_string_type is varchar2(1000 char); end types_up; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2130.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2130.md index 8643c1b3..6fbaf59f 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2130.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2130.md @@ -20,7 +20,7 @@ Type | Usage ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package body my_package is procedure my_proc is l_note varchar2(1000 char); @@ -34,7 +34,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="8" create or replace package types_up is subtype big_string_type is varchar2(1000 char); end types_up; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2135.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2135.md index c9a82a34..35ea09ac 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2135.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2135.md @@ -9,7 +9,7 @@ Expending resources calculating and assigning values to a local variable and nev ## Example (bad) -``` sql +``` sql hl_lines="13" create or replace package body my_package is procedure my_proc is co_employee_id constant employees.employee_id%type := 1042; @@ -35,7 +35,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="13 15" create or replace package body my_package is procedure my_proc is co_employee_id constant employees.employee_id%type := 1042; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2140.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2140.md index dfff463f..607fbaf6 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2140.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2140.md @@ -9,7 +9,7 @@ Variables are initialized to `null` by default. ## Example (bad) -``` sql +``` sql hl_lines="2" declare l_note big_string_type := null; begin @@ -20,7 +20,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" declare l_note big_string_type; begin diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2145.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2145.md index f290e183..ffb40cd0 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2145.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2145.md @@ -9,7 +9,7 @@ There is no reason to assign a variable to itself. It is either a redundant stat ## Example (bad) -``` sql +``` sql hl_lines="8" declare co_parallel_degree constant types_up.name%type := 'parallel_degree'; l_function_result pls_integer; @@ -26,7 +26,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8" declare co_parallel_degree constant types_up.name%type := 'parallel_degree'; l_function_result pls_integer; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2150.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2150.md index fa7ee9f5..daa7023b 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2150.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2150.md @@ -9,7 +9,7 @@ The `null` value can cause confusion both from the standpoint of code review and ## Example (bad) -``` sql +``` sql hl_lines="4" declare l_value integer; begin @@ -22,7 +22,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" declare l_value integer; begin diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2160.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2160.md index f80910a6..d71ff50a 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2160.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2160.md @@ -9,7 +9,7 @@ If your initialization fails, you will not be able to handle the error in your e ## Example (bad) -``` sql +``` sql hl_lines="3 4" declare co_department_id constant integer := 100; l_department_name departments.department_name%type := @@ -22,7 +22,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8" declare co_department_id constant integer := 100; co_unkown_name constant departments.department_name%type := 'unknown'; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2170.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2170.md index 49b0fa28..da6b5eba 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2170.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2170.md @@ -9,7 +9,7 @@ The readability of your code will be higher when you do not overload variables. ## Example (bad) -``` sql +``` sql hl_lines="7 11" begin <
> declare @@ -33,7 +33,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="7 11" begin <
> declare diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2180.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2180.md index 58e78e9a..6920458e 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2180.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2180.md @@ -9,7 +9,7 @@ Quoted identifiers make your code hard to read and maintain. ## Example (bad) -``` sql +``` sql hl_lines="2-4" declare "sal+comm" integer; -- violates also naming conventions (G-9102) "my constant" constant integer := 1; -- violates also naming conventions (G-9114) @@ -26,7 +26,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2-4" declare l_sal_comm integer; co_my_constant constant integer := 1; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2185.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2185.md index 2db3217d..79e99fdd 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2185.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2185.md @@ -9,7 +9,7 @@ You should ensure that the name you have chosen well defines its purpose and usa ## Example (bad) -``` sql +``` sql hl_lines="2-4" declare i integer; c constant integer := 1; -- violates also naming conventions (G-9114) @@ -26,7 +26,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2-4" declare l_sal_comm integer; co_my_constant constant integer := 1; diff --git a/docs/4-language-usage/2-variables-and-types/1-general/g-2190.md b/docs/4-language-usage/2-variables-and-types/1-general/g-2190.md index 47da6696..7255a50b 100644 --- a/docs/4-language-usage/2-variables-and-types/1-general/g-2190.md +++ b/docs/4-language-usage/2-variables-and-types/1-general/g-2190.md @@ -11,7 +11,7 @@ Use of `rowid` or `urowid` means that your SQL statement will not be portable to ## Example (bad) -``` sql +``` sql hl_lines="7" declare l_department_name departments.department_name%type; l_rowid rowid; @@ -25,7 +25,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="7" declare l_department_name departments.department_name%type; l_department_id departments.department_id%type; diff --git a/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2210.md b/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2210.md index 0a7a4e16..07d48200 100644 --- a/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2210.md +++ b/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2210.md @@ -9,7 +9,7 @@ If you do not specify precision `number` is defaulted to 38 or the maximum suppo ## Example (bad) -``` sql +``` sql hl_lines="2" create or replace package types_up is subtype salary_type is number; end types_up; @@ -18,7 +18,7 @@ end types_up; ## Example (good) -``` sql +``` sql hl_lines="2" create or replace package types_up is subtype salary_type is number(5,1); end types_up; diff --git a/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2220.md b/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2220.md index cefdb618..929dff16 100644 --- a/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2220.md +++ b/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2220.md @@ -14,7 +14,7 @@ There are many reasons to use `pls_integer` instead of `number`: ## Example (bad) -``` sql +``` sql hl_lines="2" declare l_result number(9,0) := 0; -- violates also G-2130, G-2230 co_upper_bound constant pls_integer := 1e8; @@ -33,7 +33,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" declare l_result pls_integer := 0; co_upper_bound constant pls_integer := 1e8; diff --git a/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2230.md b/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2230.md index f34a284f..33877977 100644 --- a/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2230.md +++ b/docs/4-language-usage/2-variables-and-types/2-numeric-data-types/g-2230.md @@ -12,7 +12,7 @@ With Oracle Database 11g, the new data type `simple_integer` has been introduced ## Example (bad) -``` sql +``` sql hl_lines="2" declare l_result number(9,0) := 0; -- violates also G-2130, G-2220 co_upper_bound constant pls_integer := 1e8; @@ -31,7 +31,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" declare l_result simple_integer := 0; co_upper_bound constant pls_integer := 1e8; diff --git a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2310.md b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2310.md index 63d4beca..7756253c 100644 --- a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2310.md +++ b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2310.md @@ -9,7 +9,7 @@ ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package types_up is subtype description_type is char(200); @@ -19,7 +19,7 @@ end types_up; Unexpected trailing spaces can lead to wrong results. -```sql +``` sql hl_lines="4" with dept as ( select cast(department_name as varchar2(30 char)) as dname_vc2 @@ -39,7 +39,7 @@ select count(*) ## Example (good) -``` sql +``` sql hl_lines="3" create or replace package types_up is subtype description_type is varchar2(200 char); diff --git a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2320.md b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2320.md index fa8634d1..b7c6e3a8 100644 --- a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2320.md +++ b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2320.md @@ -9,7 +9,7 @@ Do not use the `varchar` data type. Use the `varchar2` data type instead. Althou ## Example (bad) -``` sql +``` sql hl_lines="2" create or replace package types_up is subtype description_type is varchar(200); end types_up; @@ -18,7 +18,7 @@ end types_up; ## Example (good) -``` sql +``` sql hl_lines="2" create or replace package types_up is subtype description_type is varchar2(200 char); end types_up; diff --git a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2330.md b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2330.md index a8292a8b..a4ad1eb8 100644 --- a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2330.md +++ b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2330.md @@ -9,7 +9,7 @@ Today zero-length strings and `null` are currently handled identical by the Orac ## Example (bad) -``` sql +``` sql hl_lines="2 8" create or replace package body constants_up is co_null_string constant types_up.text := ''; @@ -25,7 +25,7 @@ end constants_up; ## Example (good) -``` sql +``` sql hl_lines="6" create or replace package body constants_up is function empty_string return varchar2 deterministic diff --git a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2340.md b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2340.md index fd9bf7c1..fa08e805 100644 --- a/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2340.md +++ b/docs/4-language-usage/2-variables-and-types/3-character-data-types/g-2340.md @@ -11,7 +11,7 @@ In a multibyte environment a `varchar2(10)` definition may not necessarily hold ## Example (bad) -``` sql +``` sql hl_lines="2" create or replace package types_up is subtype description_type is varchar2(200); end types_up; @@ -20,7 +20,7 @@ end types_up; ## Example (good) -``` sql +``` sql hl_lines="2" create or replace package types_up is subtype description_type is varchar2(200 char); end types_up; diff --git a/docs/4-language-usage/2-variables-and-types/4-boolean-data-types/g-2410.md b/docs/4-language-usage/2-variables-and-types/4-boolean-data-types/g-2410.md index 6072a302..d621dd44 100644 --- a/docs/4-language-usage/2-variables-and-types/4-boolean-data-types/g-2410.md +++ b/docs/4-language-usage/2-variables-and-types/4-boolean-data-types/g-2410.md @@ -9,7 +9,7 @@ The use of `true` and `false` clarifies that this is a boolean value and makes t ## Example (bad) -``` sql +``` sql hl_lines="4" declare co_newfile constant pls_integer := 1000; co_oldfile constant pls_integer := 500; @@ -27,7 +27,7 @@ end; ## Example (better) -``` sql +``` sql hl_lines="4 6" declare co_newfile constant pls_integer := 1000; co_oldfile constant pls_integer := 500; @@ -45,7 +45,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4 6" declare co_newfile constant pls_integer := 1000; co_oldfile constant pls_integer := 500; diff --git a/docs/4-language-usage/2-variables-and-types/5-large-objects/g-2510.md b/docs/4-language-usage/2-variables-and-types/5-large-objects/g-2510.md index a2d076b4..3b98b831 100644 --- a/docs/4-language-usage/2-variables-and-types/5-large-objects/g-2510.md +++ b/docs/4-language-usage/2-variables-and-types/5-large-objects/g-2510.md @@ -11,7 +11,7 @@ There are many constraints to `long` datatypes in comparison to the `lob` types. ## Example (bad) -``` sql +``` sql hl_lines="2 3" declare l_long long; -- violates also G-2130 l_raw long raw; -- violates also G-2130 @@ -24,7 +24,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2 3" declare l_long clob; l_raw blob; diff --git a/docs/4-language-usage/2-variables-and-types/6-cursor-variables/g-2610.md b/docs/4-language-usage/2-variables-and-types/6-cursor-variables/g-2610.md index 2d528f48..0ae2f32d 100644 --- a/docs/4-language-usage/2-variables-and-types/6-cursor-variables/g-2610.md +++ b/docs/4-language-usage/2-variables-and-types/6-cursor-variables/g-2610.md @@ -9,7 +9,7 @@ There is no reason to define your own weak ref cursor types, as they are not dif ## Example (bad) -``` sql +``` sql hl_lines="2 3" declare type local_weak_cursor_type is ref cursor; c_data local_weak_cursor_type; @@ -29,7 +29,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" declare c_data sys_refcursor; begin diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3110.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3110.md index 60b2a2fc..c86f74b5 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3110.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3110.md @@ -9,7 +9,7 @@ Data structures often change. Having the target columns in your insert statement ## Example (bad) -``` sql +``` sql hl_lines="4" create or replace package body dept_api is procedure ins_dept(in_dept_row in dept%rowtype) is begin @@ -27,7 +27,7 @@ end dept_api; ## Example (good) -``` sql +``` sql hl_lines="4-9" create or replace package body dept_api is procedure ins_dept(in_dept_row in dept%rowtype) is begin diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3115.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3115.md index 7dbab1cc..1aa87853 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3115.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3115.md @@ -11,14 +11,14 @@ One exception to this rule can be when you attempt to fire cross edition trigger ## Example (bad) -``` sql +``` sql hl_lines="2" update employees set first_name = first_name; ``` ## Example (good) -``` sql +``` sql hl_lines="2" update employees set first_name = initcap(first_name); ``` \ No newline at end of file diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md index 4633d5cb..7acc2225 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md @@ -11,7 +11,7 @@ Especially when using subqueries the omission of table aliases may end in unexpe ## Example (bad) -``` sql +``` sql hl_lines="1-3" select last_name ,first_name ,department_name @@ -23,7 +23,7 @@ select last_name If the `jobs` table has no `employee_id` column and `employees` has one this query will not raise an error but return all rows of the `employees` table as a subquery is allowed to access columns of all its parent tables - this construct is known as correlated subquery. -``` sql +``` sql hl_lines="5" select last_name ,first_name from employees @@ -36,7 +36,7 @@ select last_name ## Example (better) -``` sql +``` sql hl_lines="1-3" select e.last_name ,e.first_name ,d.department_name @@ -50,7 +50,7 @@ select e.last_name Using meaningful aliases improves the readability of your code. -``` sql +``` sql hl_lines="1-3" select emp.last_name ,emp.first_name ,dept.department_name @@ -62,13 +62,13 @@ select emp.last_name If the `jobs` table has no `employee_id` column this query will return an error due to the directive (given by adding the table alias to the column) to read the `employee_id` column from the `jobs` table. -``` sql +``` sql hl_lines="5" select emp.last_name ,emp.first_name from employees emp where emp.employee_id in ( - select j.employee_id - from jobs j - where j.job_title like '%Manager%' -- NOSONAR: G-1050 literal is ok for a standalone query + select job.employee_id + from jobs job + where job.job_title like '%Manager%' -- NOSONAR: G-1050 literal is ok for a standalone query ); ``` diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3130.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3130.md index 9b69722b..aa14a796 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3130.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3130.md @@ -9,7 +9,7 @@ ANSI SQL-92 join syntax supports the full outer join. A further advantage of the ## Example (bad) -``` sql +``` sql hl_lines="5-7" select e.employee_id ,e.last_name ,e.first_name @@ -22,7 +22,7 @@ select e.employee_id ## Example (good) -``` sql +``` sql hl_lines="5-7" select emp.employee_id ,emp.last_name ,emp.first_name diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3140.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3140.md index eb194de4..7067bd73 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3140.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3140.md @@ -9,7 +9,7 @@ Using cursor-anchored records as targets for your cursors results enables the po ## Example (bad) -``` sql +``` sql hl_lines="10 15" declare cursor c_employees is select employee_id,first_name,last_name @@ -33,7 +33,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8 13" declare cursor c_employees is select employee_id,first_name,last_name diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3145.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3145.md index 98700a2a..fde5bd3a 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3145.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3145.md @@ -11,7 +11,7 @@ Exceptions to the rule can be when querying an inline view (where the SELECT \* ## Example (bad) -``` sql +``` sql hl_lines="4" begin <> for r_employee in ( @@ -31,7 +31,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" begin <> for r_employee in ( diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3150.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3150.md index 91669d07..a9088520 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3150.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3150.md @@ -13,7 +13,7 @@ An identity column is a surrogate key by design – there is no reason why we sh ## Example (bad) -``` sql +``` sql hl_lines="16" create table locations ( location_id number(10) not null ,location_name varchar2(60 char) not null @@ -36,7 +36,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" create table locations ( location_id number(10) generated always as identity ,location_name varchar2(60 char) not null diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3160.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3160.md index a3429f21..dabce939 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3160.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3160.md @@ -18,7 +18,7 @@ Invisible columns may be accessed by explicitly adding them to the column list i ## Example (bad) -``` sql +``` sql hl_lines="9" declare r_employee employees%rowtype; co_id constant employees.employee_id%type := 107; @@ -32,7 +32,7 @@ begin end; / ``` -``` +``` hl_lines="2" Error report - ORA-54017: update operation disallowed on virtual columns ORA-06512: at line 9 @@ -40,7 +40,7 @@ ORA-06512: at line 9 ## Example (good) -``` sql +``` sql hl_lines="2-3 14" alter table employees add total_salary invisible generated always as (salary + nvl(commission_pct,0) * salary) diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3170.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3170.md index 6bdb44b8..0c46e501 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3170.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3170.md @@ -16,7 +16,7 @@ Default values have been nullifiable until Oracle Database 12c. Meaning any tool ## Example (bad) -``` sql +``` sql hl_lines="3" create table null_test ( test_case number(2) not null ,column_defaulted varchar2(10 char) default 'Default') @@ -27,7 +27,7 @@ insert into null_test(test_case,column_defaulted) values (3,null); -- NOSONAR select test_case,column_defaulted from null_test; ``` -``` +``` hl_lines="5" TEST_CASE COLUMN_DEF --------- ----------- 1 Value @@ -37,7 +37,7 @@ TEST_CASE COLUMN_DEF ## Example (good) -``` sql +``` sql hl_lines="3" create table null_test ( test_case number(2) not null ,column_defaulted varchar2(10 char) default on null 'Default') @@ -48,7 +48,7 @@ insert into null_test(test_case,column_defaulted) values (3,null); -- NOSONAR select test_case,column_defaulted from null_test; ``` -``` +``` hl_lines="5" TEST_CASE COLUMN_DEF ---------- ---------- 1 Value 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 c7159086..cf5c7d91 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 @@ -9,7 +9,7 @@ If you change your `select` list afterwards the `order by` will still work but o ## Example (bad) -``` sql +``` sql hl_lines="6" select upper(first_name) ,last_name ,salary @@ -20,7 +20,7 @@ select upper(first_name) ## Example (good) -``` sql +``` sql hl_lines="6-8" select upper(first_name) as first_name ,last_name ,salary 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 9ba52b38..1553a2a1 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 @@ -1,4 +1,4 @@ -# G-3182: Always specify column names instead of positional references in GROUP BY clauses. +# G-3182: Always specify column names/aliases instead of positional references in GROUP BY clauses. !!! bug "Blocker" Reliability @@ -11,13 +11,13 @@ Oracle Database 23c If you use a numeric literal in the `group by` clause in an Oracle Database prior to version 23c, then this literal is not required. It is simply a constant. -Starting with Oracle Database 23c, it is possible to use a literal in the `group by` clause to refer to a column name in the `select` list. However, this only works if the `group_by_position_enabled` parameter is set to `true`. In any case, it is not convenient for the readers of the code to have to count the columns in the `select` list to know how the result is grouped. +Starting with Oracle Database 23c, it is possible to use a literal in the `group by` clause to refer to a column name or column alias in the `select` list. However, this only works if the `group_by_position_enabled` parameter is set to `true`. In any case, it is not convenient for the readers of the code to have to count the columns in the `select` list to know how the result is grouped. Since the meaning of a `literal` depends on the configuration and database version, the intention is unclear and might lead to an incorrect result. ## Example (bad) -``` sql +``` sql hl_lines="4" select job_id ,sum(salary) as sum_salary from employees @@ -27,7 +27,7 @@ select job_id ## Example (good) -``` sql +``` sql hl_lines="4" select job_id ,sum(salary) as sum_salary from employees diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3183.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3183.md index dc07edbf..576090a1 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3183.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3183.md @@ -15,7 +15,7 @@ Unless you use `rollup`, `cube` or `grouping sets`, it is not necessary to use e ## Example (bad) -``` sql +``` sql hl_lines="4" select lower(job_id) as job ,sum(salary) as sum_salary from employees @@ -25,7 +25,7 @@ select lower(job_id) as job ## Example (good) -``` sql +``` sql hl_lines="4" select lower(job_id) as job ,sum(salary) as sum_salary from employees 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 6d5c60a0..2ab96f04 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 @@ -9,7 +9,7 @@ The `rownum` pseudo-column is assigned before the `order by` clause is used, so ## Example (bad) -``` sql +``` sql hl_lines="7" select first_name ,last_name ,salary @@ -22,7 +22,7 @@ select first_name ## Example (good) -``` sql +``` sql hl_lines="6-14" select first_name ,last_name ,salary @@ -43,7 +43,7 @@ select first_name (Assuming you are using Oracle Database 12c or later.) -``` sql +``` sql hl_lines="8" select first_name ,last_name ,salary diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3190.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3190.md index d6f49d13..b4a0627e 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3190.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3190.md @@ -9,7 +9,7 @@ A `natural join` joins tables on equally named columns. This may comfortably fit ## Example (bad) -``` sql +``` sql hl_lines="5" select d.department_name ,e.last_name ,e.first_name @@ -25,7 +25,7 @@ Accounting Gietz William Executive De Haan Lex ... ``` -``` sql +``` sql hl_lines="8" alter table departments add modified_at date default on null sysdate; alter table employees add modified_at date default on null sysdate; @@ -43,7 +43,7 @@ No data found ## Example (good) -``` sql +``` sql hl_lines="5-6" select d.department_name ,e.last_name ,e.first_name 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 1654945e..1bc5f473 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 @@ -9,7 +9,7 @@ Using `like` without at least one wildcard (`%` or `_`) is unclear to a maintain ## Example (bad) -``` sql +``` sql hl_lines="4" select e.employee_id ,e.last_name from employees e @@ -20,7 +20,7 @@ select e.employee_id Using a wildcard: -``` sql +``` sql hl_lines="4" select e.employee_id ,e.last_name from employees e @@ -29,7 +29,7 @@ select e.employee_id Change to equality operator instead: -``` sql +``` sql hl_lines="4" select e.employee_id ,e.last_name from employees e diff --git a/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3210.md b/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3210.md index 18f4ee3a..4f9af31c 100644 --- a/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3210.md +++ b/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3210.md @@ -11,7 +11,7 @@ Context switches between PL/SQL and SQL are extremely costly. BULK Operations re ## Example (bad) -``` sql +``` sql hl_lines="11-12" declare t_employee_ids employee_api.t_employee_ids_type; co_increase constant employees.salary%type := 0.1; @@ -33,7 +33,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="10-11" declare t_employee_ids employee_api.t_employee_ids_type; co_increase constant employees.salary%type := 0.1; diff --git a/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3220.md b/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3220.md index 97dd7093..a7dc4008 100644 --- a/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3220.md +++ b/docs/4-language-usage/3-dml-and-sql/2-bulk-operations/g-3220.md @@ -11,7 +11,7 @@ If your use of `forall` is meant to be atomic (all or nothing), don't use `save ## Example (bad) -``` sql +``` sql hl_lines="12" declare t_employee_ids employee_api.t_employee_ids_type; co_increase constant employees.salary%type := 0.1; @@ -33,7 +33,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="12 17-22" declare t_employee_ids employee_api.t_employee_ids_type; co_increase constant employees.salary%type := 0.1; diff --git a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md index 25fc1cc6..c2106715 100644 --- a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md +++ b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md @@ -13,7 +13,7 @@ Doing frequent commits within a cursor loop (all types of loops over cursors, wh ## Example (bad) -``` sql +``` sql hl_lines="22" declare l_counter integer := 0; l_discount discount.percentage%type; @@ -48,7 +48,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="19" declare l_discount discount.percentage%type; co_status_new constant orders.order_status%type := 'New'; @@ -76,7 +76,7 @@ end; (Assuming suitable foreign key relationship exists to allow updating a join.) -``` sql +``` sql hl_lines="13" declare co_status_new constant orders.order_status%type := 'New'; begin diff --git a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3320.md b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3320.md index ddf7f588..2c97bc33 100644 --- a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3320.md +++ b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3320.md @@ -9,7 +9,7 @@ Commit inside a non-cursor loop (other loop types than loops over cursors - see ## Example (bad) -``` sql +``` sql hl_lines="10 27" declare co_upper_bound constant integer := 5; co_max_level constant integer := 3; @@ -46,7 +46,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="26 31 32" declare co_upper_bound constant integer := 5; co_max_level constant integer := 3; diff --git a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3330.md b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3330.md new file mode 100644 index 00000000..22e0bfeb --- /dev/null +++ b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3330.md @@ -0,0 +1,49 @@ +# G-3330: Avoid autonomous transactions. + +!!! bug "Blocker" + Reliability, Testability + +## Reason + +>Before we take a look at how autonomous transactions work, I’d like to emphasize that this type of transaction is +a powerful and therefore dangerous tool when used improperly. The true need for an autonomous transaction is very +rare indeed. I would be very suspicious of any code that makes use of them—that code would get extra examination. +It is far too easy to accidentally introduce logical data integrity issues into a system using them. (page 300) + +>In my experience, that is the only truly valid use of an autonomous transaction—to log errors or informational +messages in a manner that can be committed independently of the parent transaction. (page 305) + +>-- Kyte, Thomas (2013). _Expert Oracle Database Architecture. Third Edition_. Apress. + +It is most likely not possible to distinguish legitimate uses of autonomous transactions from illegitimate ones via static code analysis. However, since we expect exactly one autonomous transaction per application, the number of false positives is manageable. + + +## Example (bad) + +``` sql hl_lines="3 7" +create or replace package body dept_api is + procedure ins_dept(in_dept_row in dept%rowtype) is + pragma autonomous_transaction; + begin + insert into dept + values in_dept_row; + commit; -- required by autonomous transaction + end ins_dept; +end dept_api; +/ +``` + +## Example (good) + +``` sql hl_lines="6 7" +create or replace package body dept_api is + procedure ins_dept(in_dept_row in dept%rowtype) is + begin + insert into dept + values in_dept_row; + -- transaction is commited in calling module + -- after the completion of the unit of work + end ins_dept; +end dept_api; +/ +``` \ No newline at end of file diff --git a/docs/4-language-usage/4-control-structures/1-cursor/g-4110.md b/docs/4-language-usage/4-control-structures/1-cursor/g-4110.md index 727b303c..073bdb2e 100644 --- a/docs/4-language-usage/4-control-structures/1-cursor/g-4110.md +++ b/docs/4-language-usage/4-control-structures/1-cursor/g-4110.md @@ -9,7 +9,7 @@ The readability of your code will be higher when you avoid negative sentences. ## Example (bad) -``` sql +``` sql hl_lines="23" declare cursor c_employees is select * @@ -42,7 +42,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="23" declare cursor c_employees is select * diff --git a/docs/4-language-usage/4-control-structures/1-cursor/g-4120.md b/docs/4-language-usage/4-control-structures/1-cursor/g-4120.md index 7f9e8443..9fc8c421 100644 --- a/docs/4-language-usage/4-control-structures/1-cursor/g-4120.md +++ b/docs/4-language-usage/4-control-structures/1-cursor/g-4120.md @@ -11,7 +11,7 @@ The employees table holds 107 rows. The example below will only show 100 rows as the cursor attribute `notfound` is set to true as soon as the number of rows to be fetched defined by the `limit` clause is not fulfilled anymore. -``` sql +``` sql hl_lines="16" declare cursor c_employees is select * @@ -45,7 +45,7 @@ end; This example will show all 107 rows but execute one fetch too much (12 instead of 11). -``` sql +``` sql hl_lines="16" declare cursor c_employees is select * @@ -78,7 +78,7 @@ end; This example does the trick (11 fetches only to process all rows) -``` sql +``` sql hl_lines="21" declare cursor c_employees is select * diff --git a/docs/4-language-usage/4-control-structures/1-cursor/g-4130.md b/docs/4-language-usage/4-control-structures/1-cursor/g-4130.md index 7cd79d7a..4906a103 100644 --- a/docs/4-language-usage/4-control-structures/1-cursor/g-4130.md +++ b/docs/4-language-usage/4-control-structures/1-cursor/g-4130.md @@ -9,7 +9,7 @@ Any cursors left open can consume additional memory space (i.e. SGA) within the ## Example (bad) -``` sql +``` sql hl_lines="11" create or replace package body employee_api as function department_salary(in_dept_id in integer) -- NOSONAR: non-deterministic return number is @@ -31,7 +31,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="11 13" create or replace package body employee_api as function department_salary(in_dept_id in integer) -- NOSONAR: non-deterministic return number is diff --git a/docs/4-language-usage/4-control-structures/1-cursor/g-4140.md b/docs/4-language-usage/4-control-structures/1-cursor/g-4140.md index f5c3ef5b..8efc3434 100644 --- a/docs/4-language-usage/4-control-structures/1-cursor/g-4140.md +++ b/docs/4-language-usage/4-control-structures/1-cursor/g-4140.md @@ -14,7 +14,7 @@ In the following example, a procedure call is inserted between the `delete` stat ## Example (bad) -``` sql +``` sql hl_lines="19-21" create or replace package body employee_api as co_one constant simple_integer := 1; co_msg constant types_up.text := 'Do something based on '; @@ -46,7 +46,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="20-24" create or replace package body employee_api as co_one constant simple_integer := 1; co_msg constant types_up.text := 'Do something based on '; diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4210.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4210.md index f4351d7a..2ff2921b 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4210.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4210.md @@ -9,7 +9,7 @@ ## Example (bad) -``` sql +``` sql hl_lines="4-10" declare l_color types_up.color_code_type; begin @@ -26,7 +26,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4-11" declare l_color types_up.color_code_type; begin @@ -37,8 +37,6 @@ begin my_package.do_blue(); when constants_up.co_black then my_package.do_black(); - else - null; end case; end; / diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4220.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4220.md index ddd4ffb0..18a264c4 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4220.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4220.md @@ -9,7 +9,7 @@ ## Example (bad) -``` sql +``` sql hl_lines="2" -- @formatter:off select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_english , constants_up.co_ctry_fr, constants_up.co_lang_french @@ -20,7 +20,7 @@ select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_e `null` values can be compared in `decode`: -``` sql +``` sql hl_lines="4" -- @formatter:off select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_english , constants_up.co_ctry_fr, constants_up.co_lang_french @@ -31,7 +31,7 @@ select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_e ## Example (good) -``` sql +``` sql hl_lines="1" select case ctry.country_code when constants_up.co_ctry_uk then constants_up.co_lang_english @@ -47,7 +47,7 @@ select case ctry.country_code Simple `case` can not compare `null` values, instead the searched `case` expression must be used: -``` sql +``` sql hl_lines="1 6" select case when ctry.country_code = constants_up.co_ctry_uk then constants_up.co_lang_english diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4230.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4230.md index bba014d6..32d3f010 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4230.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4230.md @@ -12,14 +12,14 @@ The `coalesce` function does not have this drawback. ## Example (bad) -``` sql +``` sql hl_lines="1" select nvl(dummy,my_package.expensive_null(value_in => dummy)) from dual; ``` ## Example (good) -``` sql +``` sql hl_lines="1" select coalesce(dummy,my_package.expensive_null(value_in => dummy)) from dual; ``` \ No newline at end of file diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4240.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4240.md index 7b3b728c..c4b1a7b9 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4240.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4240.md @@ -9,7 +9,7 @@ The `nvl2` function always evaluates all parameters before deciding which one to ## Example (bad) -``` sql +``` sql hl_lines="1-2" select nvl2(dummy,my_package.expensive_nn(value_in => dummy) ,my_package.expensive_null(value_in => dummy)) from dual; @@ -17,7 +17,7 @@ select nvl2(dummy,my_package.expensive_nn(value_in => dummy) ## Example (good) -``` sql +``` sql hl_lines="1-6" select case when dummy is null then my_package.expensive_null(value_in => dummy) diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4250.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4250.md index 90090ceb..58fc28e4 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4250.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4250.md @@ -9,7 +9,7 @@ Conditions are evaluated top to bottom in branches of a `case` statement or chai ## Example (bad) -``` sql +``` sql hl_lines="9-10" declare l_color types_up.color_code_type; begin @@ -29,7 +29,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="9-10" declare l_color types_up.color_code_type; begin diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4260.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4260.md index cb3ec5a0..44893151 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4260.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4260.md @@ -9,7 +9,7 @@ It is more readable to use the opposite comparison operator instead of inverting ## Example (bad) -``` sql +``` sql hl_lines="4" declare l_color types_up.color_code_type; begin @@ -22,7 +22,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" declare l_color types_up.color_code_type; begin diff --git a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4270.md b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4270.md index e4ac66d2..b8d6bc70 100644 --- a/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4270.md +++ b/docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4270.md @@ -9,7 +9,7 @@ It is more readable to simply use the boolean value as a condition itself, rathe ## Example (bad) -``` sql +``` sql hl_lines="6" declare co_string constant types_up.text := '42'; l_is_valid boolean; @@ -24,7 +24,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="6" declare co_string constant types_up.text := '42'; l_is_valid boolean; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4310.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4310.md index 952932e3..796f1c15 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4310.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4310.md @@ -13,7 +13,7 @@ ## Example (bad) -``` sql +``` sql hl_lines="23 28 31" create or replace package body my_package is procedure password_check(in_password in varchar2) is co_password constant dba_users.password%type := in_password; @@ -54,7 +54,7 @@ end my_package; ## Example (better) -``` sql +``` sql hl_lines="27 30" create or replace package body my_package is procedure password_check(in_password in varchar2) is co_password constant dba_users.password%type := in_password; @@ -94,7 +94,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="8" create or replace package body my_package is procedure password_check(in_password in varchar2) is co_password constant dba_users.password%type := in_password; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4320.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4320.md index e20e52da..6eb5ace2 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4320.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4320.md @@ -9,7 +9,7 @@ It's a good alternative for comments to indicate the start and end of a named lo ## Example (bad) -``` sql +``` sql hl_lines="8 11 13 15 17 20 22 25" declare i integer; co_min_value constant simple_integer := 1; @@ -41,7 +41,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8 12 14 17 19 23 25 29" declare i integer; co_min_value constant simple_integer := 1; @@ -67,10 +67,7 @@ begin end loop for_loop; <> - for r_employee in ( - select last_name - from employees - ) + for r_employee in (select last_name from employees) loop sys.dbms_output.put_line(r_employee.last_name); end loop process_employees; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4325.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4325.md index 9fc96121..f82c91f8 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4325.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4325.md @@ -9,7 +9,7 @@ Reusing labels inside the scope of another label with the same name leads to con ## Example (bad) -``` sql +``` sql hl_lines="1 6" <> declare co_min_value constant simple_integer := 1; @@ -26,7 +26,7 @@ end my_label; ## Example (good) -``` sql +``` sql hl_lines="1 6" <> declare co_min_value constant simple_integer := 1; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4330.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4330.md index 3ccf2e78..0ff4b702 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4330.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4330.md @@ -12,7 +12,7 @@ it should be avoided, whenever possible. ## Example (bad) -``` sql +``` sql hl_lines="10 11" declare cursor c_employees is select employee_id,last_name @@ -35,7 +35,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="7 8" declare cursor c_employees is select employee_id,last_name diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4340.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4340.md index 8edfce46..f3bbec8f 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4340.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4340.md @@ -18,7 +18,7 @@ Please note that: ## Example (bad) -``` sql +``` sql hl_lines="14 15" declare type t_employee_type is varray(10) of employees.employee_id%type; t_employees t_employee_type; @@ -43,7 +43,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="10 11" declare type t_employee_type is varray(10) of employees.employee_id%type; t_employees t_employee_type; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4350.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4350.md index 50a6d1e6..86c1e148 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4350.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4350.md @@ -15,7 +15,7 @@ Please note that: ## Example (bad) -``` sql +``` sql hl_lines="6" declare type t_employee_type is table of employees.employee_id%type; t_employees t_employee_type := t_employee_type(); @@ -33,7 +33,7 @@ end; Raise an unitialized collection error if `t_employees` is not initialized. -``` sql +``` sql hl_lines="6" declare type t_employee_type is table of employees.employee_id%type; t_employees t_employee_type := t_employee_type(); @@ -51,7 +51,7 @@ end; Raises neither an error nor checking whether the array is empty. `t_employees.count()` always returns a `number` (unless the array is not initialized). If the array is empty `count()` returns 0 and therefore the loop will not be entered. -``` sql +``` sql hl_lines="5 7" declare type t_employee_type is table of employees.employee_id%type; t_employees t_employee_type := t_employee_type(); diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4360.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4360.md index d0424be7..756afa04 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4360.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4360.md @@ -15,7 +15,7 @@ Please note that: ## Example (bad) -``` sql +``` sql hl_lines="14" declare -- raises no_data_found when processing 2nd record type t_employee_type is table of employees.employee_id%type; t_employees t_employee_type; @@ -40,7 +40,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="16" declare type t_employee_type is table of employees.employee_id%type; t_employees t_employee_type; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4365.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4365.md index e5394013..6ab04ccc 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4365.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4365.md @@ -9,7 +9,7 @@ An unconditional `continue` is either redundant (a `continue` as the last statem ## Example (bad) -``` sql +``` sql hl_lines="6" begin <> loop @@ -25,7 +25,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8" declare co_first_year constant pls_integer := 1900; begin diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4370.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4370.md index 2ce10d54..ec462805 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4370.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4370.md @@ -9,7 +9,7 @@ A numeric for loop as well as a while loop and a cursor for loop have defined lo ## Example (bad) -``` sql +``` sql hl_lines="9 12 21 24 28 35" declare i integer; co_min_value constant simple_integer := 1; @@ -52,7 +52,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="9 20 26" declare i integer; co_min_value constant simple_integer := 1; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4375.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4375.md index 6eb8e87c..32c9b649 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4375.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4375.md @@ -9,7 +9,7 @@ If you need to use an `exit` statement use its full semantic to make the code ea ## Example (bad) -``` sql +``` sql hl_lines="8-10" declare co_first_year constant pls_integer := 1900; begin @@ -29,7 +29,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8" declare co_first_year constant pls_integer := 1900; begin diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4380.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4380.md index 86009ca1..61f246d5 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4380.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4380.md @@ -9,7 +9,7 @@ It's a good alternative for comments, especially for nested loops to name the lo ## Example (bad) -``` sql +``` sql hl_lines="23 26" declare co_init_loop constant simple_integer := 0; co_increment constant simple_integer := 1; @@ -43,7 +43,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="23" declare co_init_loop constant simple_integer := 0; co_increment constant simple_integer := 1; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4385.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4385.md index 1d81a66c..6d26e948 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4385.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4385.md @@ -9,7 +9,7 @@ You might process more data than required, which leads to bad performance. ## Example (bad) -``` sql +``` sql hl_lines="8-11" declare l_employee_found boolean := false; cursor c_employees is @@ -30,7 +30,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8-11" declare l_employee_found boolean := false; cursor c_employees is diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4387.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4387.md new file mode 100644 index 00000000..ab73d7c1 --- /dev/null +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4387.md @@ -0,0 +1,58 @@ +# G-4387: Never use a FOR LOOP for a query that should return not more than one row. + +!!! bug "Blocker" + Reliability, Efficiency, Maintainability + +!!! missing "Unsupported in db\* CODECOP Validators" + Without access to the Oracle Data Dictionary, we cannot determine the number of rows to be processed. + +## Reason + +A `for loop` can hide a `too_many_rows` exception. The more complex a query is, the higher is the risk that more than one row will be processed. +This affects performance and can lead to a wrong result. + +A `for loop` can also hide a `no_data_found` exception and the reader cannot determine whether this is intentional or not. + +## Example (bad) + +``` sql hl_lines="6 9 12" +create or replace package body employee_api is + function emp_name(in_empno in integer) return varchar2 is -- NOSONAR: non-deterministic + l_ename emp.ename%type; + begin + <> + for r in ( + select ename + from emp + where empno = in_empno + ) + loop + l_ename := r.ename; + end loop fetch_name; + return l_ename; + end emp_name; +end employee_api; +/ +``` + +## Example (good) + +``` sql hl_lines="6 8" +create or replace package body employee_api is + function emp_name(in_empno in integer) return varchar2 is -- NOSONAR: non-deterministic + l_ename emp.ename%type; + begin + select ename + into l_ename + from emp + where empno = in_empno; + return l_ename; + exception + when no_data_found then + return null; + when too_many_rows then + raise; + end emp_name; +end employee_api; +/ +``` \ No newline at end of file diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4390.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4390.md index ef2ba610..15495b2b 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4390.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4390.md @@ -9,7 +9,7 @@ If the loop index is used for anything but traffic control inside the loop, this ## Example (bad) -``` sql +``` sql hl_lines="14" declare l_row pls_integer; l_value pls_integer; @@ -37,7 +37,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="9 13" declare co_lower_bound constant simple_integer := 1; co_upper_bound constant simple_integer := 5; diff --git a/docs/4-language-usage/4-control-structures/3-flow-control/g-4395.md b/docs/4-language-usage/4-control-structures/3-flow-control/g-4395.md index d8a952f8..7e8e23b8 100644 --- a/docs/4-language-usage/4-control-structures/3-flow-control/g-4395.md +++ b/docs/4-language-usage/4-control-structures/3-flow-control/g-4395.md @@ -9,7 +9,7 @@ Your `loop` statement uses a hard-coded value for either its upper or lower boun ## Example (bad) -``` sql +``` sql hl_lines="3" begin <> for i in 1..5 @@ -22,7 +22,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="6" declare co_lower_bound constant simple_integer := 1; co_upper_bound constant simple_integer := 5; diff --git a/docs/4-language-usage/5-exception-handling/g-5010.md b/docs/4-language-usage/5-exception-handling/g-5010.md index f22edad4..28008337 100644 --- a/docs/4-language-usage/5-exception-handling/g-5010.md +++ b/docs/4-language-usage/5-exception-handling/g-5010.md @@ -20,7 +20,7 @@ This kind of framework should include ## Example (bad) -``` sql +``` sql hl_lines="5 7" declare co_start constant logger_logs.text%type := 'start'; co_end constant logger_logs.text%type := 'end'; @@ -34,7 +34,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="7 9" declare -- see https://github.com/OraOpenSource/Logger co_start constant logger_logs.text%type := 'start'; diff --git a/docs/4-language-usage/5-exception-handling/g-5020.md b/docs/4-language-usage/5-exception-handling/g-5020.md index 95fb4939..0cd14432 100644 --- a/docs/4-language-usage/5-exception-handling/g-5020.md +++ b/docs/4-language-usage/5-exception-handling/g-5020.md @@ -10,7 +10,7 @@ but it is better to use named exceptions instead, because it ensures a certain l ## Example (bad) -``` sql +``` sql hl_lines="9" declare co_no_data_found constant integer := -1; begin @@ -28,7 +28,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="6" begin my_package.some_processing(); -- some code which raises an exception exception diff --git a/docs/4-language-usage/5-exception-handling/g-5030.md b/docs/4-language-usage/5-exception-handling/g-5030.md index d53d6676..8de4cccc 100644 --- a/docs/4-language-usage/5-exception-handling/g-5030.md +++ b/docs/4-language-usage/5-exception-handling/g-5030.md @@ -11,7 +11,7 @@ This is error-prone because your local declaration overrides the global declarat Using the code below, we are not able to handle the `no_data_found` exception raised by the `select` statement as we have overwritten that exception handler. In addition, our exception handler doesn't have an exception number assigned, which should be raised when the `select` statement does not find any rows. -``` sql +``` sql hl_lines="3 13" declare l_dummy dual.dummy%type; no_data_found exception; -- violates also naming convention G-9113 @@ -43,7 +43,7 @@ ORA-06512: at line 7 ## Example (good) -``` sql +``` sql hl_lines="3 14" declare l_dummy dual.dummy%type; e_empty_value exception; diff --git a/docs/4-language-usage/5-exception-handling/g-5040.md b/docs/4-language-usage/5-exception-handling/g-5040.md index b98a4d7b..25228aba 100644 --- a/docs/4-language-usage/5-exception-handling/g-5040.md +++ b/docs/4-language-usage/5-exception-handling/g-5040.md @@ -11,7 +11,7 @@ When using a logging framework like Logger, consider making an exception to this ## Example (bad) -``` sql +``` sql hl_lines="4" begin my_package.some_processing(); exception @@ -23,7 +23,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" begin my_package.some_processing(); exception @@ -35,7 +35,7 @@ end; An exception to the rule where `when others` can be good to log the error and then re-raise it: -``` sql +``` sql hl_lines="4-6" begin my_package.some_processing(); exception diff --git a/docs/4-language-usage/5-exception-handling/g-5050.md b/docs/4-language-usage/5-exception-handling/g-5050.md index eb66c0a5..7660f871 100644 --- a/docs/4-language-usage/5-exception-handling/g-5050.md +++ b/docs/4-language-usage/5-exception-handling/g-5050.md @@ -9,7 +9,7 @@ If you are not very organized in the way you allocate, define and use the error ## Example (bad) -``` sql +``` sql hl_lines="4" declare co_invalid_emp_text constant types_up.text := 'Invalid employee_id'; begin @@ -20,7 +20,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" begin err_up.raise(in_error => err.co_invalid_employee_id); end; diff --git a/docs/4-language-usage/5-exception-handling/g-5060.md b/docs/4-language-usage/5-exception-handling/g-5060.md index d83fe8a9..0c7a0332 100644 --- a/docs/4-language-usage/5-exception-handling/g-5060.md +++ b/docs/4-language-usage/5-exception-handling/g-5060.md @@ -13,7 +13,7 @@ The form that this failure takes does not necessarily need to be an exception. W ## Example (bad) -``` sql +``` sql hl_lines="7-8" create or replace package body department_api is function name_by_id(in_id in departments.department_id%type) -- NOSONAR: non-deterministic return departments.department_name%type is @@ -33,7 +33,7 @@ end department_api; ## Example (good) -``` sql +``` sql hl_lines="7-8 13-17" create or replace package body department_api is function name_by_id(in_id in departments.department_id%type) -- NOSONAR: non-deterministic return departments.department_name%type is diff --git a/docs/4-language-usage/5-exception-handling/g-5070.md b/docs/4-language-usage/5-exception-handling/g-5070.md index 9beef1f9..fa250364 100644 --- a/docs/4-language-usage/5-exception-handling/g-5070.md +++ b/docs/4-language-usage/5-exception-handling/g-5070.md @@ -13,7 +13,7 @@ Being as specific as possible with the errors raised will allow developers to ch ## Example (bad) -``` sql +``` sql hl_lines="2" begin raise no_data_found; end; @@ -22,7 +22,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" declare e_my_exception exception; begin diff --git a/docs/4-language-usage/5-exception-handling/g-5080.md b/docs/4-language-usage/5-exception-handling/g-5080.md index d75de554..d4f4c4af 100644 --- a/docs/4-language-usage/5-exception-handling/g-5080.md +++ b/docs/4-language-usage/5-exception-handling/g-5080.md @@ -11,7 +11,7 @@ If you use `sqlerrm` or `format_error_stack` to log/display error, you should al ## Example (bad) -``` sql +``` sql hl_lines="19" create or replace package body order_api as procedure discount_and_recalculate( in_customer_id in integer @@ -39,7 +39,7 @@ end order_api; ## Example (good) -``` sql +``` sql hl_lines="22 24" create or replace package body order_api as procedure discount_and_recalculate( in_customer_id in integer 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 b2ee5faa..702bd3f3 100644 --- a/docs/4-language-usage/6-dynamic-sql/g-6010.md +++ b/docs/4-language-usage/6-dynamic-sql/g-6010.md @@ -9,7 +9,7 @@ Having the executed statement in a variable makes it easier to debug your code ( ## Example (bad) -``` sql +``` sql hl_lines="4" declare l_next_val employees.employee_id%type; begin @@ -21,7 +21,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="6" declare l_next_val employees.employee_id%type; co_sql constant types_up.big_string_type := diff --git a/docs/4-language-usage/6-dynamic-sql/g-6020.md b/docs/4-language-usage/6-dynamic-sql/g-6020.md index e9da6941..9db6a0e0 100644 --- a/docs/4-language-usage/6-dynamic-sql/g-6020.md +++ b/docs/4-language-usage/6-dynamic-sql/g-6020.md @@ -12,7 +12,7 @@ You should use the `returning into` clause for values returned from a DML operat ## Example (bad) -``` sql +``` sql hl_lines="16" create or replace package body employee_api is procedure upd_salary( in_employee_id in employees.employee_id%type @@ -36,7 +36,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="17" create or replace package body employee_api is procedure upd_salary( in_employee_id in employees.employee_id%type diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7110.md b/docs/4-language-usage/7-stored-objects/1-general/g-7110.md index 4c332bfc..01cafe98 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7110.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7110.md @@ -11,7 +11,7 @@ This is not needed for standard functions like (`to_char`, `to_date`, `nvl`, `ro ## Example (bad) -``` sql +``` sql hl_lines="5" declare r_employee employees%rowtype; co_id constant employees.employee_id%type := 107; @@ -23,7 +23,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="6 7" declare r_employee employees%rowtype; co_id constant employees.employee_id%type := 107; diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7120.md b/docs/4-language-usage/7-stored-objects/1-general/g-7120.md index a51a9c5f..8e921622 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7120.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7120.md @@ -9,10 +9,9 @@ It's a good alternative for comments to indicate the end of program units, espec ## Example (bad) -``` sql +``` sql hl_lines="18 19" create or replace package body employee_api is - function -- NOSONAR: non-deterministic - employee_by_id(in_employee_id in integer) + function employee_by_id(in_employee_id in integer) -- NOSONAR: non-deterministic return employees%rowtype is co_employee_id constant employees.employee_id%type := in_employee_id; r_employee employees%rowtype; @@ -35,7 +34,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="18 19" create or replace package body employee_api is function employee_by_id(in_employee_id in integer) -- NOSONAR: non-deterministic return employees%rowtype is diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7125.md b/docs/4-language-usage/7-stored-objects/1-general/g-7125.md index 01133a55..f863ddba 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7125.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7125.md @@ -9,7 +9,7 @@ Using `create` alone makes your scripts give an error if the program unit alread ## Example (bad) -``` sql +``` sql hl_lines="1" create package body employee_api is function employee_by_id(in_employee_id in integer) -- NOSONAR: non-deterministic return employees%rowtype is @@ -34,7 +34,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="1" create or replace package body employee_api is function employee_by_id(in_employee_id in integer) -- NOSONAR: non-deterministic return employees%rowtype is diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7130.md b/docs/4-language-usage/7-stored-objects/1-general/g-7130.md index b119e783..075aaa8e 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7130.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7130.md @@ -11,7 +11,7 @@ This external dependency is hidden, and may cause problems in the future. You sh ## Example (bad) -``` sql +``` sql hl_lines="6 9 10" create or replace package body employee_api is procedure calc_salary(in_employee_id in integer) is co_employee_id constant employees.employee_id%type := in_employee_id; @@ -45,7 +45,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="7 8 17 18" create or replace package body employee_api is procedure calc_salary(in_employee_id in integer) is co_employee_id constant employees.employee_id%type := in_employee_id; diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7140.md b/docs/4-language-usage/7-stored-objects/1-general/g-7140.md index 8b77cdfc..d9afa648 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7140.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7140.md @@ -13,7 +13,7 @@ There is never a better time to review all the steps you took, and to understand ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package body my_package is procedure my_procedure is function my_func return number @@ -32,7 +32,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="3 11" create or replace package body my_package is procedure my_procedure is function my_func return number diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7150.md b/docs/4-language-usage/7-stored-objects/1-general/g-7150.md index 4a56646a..a2ddd06f 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7150.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7150.md @@ -9,7 +9,7 @@ You should go through your programs and remove any parameter that is no longer u ## Example (bad) -``` sql +``` sql hl_lines="4" create or replace package body department_api is function name_by_id( -- NOSONAR: non-deterministic in_department_id in integer diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7160.md b/docs/4-language-usage/7-stored-objects/1-general/g-7160.md index 9b9dd239..4c7a6a22 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7160.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7160.md @@ -9,7 +9,7 @@ By showing the mode of parameters, you help the reader. If you do not specify a ## Example (bad) -``` sql +``` sql hl_lines="3-6" create or replace package employee_api is procedure store(io_id in out employees.id%type ,in_first_name employees.first_name%type @@ -23,7 +23,7 @@ end employee_up; ## Example (good) -``` sql +``` sql hl_lines="3-6" create or replace package employee_api is procedure store(io_id in out employees.id%type ,in_first_name in employees.first_name%type diff --git a/docs/4-language-usage/7-stored-objects/1-general/g-7170.md b/docs/4-language-usage/7-stored-objects/1-general/g-7170.md index 92b8401c..91c285f6 100644 --- a/docs/4-language-usage/7-stored-objects/1-general/g-7170.md +++ b/docs/4-language-usage/7-stored-objects/1-general/g-7170.md @@ -12,7 +12,7 @@ Avoid using parameter mode `in out` unless you actually use the parameter both a ## Example (bad) -``` sql +``` sql hl_lines="3-12" create or replace package body employee_up is procedure rcv_emp( io_first_name in out employees.first_name%type @@ -56,7 +56,7 @@ end employee_up; ## Example (good) -``` sql +``` sql hl_lines="3-12" create or replace package body employee_up is procedure rcv_emp( out_first_name out employees.first_name%type diff --git a/docs/4-language-usage/7-stored-objects/2-packages/g-7220.md b/docs/4-language-usage/7-stored-objects/2-packages/g-7220.md index 8bbf2f19..58dbaabb 100644 --- a/docs/4-language-usage/7-stored-objects/2-packages/g-7220.md +++ b/docs/4-language-usage/7-stored-objects/2-packages/g-7220.md @@ -9,7 +9,7 @@ Having forward declarations allows you to order the functions and procedures of ## Example (bad) -``` sql +``` sql hl_lines="7" create or replace package department_api is procedure del(in_department_id in departments.department_id%type); end department_api; @@ -48,7 +48,7 @@ end department_api; ## Example (good) -``` sql +``` sql hl_lines="7 18" create or replace package department_api is procedure del(in_department_id in departments.department_id%type); end department_api; diff --git a/docs/4-language-usage/7-stored-objects/2-packages/g-7230.md b/docs/4-language-usage/7-stored-objects/2-packages/g-7230.md index 250d78d6..e852703f 100644 --- a/docs/4-language-usage/7-stored-objects/2-packages/g-7230.md +++ b/docs/4-language-usage/7-stored-objects/2-packages/g-7230.md @@ -15,7 +15,7 @@ For package-level constants, consider whether the constant should be public and ## Example (bad) -``` sql +``` sql hl_lines="4" create or replace package employee_api as co_min_increase constant types_up.sal_increase_type := 0.01; co_max_increase constant types_up.sal_increase_type := 0.5; @@ -44,7 +44,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="14" create or replace package constants_up as co_min_increase constant types_up.sal_increase_type := 0.01; co_max_increase constant types_up.sal_increase_type := 0.5; diff --git a/docs/4-language-usage/7-stored-objects/2-packages/g-7250.md b/docs/4-language-usage/7-stored-objects/2-packages/g-7250.md index fe8af638..30549d12 100644 --- a/docs/4-language-usage/7-stored-objects/2-packages/g-7250.md +++ b/docs/4-language-usage/7-stored-objects/2-packages/g-7250.md @@ -9,7 +9,7 @@ The purpose of the initialization block of a package body is to set initial valu ## Example (bad) -``` sql +``` sql hl_lines="17-19" create or replace package body employee_api as g_salary_increase types_up.sal_increase_type; @@ -37,7 +37,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="17" create or replace package body employee_api as g_salary_increase types_up.sal_increase_type; diff --git a/docs/4-language-usage/7-stored-objects/3-procedures/g-7310.md b/docs/4-language-usage/7-stored-objects/3-procedures/g-7310.md index f832ea85..a127d306 100644 --- a/docs/4-language-usage/7-stored-objects/3-procedures/g-7310.md +++ b/docs/4-language-usage/7-stored-objects/3-procedures/g-7310.md @@ -11,7 +11,7 @@ Package bodies may be changed and compiled without invalidating other packages. ## Example (bad) -``` sql +``` sql hl_lines="1" create or replace procedure my_procedure is begin null; @@ -21,7 +21,7 @@ end my_procedure; ## Example (good) -``` sql +``` sql hl_lines="7" create or replace package my_package is procedure my_procedure; end my_package; diff --git a/docs/4-language-usage/7-stored-objects/3-procedures/g-7320.md b/docs/4-language-usage/7-stored-objects/3-procedures/g-7320.md index f7e6ae87..302123d9 100644 --- a/docs/4-language-usage/7-stored-objects/3-procedures/g-7320.md +++ b/docs/4-language-usage/7-stored-objects/3-procedures/g-7320.md @@ -11,7 +11,7 @@ A good general rule to follow as you write your PL/SQL programs is "one way in a ## Example (bad) -``` sql +``` sql hl_lines="9" create or replace package body my_package is procedure my_procedure is l_idx simple_integer := 1; @@ -32,7 +32,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="8" create or replace package body my_package is procedure my_procedure is l_idx simple_integer := 1; diff --git a/docs/4-language-usage/7-stored-objects/3-procedures/g-7330.md b/docs/4-language-usage/7-stored-objects/3-procedures/g-7330.md index 50902f86..0ea69ab9 100644 --- a/docs/4-language-usage/7-stored-objects/3-procedures/g-7330.md +++ b/docs/4-language-usage/7-stored-objects/3-procedures/g-7330.md @@ -9,7 +9,7 @@ Marking a parameter for output means that callers will expect its value to be up ## Example (bad) -``` sql +``` sql hl_lines="4" create or replace package body my_package is procedure greet( in_name in varchar2 @@ -27,7 +27,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="4 9" create or replace package body my_package is procedure greet( in_name in varchar2 diff --git a/docs/4-language-usage/7-stored-objects/4-functions/g-7410.md b/docs/4-language-usage/7-stored-objects/4-functions/g-7410.md index a82bb08e..38bbf4a4 100644 --- a/docs/4-language-usage/7-stored-objects/4-functions/g-7410.md +++ b/docs/4-language-usage/7-stored-objects/4-functions/g-7410.md @@ -11,7 +11,7 @@ Package bodies may be changed and compiled without invalidating other packages. ## Example (bad) -``` sql +``` sql hl_lines="1" create or replace function my_function return varchar2 deterministic is @@ -23,7 +23,7 @@ end my_function; ## Example (good) -``` sql +``` sql hl_lines="2" create or replace package body my_package is function my_function return varchar2 deterministic diff --git a/docs/4-language-usage/7-stored-objects/4-functions/g-7420.md b/docs/4-language-usage/7-stored-objects/4-functions/g-7420.md index b267e88a..964d368c 100644 --- a/docs/4-language-usage/7-stored-objects/4-functions/g-7420.md +++ b/docs/4-language-usage/7-stored-objects/4-functions/g-7420.md @@ -9,7 +9,7 @@ The reader expects the `return` statement to be the last statement of a function ## Example (bad) -``` sql +``` sql hl_lines="16" create or replace package body my_package is function my_function( in_from in pls_integer @@ -35,7 +35,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="16" create or replace package body my_package is function my_function( in_from in pls_integer diff --git a/docs/4-language-usage/7-stored-objects/4-functions/g-7430.md b/docs/4-language-usage/7-stored-objects/4-functions/g-7430.md index df5968c7..d5429d33 100644 --- a/docs/4-language-usage/7-stored-objects/4-functions/g-7430.md +++ b/docs/4-language-usage/7-stored-objects/4-functions/g-7430.md @@ -9,7 +9,7 @@ A function should have a single point of entry as well as a single exit-point. ## Example (bad) -``` sql +``` sql hl_lines="8 10" create or replace package body my_package is function my_function(in_value in pls_integer) return boolean deterministic @@ -28,7 +28,7 @@ end my_package; ## Example (better) -``` sql +``` sql hl_lines="14" create or replace package body my_package is function my_function(in_value in pls_integer) return boolean deterministic @@ -50,7 +50,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="7" create or replace package body my_package is function my_function(in_value in pls_integer) return boolean deterministic diff --git a/docs/4-language-usage/7-stored-objects/4-functions/g-7440.md b/docs/4-language-usage/7-stored-objects/4-functions/g-7440.md index 30a4499a..095c8d46 100644 --- a/docs/4-language-usage/7-stored-objects/4-functions/g-7440.md +++ b/docs/4-language-usage/7-stored-objects/4-functions/g-7440.md @@ -9,7 +9,7 @@ A function should return all its data through the `return` clause. Having an `ou ## Example (bad) -``` sql +``` sql hl_lines="2" create or replace package body my_package is function my_function(out_date out date) return boolean deterministic @@ -24,7 +24,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="2" create or replace package body my_package is function my_function return date deterministic diff --git a/docs/4-language-usage/7-stored-objects/4-functions/g-7450.md b/docs/4-language-usage/7-stored-objects/4-functions/g-7450.md index d26633cb..47c0a203 100644 --- a/docs/4-language-usage/7-stored-objects/4-functions/g-7450.md +++ b/docs/4-language-usage/7-stored-objects/4-functions/g-7450.md @@ -9,7 +9,7 @@ If a boolean function returns `null`, the caller has do deal with it. This makes ## Example (bad) -``` sql +``` sql hl_lines="6" create or replace package body my_package is function my_function return boolean deterministic @@ -23,7 +23,7 @@ end my_package; ## Example (good) -``` sql +``` sql hl_lines="6" create or replace package body my_package is function my_function return boolean deterministic diff --git a/docs/4-language-usage/7-stored-objects/4-functions/g-7460.md b/docs/4-language-usage/7-stored-objects/4-functions/g-7460.md index 83c8dcd5..a16c3e60 100644 --- a/docs/4-language-usage/7-stored-objects/4-functions/g-7460.md +++ b/docs/4-language-usage/7-stored-objects/4-functions/g-7460.md @@ -9,7 +9,7 @@ A deterministic function (always return same result for identical parameters) wh ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package department_api is function name_by_id(in_department_id in departments.department_id%type) return departments.department_name%type; @@ -19,7 +19,7 @@ end department_api; ## Example (good) -``` sql +``` sql hl_lines="3 4" create or replace package department_api is function name_by_id(in_department_id in departments.department_id%type) return departments.department_name%type diff --git a/docs/4-language-usage/7-stored-objects/5-oracle-supplied-packages/g-7510.md b/docs/4-language-usage/7-stored-objects/5-oracle-supplied-packages/g-7510.md index 2be0b0d7..b3873e45 100644 --- a/docs/4-language-usage/7-stored-objects/5-oracle-supplied-packages/g-7510.md +++ b/docs/4-language-usage/7-stored-objects/5-oracle-supplied-packages/g-7510.md @@ -9,7 +9,7 @@ The signature of Oracle supplied packages is well known and therefore it is quit ## Example (bad) -``` sql +``` sql hl_lines="4" declare co_hello_world constant string(30 char) := 'Hello World'; begin @@ -20,7 +20,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" declare co_hello_world constant string(30 char) := 'Hello World'; begin diff --git a/docs/4-language-usage/7-stored-objects/7-triggers/g-7710.md b/docs/4-language-usage/7-stored-objects/7-triggers/g-7710.md index cd6496b3..83bfcc89 100644 --- a/docs/4-language-usage/7-stored-objects/7-triggers/g-7710.md +++ b/docs/4-language-usage/7-stored-objects/7-triggers/g-7710.md @@ -9,7 +9,7 @@ Having triggers that act on other tables in a way that causes triggers on that t ## Example (bad) -``` sql +``` sql hl_lines="4 19 21" create or replace trigger dept_br_u before update on departments for each row begin @@ -44,7 +44,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4 17" create or replace trigger dept_br_u before update on departments for each row begin diff --git a/docs/4-language-usage/7-stored-objects/7-triggers/g-7720.md b/docs/4-language-usage/7-stored-objects/7-triggers/g-7720.md index 862c9ab5..e3319f0f 100644 --- a/docs/4-language-usage/7-stored-objects/7-triggers/g-7720.md +++ b/docs/4-language-usage/7-stored-objects/7-triggers/g-7720.md @@ -9,7 +9,7 @@ A DML trigger can have multiple triggering events separated by `or` like `before ## Example (bad) -``` sql +``` sql hl_lines="2" create or replace trigger dept_br_u before update of department_id or update of department_name -- violates also G-7730 on departments for each row @@ -29,7 +29,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2" create or replace trigger dept_br_u before update of department_id,department_name on departments for each row diff --git a/docs/4-language-usage/7-stored-objects/7-triggers/g-7730.md b/docs/4-language-usage/7-stored-objects/7-triggers/g-7730.md index eb36b147..1da0312d 100644 --- a/docs/4-language-usage/7-stored-objects/7-triggers/g-7730.md +++ b/docs/4-language-usage/7-stored-objects/7-triggers/g-7730.md @@ -11,7 +11,7 @@ If the trigger makes assignment to a primary key and there are child tables with ## Example (bad) -``` sql +``` sql hl_lines="2" create or replace trigger dept_br_iu before insert or update on departments for each row @@ -28,7 +28,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2 10" create or replace trigger dept_br_i before insert on departments for each row diff --git a/docs/4-language-usage/7-stored-objects/7-triggers/g-7740.md b/docs/4-language-usage/7-stored-objects/7-triggers/g-7740.md index 92db74cc..52800e3a 100644 --- a/docs/4-language-usage/7-stored-objects/7-triggers/g-7740.md +++ b/docs/4-language-usage/7-stored-objects/7-triggers/g-7740.md @@ -14,7 +14,7 @@ This locking of child tables behaviour goes for simple DML triggers as well as c ## Example (bad) -``` sql +``` sql hl_lines="2 6" create or replace trigger dept_br_iu before insert or update -- NOSONAR: G-7730 on departments for each row @@ -32,7 +32,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="2 5 11" create or replace trigger dept_br_i before insert on departments for each row diff --git a/docs/4-language-usage/7-stored-objects/8-sequences/g-7810.md b/docs/4-language-usage/7-stored-objects/8-sequences/g-7810.md index bb74db5a..891f3d57 100644 --- a/docs/4-language-usage/7-stored-objects/8-sequences/g-7810.md +++ b/docs/4-language-usage/7-stored-objects/8-sequences/g-7810.md @@ -9,7 +9,7 @@ Since Oracle Database 11g it is no longer needed to use a `select` statement to ## Example (bad) -``` sql +``` sql hl_lines="4" declare l_sequence_number employees.employee_id%type; begin @@ -23,7 +23,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="4" declare l_sequence_number employees.employee_id%type; begin diff --git a/docs/4-language-usage/7-stored-objects/9-sql-macros/g-7910.md b/docs/4-language-usage/7-stored-objects/9-sql-macros/g-7910.md index f25e530c..234187da 100644 --- a/docs/4-language-usage/7-stored-objects/9-sql-macros/g-7910.md +++ b/docs/4-language-usage/7-stored-objects/9-sql-macros/g-7910.md @@ -15,7 +15,7 @@ Logging macro calls via a call to a procedure that does DML in an autonomous tra ## Example (bad) -``` sql +``` sql hl_lines="5-7" create or replace package body my_package is function row_generator(in_num_rows in number) -- NOSONAR: non-deterministic return varchar2 sql_macro as diff --git a/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8110.md b/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8110.md index 8407b9ae..34cd29ea 100644 --- a/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8110.md +++ b/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8110.md @@ -9,7 +9,7 @@ If you do a `select count(*)` all rows will be read according to the `where` cla ## Example (bad) -``` sql +``` sql hl_lines="6" declare l_count pls_integer; co_zero constant simple_integer := 0; @@ -37,7 +37,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="8" declare co_salary constant employees.salary%type := 5000; begin diff --git a/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8120.md b/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8120.md index 959c009a..f69e7208 100644 --- a/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8120.md +++ b/docs/4-language-usage/8-patterns/1-checking-the-number-of-rows/g-8120.md @@ -9,7 +9,7 @@ The result of an existence check is a snapshot of the current situation. You nev ## Example (bad) -``` sql +``` sql hl_lines="10 11" create or replace package body department_api is procedure ins(in_r_department in departments%rowtype) is l_count pls_integer; @@ -30,7 +30,7 @@ end department_api; ## Example (good) -``` sql +``` sql hl_lines="4 7" create or replace package body department_api is procedure ins(in_r_department in departments%rowtype) is begin diff --git a/docs/4-language-usage/8-patterns/2-access-objects-of-foreign-application-schemas/g-8210.md b/docs/4-language-usage/8-patterns/2-access-objects-of-foreign-application-schemas/g-8210.md index 88726ca3..d085708a 100644 --- a/docs/4-language-usage/8-patterns/2-access-objects-of-foreign-application-schemas/g-8210.md +++ b/docs/4-language-usage/8-patterns/2-access-objects-of-foreign-application-schemas/g-8210.md @@ -9,7 +9,7 @@ If a connection is needed to a table that is placed in a foreign schema, using s ## Example (bad) -``` sql +``` sql hl_lines="7" declare l_product_name oe.products.product_name%type; co_price constant oe.products@list_price%type := 1000; @@ -29,7 +29,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="1 9" create synonym oe_products for oe.products; declare diff --git a/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md b/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md index 95035ded..7f0bc30a 100644 --- a/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md +++ b/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md @@ -23,7 +23,7 @@ To limit the number of false positives, only the following data types used in pa ## Example (bad) -``` sql +``` sql hl_lines="3" create or replace package body department_api is function dept_by_name( -- NOSONAR: non-deterministic in_dept_name in departments.department_name%type @@ -56,7 +56,7 @@ end department_api; ## Example (good) -``` sql +``` sql hl_lines="3 6" create or replace package body department_api is function dept_by_name( -- NOSONAR: non-deterministic in_dept_name in departments.department_name%type @@ -86,7 +86,7 @@ end department_api; The exception should be handled where the function is called, like this: -``` sql +``` sql hl_lines="8" declare co_dept_name constant type_up.text := 'Far to long name of a department'; begin diff --git a/docs/4-language-usage/8-patterns/4-ensure-single-execution-at-a-time-of-a-program-unit/g-8410.md b/docs/4-language-usage/8-patterns/4-ensure-single-execution-at-a-time-of-a-program-unit/g-8410.md index f7b41e97..abda8df0 100644 --- a/docs/4-language-usage/8-patterns/4-ensure-single-execution-at-a-time-of-a-program-unit/g-8410.md +++ b/docs/4-language-usage/8-patterns/4-ensure-single-execution-at-a-time-of-a-program-unit/g-8410.md @@ -14,7 +14,7 @@ The alternative using a table where a “Lock-Row” is stored has the disadvant ## Example (bad) -``` sql +``` sql hl_lines="12 18" /* Example */ create or replace package body lock_up is -- manage locks in a dedicated table created as follows: @@ -59,7 +59,7 @@ end; ## Example (good) -``` sql +``` sql hl_lines="17 32" /* Example */ create or replace package body lock_up is function request_lock( -- NOSONAR: non-deterministic diff --git a/docs/4-language-usage/8-patterns/5-use-dbms-application-info-package-to-follow-progress-of-a-process/g-8510.md b/docs/4-language-usage/8-patterns/5-use-dbms-application-info-package-to-follow-progress-of-a-process/g-8510.md index ccb38196..75484611 100644 --- a/docs/4-language-usage/8-patterns/5-use-dbms-application-info-package-to-follow-progress-of-a-process/g-8510.md +++ b/docs/4-language-usage/8-patterns/5-use-dbms-application-info-package-to-follow-progress-of-a-process/g-8510.md @@ -12,7 +12,7 @@ This technique allows us to view progress of a process without having to persist ## Example (bad) -``` sql +``` sql hl_lines="12 18" create or replace package body employee_api is procedure process_emps is begin @@ -33,7 +33,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="18" create or replace package body employee_api is procedure process_emps is co_action_name constant v$session.action%type := 'init'; 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 c0c0c038..68f51238 100644 --- a/docs/4-language-usage/9-function-usage/g-9010.md +++ b/docs/4-language-usage/9-function-usage/g-9010.md @@ -11,7 +11,7 @@ Using an explicit format model for string to `date` or `timestamp` conversion av ## Example (bad) -``` sql +``` sql hl_lines="10" create or replace package body employee_api is procedure set_dob( in_employee_id in employees.employee_id%type @@ -30,7 +30,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="12" create or replace package body employee_api is procedure set_dob( in_employee_id in employees.employee_id%type 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 874f8897..1c9843db 100644 --- a/docs/4-language-usage/9-function-usage/g-9020.md +++ b/docs/4-language-usage/9-function-usage/g-9020.md @@ -11,7 +11,7 @@ To avoid an inappropriate dependability on configurable NLS parameters, try to u ## Example (bad) -``` sql +``` sql hl_lines="10" create or replace package body employee_api is procedure set_salary( in_employee_id in employees.employee_id%type @@ -30,7 +30,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="12 13" create or replace package body employee_api is procedure set_salary( in_employee_id in employees.employee_id%type diff --git a/docs/4-language-usage/9-function-usage/g-9030.md b/docs/4-language-usage/9-function-usage/g-9030.md index 28ecccd3..63469f20 100644 --- a/docs/4-language-usage/9-function-usage/g-9030.md +++ b/docs/4-language-usage/9-function-usage/g-9030.md @@ -13,7 +13,7 @@ When converting from strings to other datatypes using a conversion function that ## Example (bad) -``` sql +``` sql hl_lines="10" create or replace package body employee_api is procedure set_dob( in_employee_id in employees.employee_id%type @@ -32,7 +32,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="11" create or replace package body employee_api is procedure set_dob( in_employee_id in employees.employee_id%type 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 6404d323..109e381e 100644 --- a/docs/4-language-usage/9-function-usage/g-9040.md +++ b/docs/4-language-usage/9-function-usage/g-9040.md @@ -13,7 +13,7 @@ The exception to this rule can be if you are converting textual input typed by a ## Example (bad) -``` sql +``` sql hl_lines="12" create or replace package body employee_api is procedure set_dob( in_employee_id in employees.employee_id%type @@ -35,7 +35,7 @@ end employee_api; ## Example (good) -``` sql +``` sql hl_lines="12" create or replace package body employee_api is procedure set_dob( in_employee_id in employees.employee_id%type diff --git a/docs/9-appendix/appendix.md b/docs/9-appendix/appendix.md index 423893ff..0c874a3f 100644 --- a/docs/9-appendix/appendix.md +++ b/docs/9-appendix/appendix.md @@ -54,7 +54,7 @@ n/a | 3150 | Try to use identity columns for surrogate keys. | Critical | | | n/a | 3160 | Avoid visible virtual columns. | Blocker | | | ✘ | | ✘ | | | n/a | 3170 | Always use DEFAULT ON NULL declarations to assign default values to table columns if you refuse to store NULL values. | Blocker | | | | | ✘ | | | n/a | 3180 | Always specify column names instead of positional references in ORDER BY clauses. | Major | ✘ | | | | ✘ | | | -n/a | 3182 | Always specify column names instead of positional references in GROUP BY clauses. | Blocker | | | | | ✘ | | | +n/a | 3182 | Always specify column names/aliases instead of positional references in GROUP BY clauses. | Blocker | | | | | ✘ | | | n/a | 3183 | Always specify column aliases instead of expressions in GROUP BY clauses. | Minor | | | ✘ | | | | | n/a | 3185 | Never use ROWNUM at the same query level as ORDER BY. | Blocker | | | | | ✘ | | | ✘ n/a | 3190 | Avoid using NATURAL JOIN. | Blocker | ✘ | | | | ✘ | | | @@ -63,6 +63,7 @@ n/a | 3195 | Always use wildcards in a LIKE clause. | Blocker | | | ✘ | n/a | 3220 | Always process saved exceptions from a FORALL statement. | Critical | | | | | ✘ | | | ✘ n/a | 3310 | Never commit within a cursor loop. | Blocker | | ✘ | | | ✘ | | | n/a | 3320 | Try to move transactions within a non-cursor loop into procedures. | Major | | | ✘ | | | ✘ | | ✘ +n/a | 3330 | Avoid autonomous transactions. | Blocker | | | | | ✘ | | | ✘ 31 | 4110 | Always use %NOTFOUND instead of NOT %FOUND to check whether a cursor returned data. | Minor | | | ✘ | | | | | 32 | 4120 | Avoid using %NOTFOUND directly after the FETCH when working with BULK OPERATIONS and LIMIT clause. | Blocker | | | | | ✘ | | | 33 | 4130 | Always close locally opened cursors. | Blocker | | ✘ | | | ✘ | | | @@ -86,6 +87,7 @@ n/a | 4365 | Never use unconditional CONTINUE or EXIT in a loop. | Major | | | 46 | 4375 | Always use EXIT WHEN instead of an IF statement to exit from a loop. | Minor | | | ✘ | | | | | 47 | 4380 | Try to label your EXIT WHEN statements. | Minor | | | ✘ | | | | | 48 | 4385 | Never use a cursor for loop to check whether a cursor returns data. | Critical | | ✘ | | | | | | +n/a | 4387 | Never use a FOR LOOP for a query that should return not more than one row. | Blocker | | ✘ | ✘ | | ✘ | | | 49 | 4390 | Avoid use of unreferenced FOR loop indexes. | Major | | ✘ | | | | | | 50 | 4395 | Avoid hard-coded upper or lower bound values with FOR loops. | Minor | ✘ | | ✘ | | | | | n/a | 5010 | Try to use a error/logging framework for your application. | Critical | | | | | ✘ | ✘ | | ✘ diff --git a/docs/index.md b/docs/index.md index 9b025962..9279a590 100644 --- a/docs/index.md +++ b/docs/index.md @@ -48,7 +48,7 @@ The authors and publisher shall have neither liability nor responsibility to any ## Revision History -The first version of these guidelines was compiled by Roger Troller on March 17, 2009. Jörn Kulessa, Daniela Reiner, Richard Bushnell, Andreas Flubacher and Thomas Mauch helped Roger complete version 1.2 until August 21, 2009. This was the first GA version. The handy printed version in A5 format was distributed free of charge at the DOAG Annual Conference and on other occasions. Since then Roger updated the guidelines regularily. Philipp Salvisberg was involved in the review process for version 3.0 which was a major update. Philipp took the lead, after Roger left Trivadis in 2016. In 2020 Kim Berg Hansen started handling guidelines maintenance, letting Philipp concentrate on the related [Trivadis db* CODECOP](https://cdn.trivadis.com/downloads/dbCODECOP-fs-en.pdf) tool. +The first version of these guidelines was compiled by Roger Troller on March 17, 2009. Jörn Kulessa, Daniela Reiner, Richard Bushnell, Andreas Flubacher and Thomas Mauch helped Roger complete version 1.2 until August 21, 2009. This was the first GA version. The handy printed version in A5 format was distributed free of charge at the DOAG Annual Conference and on other occasions. Since then Roger updated the guidelines regularily. Philipp Salvisberg was involved in the review process for version 3.0 which was a major update. Philipp took the lead, after Roger left Trivadis in 2016. In 2020 Kim Berg Hansen started handling guidelines maintenance, letting Philipp concentrate on the related [Trivadis db* CODECOP](https://github.com/search?q=topic%3Acodecop+org%3ATrivadis+fork%3Atrue&type=repositories) tool suite. After Kim left in September 2022, Philipp took over again until August 2024. Since July, 7 2018 these guidelines are hosted on GitHub. Ready to be enhanced by the community and forked to fit specific needs. diff --git a/docs/stylesheets/extra.css b/docs/stylesheets/extra.css index d25833ac..45c8c150 100644 --- a/docs/stylesheets/extra.css +++ b/docs/stylesheets/extra.css @@ -46,6 +46,10 @@ color: #ff1744; } +.highlight .hll { + background-color: rgba(215, 215, 215, .5); +} + /* fixing colors because wkhtmltopdf does not understand colors such as "var(--md-typeset-a-color);" */ @media print { diff --git a/mkdocs.yml b/mkdocs.yml index f645e615..691751a3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -36,7 +36,7 @@ markdown_extensions: - footnotes extra: - guideline_version: 4.3 + guideline_version: 4.5-SNAPSHOT social: - icon: fontawesome/brands/github-alt link: https://github.com/Trivadis