Skip to content

Commit

Permalink
Fix violations of G-1050, G-5060, G-7460
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippSalvisberg committed Aug 25, 2023
1 parent aeec9b4 commit 6f523eb
Showing 1 changed file with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,30 @@ This technique raises an error (`value_error`) which may not be handled in the c

``` sql
create or replace package body department_api is
function dept_by_name(in_dept_name in departments.department_name%type)
function dept_by_name( -- NOSONAR: non-deterministic
in_dept_name in departments.department_name%type
)
return departments%rowtype is
r_return departments%rowtype;
r_return departments%rowtype;
co_max_dept_name_length constant integer := 20;
begin
if in_dept_name is null or length(in_dept_name) > 20 then
if in_dept_name is null or length(in_dept_name) > co_max_dept_name_length then
raise err.e_param_to_large;
end if;
-- get the department by name
select *
into r_return
from departments
where department_name = in_dept_name;

return r_return;
<<trap>>
begin
select *
into r_return
from departments
where department_name = in_dept_name;
return r_return;
exception
when no_data_found then
return null;
when too_many_rows then
raise;
end trap;
end dept_by_name;
end department_api;
/
Expand All @@ -34,18 +44,27 @@ end department_api;

``` sql
create or replace package body department_api is
function dept_by_name(in_dept_name in departments.department_name%type)
function dept_by_name( -- NOSONAR: non-deterministic
in_dept_name in departments.department_name%type
)
return departments%rowtype is
l_dept_name departments.department_name%type not null := in_dept_name;
r_return departments%rowtype;
co_dept_name constant departments.department_name%type not null := in_dept_name;
r_return departments%rowtype;
begin
-- get the department by name
select *
into r_return
from departments
where department_name = l_dept_name;

return r_return;
<<trap>>
begin
select *
into r_return
from departments
where department_name = co_dept_name;
return r_return;
exception
when no_data_found then
return null;
when too_many_rows then
raise;
end trap;
end dept_by_name;
end department_api;
/
Expand All @@ -54,9 +73,11 @@ end department_api;
The exception should be handled where the function is called, like this:

``` sql
declare
co_dept_name constant type_up.text := 'Far to long name of a department';
begin
pre_processing;
r_department := department_api.dept_by_name('Far to long name of a department');
r_department := department_api.dept_by_name(co_dept_name);
post_processing;
exception
when value_error then
Expand Down

0 comments on commit 6f523eb

Please sign in to comment.